-
Notifications
You must be signed in to change notification settings - Fork 9
ENT-13808: Improve release history generation and download handling #51
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import json | ||
| import requests | ||
| from cfengine_cli.masterfiles.download import ( | ||
| ENTERPRISE_RELEASES_URL, | ||
| COMMUNITY_RELEASES_URL, | ||
|
|
@@ -29,6 +30,10 @@ def generate_release_information_impl( | |
| omit_download=False, check=False, min_version=None | ||
| ): | ||
| if not omit_download: | ||
| generate_release_history() | ||
|
|
||
| generate_git_tags_map() | ||
|
|
||
| print("Downloading masterfiles...") | ||
|
|
||
| downloaded_versions = download_all_versions(DOWNLOAD_PATH, min_version) | ||
|
|
@@ -49,10 +54,6 @@ def generate_release_information_impl( | |
| ) | ||
| generate_vcf_download(DOWNLOAD_PATH, downloaded_versions) | ||
|
|
||
| generate_release_history() | ||
|
|
||
| generate_git_tags_map() | ||
|
|
||
| if check: | ||
| print( | ||
| "Downloading releases of masterfiles from git (github.com) and generating " | ||
|
|
@@ -77,43 +78,37 @@ def generate_release_information_impl( | |
| def download_enterprise_releasedata(): | ||
| # Downloading releases.json: | ||
| try: | ||
| releases_data = get_json(ENTERPRISE_RELEASES_URL) | ||
| return requests.get(ENTERPRISE_RELEASES_URL) | ||
|
Member
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. Here you change the return type from json / dict to a |
||
|
|
||
| except CFBSNetworkError: | ||
| except requests.RequestException: | ||
| raise CFBSExitError( | ||
| "Downloading CFEngine release data failed - check your Wi-Fi / network settings." | ||
| ) | ||
|
|
||
| return releases_data | ||
|
|
||
|
|
||
| def download_community_releasedata(): | ||
| # Downloading community/releases.json | ||
| try: | ||
| releases_data = get_json(COMMUNITY_RELEASES_URL) | ||
|
|
||
| except CFBSNetworkError: | ||
| return requests.get(COMMUNITY_RELEASES_URL) | ||
| except requests.RequestException: | ||
| raise CFBSExitError( | ||
| "Downloading CFEngine release data failed - check your Wi-Fi / network settings." | ||
| ) | ||
|
|
||
| return releases_data | ||
|
|
||
|
|
||
| def process_release_type(folder, download_func): | ||
| # Function for processing either community or enterprise releases | ||
|
Comment on lines
99
to
100
Member
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. This function was here already, but looking at it, I wonder if it unnecessarily complex. Why is it necessary to pass in a download function, instead of just a download URL? |
||
| release_data = download_func() | ||
|
|
||
| write_json_pretty(f"./{folder}/releases.json", release_data) | ||
| releases_path = f"./{folder}/releases.json" | ||
| print(f"\nDownloading release data for {folder}...") | ||
| response = download_func() | ||
| # Save raw file to preserve exact format from website | ||
| save_to_file(releases_path, response.content) | ||
|
|
||
| release_data = load_json_from_file(releases_path) | ||
| stable_releases = get_stable_releases(release_data) | ||
|
|
||
| file_checksums_dict = build_release_history(stable_releases) | ||
|
|
||
| write_version_files(stable_releases, folder) | ||
|
|
||
| sorted_releases = sort_release_data(file_checksums_dict) | ||
|
|
||
| write_json(f"./{folder}/checksums.json", sorted_releases) | ||
|
Comment on lines
108
to
112
Member
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. These formatting changes should not be in the same commit. |
||
|
|
||
|
|
||
|
|
@@ -188,7 +183,21 @@ def write_version_files(stable_releases, folder): | |
| write_json(f"./{folder}/versions/{version}.json", version_data) | ||
|
|
||
|
|
||
| def write_json_pretty(path, data): | ||
| # Writes release information in same format as on cfengine.com | ||
| with open(path, "w", encoding="utf-8") as f: | ||
| json.dump(data, f, indent=2, ensure_ascii=False) | ||
| def save_to_file(path, content): | ||
| try: | ||
| with open(path, "wb") as f: | ||
| f.write(content) | ||
| print(f"Saved {path}") | ||
|
|
||
| except OSError: | ||
| raise CFBSExitError(f"Failed to write file {path}.") | ||
|
|
||
|
|
||
| def load_json_from_file(path): | ||
| # Open saved file and return as dict | ||
| try: | ||
| with open(path, "r", encoding="utf-8") as f: | ||
| return json.load(f) | ||
|
|
||
| except OSError: | ||
| raise CFBSExitError(f"Failed to read file {path}.") | ||
|
Comment on lines
+186
to
+203
Member
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. I was thinking we could make a function which does "everything" you need, without saving the data to file and then reading it from file again; def json_get_save_return(url: str, path: str) -> dict:
r = requests.get(url)
content = r.content
with open(path, "wb") as f:
f.write(content)
# TODO: Add some error handling
data = json.loads(content)
return data |
||
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.
You also need to remove it from the end, now it runs twice :)
Also, the commit message should mention why you're doing this. That's almost always more important than restating what you did unless it's really obvious.
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.
You did the removal part in the wrong commit (2nd commit).