-
Notifications
You must be signed in to change notification settings - Fork 11
Added: manifest, directory, binary support #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,19 @@ def check_version(host, project, auth=None, timeout=5) -> (bool, str): | |||||
| print(f'Something went wrong: {ex}') | ||||||
| return False, current_version | ||||||
|
|
||||||
|
|
||||||
| def fetch_manifest(host, project, remote_version, prefix_or_path_separator, auth=None, timeout=5): | ||||||
| if auth: | ||||||
| response = urequests.get(f'{host}/{project}/{remote_version}{prefix_or_path_separator}manifest', headers={'Authorization': f'Basic {auth}'}, timeout=timeout) | ||||||
| else: | ||||||
| response = urequests.get(f'{host}/{project}/{remote_version}{prefix_or_path_separator}manifest', timeout=timeout) | ||||||
| response_status_code = response.status_code | ||||||
| response_text = response.text | ||||||
| response.close() | ||||||
| if response_status_code != 200: | ||||||
| print(f'Remote manifest file {host}/{project}/{remote_version}{prefix_or_path_separator}manifest not found') | ||||||
| raise Exception(f"Missing manifest for {remote_version}") | ||||||
| return response_text.split() | ||||||
|
|
||||||
| def generate_auth(user=None, passwd=None) -> str | None: | ||||||
| if not user and not passwd: | ||||||
| return None | ||||||
|
|
@@ -37,7 +49,7 @@ def generate_auth(user=None, passwd=None) -> str | None: | |||||
| return auth_bytes.decode().strip() | ||||||
|
|
||||||
|
|
||||||
| def ota_update(host, project, filenames, use_version_prefix=True, user=None, passwd=None, hard_reset_device=True, soft_reset_device=False, timeout=5) -> None: | ||||||
| def ota_update(host, project, filenames=None, use_version_prefix=True, user=None, passwd=None, hard_reset_device=True, soft_reset_device=False, timeout=5) -> None: | ||||||
| all_files_found = True | ||||||
| auth = generate_auth(user, passwd) | ||||||
| prefix_or_path_separator = '_' if use_version_prefix else '/' | ||||||
|
|
@@ -46,28 +58,58 @@ def ota_update(host, project, filenames, use_version_prefix=True, user=None, pas | |||||
| if version_changed: | ||||||
| try: | ||||||
| uos.mkdir('tmp') | ||||||
| except: | ||||||
| pass | ||||||
| except OSError as e: | ||||||
| if e.errno != 17: | ||||||
| raise | ||||||
| if filenames is None: | ||||||
| filenames = fetch_manifest(host, project, remote_version, prefix_or_path_separator, auth=auth, timeout=timeout) | ||||||
| for filename in filenames: | ||||||
| if filename.endswith('/'): | ||||||
| dir_path="tmp" | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could define |
||||||
| for dir in filename.split('/'): | ||||||
| if len(dir) > 0: | ||||||
| built_path=f"{dir_path}/{dir}" | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| try: | ||||||
| uos.mkdir(built_path) | ||||||
| except OSError as e: | ||||||
| if e.errno != 17: | ||||||
| raise | ||||||
| continue | ||||||
| if auth: | ||||||
| response = urequests.get(f'{host}/{project}/{remote_version}{prefix_or_path_separator}{filename}', headers={'Authorization': f'Basic {auth}'}, timeout=timeout) | ||||||
| else: | ||||||
| response = urequests.get(f'{host}/{project}/{remote_version}{prefix_or_path_separator}{filename}', timeout=timeout) | ||||||
| response_status_code = response.status_code | ||||||
| response_text = response.text | ||||||
| response_content = response.content | ||||||
| response.close() | ||||||
| if response_status_code != 200: | ||||||
| print(f'Remote source file {host}/{project}/{remote_version}{prefix_or_path_separator}{filename} not found') | ||||||
| all_files_found = False | ||||||
| continue | ||||||
| with open(f'tmp/{filename}', 'w') as source_file: | ||||||
| source_file.write(response_text) | ||||||
| with open(f'tmp/{filename}', 'wb') as source_file: | ||||||
| source_file.write(response_content) | ||||||
| if all_files_found: | ||||||
| dirs=[] | ||||||
| for filename in filenames: | ||||||
| with open(f'tmp/{filename}', 'r') as source_file, open(filename, 'w') as target_file: | ||||||
| if filename.endswith('/'): | ||||||
| dir_path="" | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| for dir in filename.split('/'): | ||||||
| if len(dir) > 0: | ||||||
| built_path=f"{dir_path}/{dir}" | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| try: | ||||||
| uos.mkdir(built_path) | ||||||
| except OSError as e: | ||||||
| if e.errno != 17: | ||||||
| raise | ||||||
| dirs.append(f"tmp/{built_path}") | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| continue | ||||||
| #print(f"tmp/{filename} -> {filename}") | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover? |
||||||
| with open(f'tmp/{filename}', 'rb') as source_file, open(filename, 'wb') as target_file: | ||||||
| target_file.write(source_file.read()) | ||||||
| uos.remove(f'tmp/{filename}') | ||||||
| try: | ||||||
| while len(dirs) > 0: | ||||||
| uos.rmdir(dirs.pop()) | ||||||
| uos.rmdir('tmp') | ||||||
| except: | ||||||
| pass | ||||||
|
|
@@ -81,6 +123,7 @@ def ota_update(host, project, filenames, use_version_prefix=True, user=None, pas | |||||
| machine.reset() | ||||||
| except Exception as ex: | ||||||
| print(f'Something went wrong: {ex}') | ||||||
| raise ex | ||||||
|
|
||||||
|
|
||||||
| def check_for_ota_update(host, project, user=None, passwd=None, timeout=5, soft_reset_device=False): | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.