Skip to content
Snippets Groups Projects

Rewrite devkit service in python.

Merged Jeremy Whiting requested to merge pythonrewrite into main
1 file
+ 11
11
Compare changes
  • Side-by-side
  • Inline
@@ -57,7 +57,7 @@ HOOK_DIRS = []
@@ -57,7 +57,7 @@ HOOK_DIRS = []
USE_DEFAULT_HOOKS = True
USE_DEFAULT_HOOKS = True
def writefile(data):
def writefile(data: bytes) -> str:
# Get 1 from the resulting tuple, since that's the filename
# Get 1 from the resulting tuple, since that's the filename
filename = tempfile.mkstemp(prefix="devkit-", dir="/tmp/", text=True)[1]
filename = tempfile.mkstemp(prefix="devkit-", dir="/tmp/", text=True)[1]
@@ -69,7 +69,7 @@ def writefile(data):
@@ -69,7 +69,7 @@ def writefile(data):
return filename
return filename
def write_key(post_body):
def write_key(post_body: bytes) -> str:
# Write key to temp file and return filename if valid, etc.
# Write key to temp file and return filename if valid, etc.
# Return None if invalid
# Return None if invalid
length = len(post_body)
length = len(post_body)
@@ -77,10 +77,10 @@ def write_key(post_body):
@@ -77,10 +77,10 @@ def write_key(post_body):
if length >= 64 * 1024:
if length >= 64 * 1024:
print("Key length too long")
print("Key length too long")
return None
return ''
if not post_body.decode().startswith('ssh-rsa '):
if not post_body.decode().startswith('ssh-rsa '):
print("Key doesn't start with ssh-rsa ")
print("Key doesn't start with ssh-rsa ")
return None
return ''
# Get to the base64 bits
# Get to the base64 bits
index = 8
index = 8
@@ -104,13 +104,13 @@ def write_key(post_body):
@@ -104,13 +104,13 @@ def write_key(post_body):
if (index < length) and (body_decoded[index] == ' '):
if (index < length) and (body_decoded[index] == ' '):
break
break
print("Found = but no space or = next, invalid key")
print("Found = but no space or = next, invalid key")
return None
return ''
if body_decoded[index] == ' ':
if body_decoded[index] == ' ':
break
break
print("Found invalid data, invalid key at "
print("Found invalid data, invalid key at "
f"index: {index} data: {body_decoded[index]}")
f"index: {index} data: {body_decoded[index]}")
return None
return ''
print(f"Key is valid base64, writing to temp file index: {index}")
print(f"Key is valid base64, writing to temp file index: {index}")
while index < length:
while index < length:
@@ -124,10 +124,10 @@ def write_key(post_body):
@@ -124,10 +124,10 @@ def write_key(post_body):
found_name = True
found_name = True
if body_decoded[index] == '\0':
if body_decoded[index] == '\0':
print("Found null terminator before expected")
print("Found null terminator before expected")
return None
return ''
if body_decoded[index] == '\n' and index != length - 1:
if body_decoded[index] == '\n' and index != length - 1:
print("Found newline before expected")
print("Found newline before expected")
return None
return ''
index = index + 1
index = index + 1
# write data to the file
# write data to the file
@@ -139,7 +139,7 @@ def write_key(post_body):
@@ -139,7 +139,7 @@ def write_key(post_body):
return filename
return filename
def find_hook(name: str) -> str or None:
def find_hook(name: str) -> str:
# First see if it exists in the given paths.
# First see if it exists in the given paths.
for path in HOOK_DIRS:
for path in HOOK_DIRS:
test_path = os.path.join(path, name)
test_path = os.path.join(path, name)
@@ -148,7 +148,7 @@ def find_hook(name: str) -> str or None:
@@ -148,7 +148,7 @@ def find_hook(name: str) -> str or None:
if not USE_DEFAULT_HOOKS:
if not USE_DEFAULT_HOOKS:
print(f"Error: Unable to find hook for {name} in hook directories\n")
print(f"Error: Unable to find hook for {name} in hook directories\n")
return None
return ''
test_path = f"/etc/{PACKAGE}/hooks/{name}"
test_path = f"/etc/{PACKAGE}/hooks/{name}"
if os.path.exists(test_path) and os.access(test_path, os.X_OK):
if os.path.exists(test_path) and os.access(test_path, os.X_OK):
@@ -167,7 +167,7 @@ def find_hook(name: str) -> str or None:
@@ -167,7 +167,7 @@ def find_hook(name: str) -> str or None:
return test_path
return test_path
print(f"Error:: Unable to find hook for {name} in /etc/{PACKAGE}/hooks or {DEVKIT_HOOKS_DIR}")
print(f"Error:: Unable to find hook for {name} in /etc/{PACKAGE}/hooks or {DEVKIT_HOOKS_DIR}")
return None
return ''
# Run devkit-1-identify hook to get hostname, otherwise use default platform.node()
# Run devkit-1-identify hook to get hostname, otherwise use default platform.node()
Loading