-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply review suggestions, reduce amount downloaded
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
121 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,99 @@ | ||
from pathlib import Path | ||
import os | ||
import shutil | ||
import urllib.request | ||
|
||
from cfbs.utils import get_json | ||
|
||
DOWNLOAD = True | ||
from cfbs.utils import fetch_url, get_json, mkdir | ||
|
||
ENTERPRISE_URL = "https://cfengine.com/release-data/enterprise/releases.json" | ||
COMMUNITY_URL = "https://cfengine.com/release-data/community/releases.json" | ||
|
||
# TODO | ||
# def download_all_versions_community(): | ||
# data = get_json(COMMUNITY_URL) | ||
# # "masterfiles is at a different index" in 3.10.1 happens only for Enterprise, not Community | ||
|
||
ENTERPRISE_DOWNLOAD_PATH = "enterprise" | ||
|
||
def download_all_versions_enterprise(): | ||
data = get_json(ENTERPRISE_URL) | ||
|
||
urls_dict = {} | ||
def get_download_urls_enterprise(): | ||
download_urls = {} | ||
reported_checksums = {} | ||
|
||
for releases_data in data["releases"]: | ||
version = releases_data["version"] | ||
release_url = releases_data["URL"] | ||
data = get_json(ENTERPRISE_URL) | ||
|
||
subdata = get_json(release_url) | ||
for release_data in data["releases"]: | ||
version = release_data["version"] | ||
|
||
if version == "3.10.0": | ||
# for 3.10.0, for some reason, the masterfiles download link points to the .tar.gz tarball, rather than the .pkg.tar.gz tarball | ||
# download the .pkg.tar.gz from an unlisted analoguous URL instead | ||
download_url = "https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-masterfiles-3.10.0.pkg.tar.gz" | ||
download_urls[version] = download_url | ||
continue | ||
if version == "3.9.2": | ||
# for 3.9.2, no masterfiles are listed, but an unlisted analoguous URL exists | ||
download_url = "https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-masterfiles-3.9.2.pkg.tar.gz" | ||
download_urls[version] = download_url | ||
continue | ||
|
||
release_url = release_data["URL"] | ||
subdata = get_json(release_url) | ||
artifacts_data = subdata["artifacts"] | ||
|
||
if "Additional Assets" not in artifacts_data: | ||
# happens for 3.9.0b1, 3.8.0b1, 3.6.1, 3.6.0 | ||
download_url = None | ||
continue | ||
|
||
else: | ||
# for 3.10.0, for some reason, the masterfiles download link points to the .tar.gz tarball, rather than the .pkg.tar.gz tarball | ||
# here, download the .pkg.tar.gz from a hidden analoguous URL instead | ||
if version == "3.10.0": | ||
download_url = "https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-masterfiles-3.10.0.pkg.tar.gz" | ||
else: | ||
# there's precisely one version (3.10.1) for which masterfiles is at a different index | ||
if version == "3.10.1": | ||
artifacts_data = artifacts_data["Additional Assets"][1] | ||
else: | ||
artifacts_data = artifacts_data["Additional Assets"][0] | ||
|
||
if artifacts_data["Title"] != "Masterfiles ready-to-install tarball": | ||
# happens for 3.10.1, 3.9.2, 3.9.0, 3.8.2, 3.8.1, 3.8.0, 3.6.2--3.7.4 | ||
# 3.10.1: see above | ||
# 3.9.2: no masterfiles listed, but an analogous hidden URL exists | ||
# 3.9.0 and others: no masterfiles listed, and analogous hidden URLs seemingly do not exist | ||
if version == "3.9.2": | ||
download_url = "https://cfengine-package-repos.s3.amazonaws.com/tarballs/cfengine-masterfiles-3.9.2.pkg.tar.gz" | ||
else: | ||
download_url = None | ||
else: | ||
download_url = artifacts_data["URL"] | ||
reported_checksums[version] = artifacts_data["SHA256"] | ||
|
||
if download_url is not None: | ||
urls_dict[version] = download_url | ||
assets_data = artifacts_data["Additional Assets"] | ||
masterfiles_data = None | ||
|
||
for asset in assets_data: | ||
if asset["Title"] == "Masterfiles ready-to-install tarball": | ||
masterfiles_data = asset | ||
|
||
if masterfiles_data is None: | ||
# happens for 3.9.2, 3.9.0, 3.8.2, 3.8.1, 3.8.0, 3.7.4--3.6.2 | ||
# 3.9.2: see above | ||
# 3.9.0 and below: no masterfiles listed, and analogous unlisted URLs seemingly do not exist | ||
continue | ||
|
||
download_urls[version] = masterfiles_data["URL"] | ||
reported_checksums[version] = masterfiles_data["SHA256"] | ||
|
||
return download_urls, reported_checksums | ||
|
||
|
||
def download_versions_from_urls(output_path, download_urls): | ||
downloaded_versions = [] | ||
if DOWNLOAD: | ||
root_path = Path("./enterprise") | ||
Path.mkdir(root_path, exist_ok=True) | ||
|
||
for version, url in urls_dict.items(): | ||
# ignore master and .x versions | ||
if url.startswith("http://buildcache"): | ||
continue | ||
mkdir(output_path) | ||
|
||
for version, url in download_urls.items(): | ||
# ignore master and .x versions | ||
if url.startswith("http://buildcache"): | ||
continue | ||
|
||
print("Downloading from", url) | ||
downloaded_versions.append(version) | ||
|
||
version_path = os.path.join(output_path, version) | ||
mkdir(version_path) | ||
|
||
filename = url.split("/")[-1] | ||
tarball_path = os.path.join(version_path, filename) | ||
fetch_url(url, tarball_path) | ||
|
||
downloaded_versions.append(version) | ||
print(url) | ||
tarball_dir_path = os.path.join(version_path, "tarball") | ||
shutil.unpack_archive(tarball_path, tarball_dir_path) | ||
|
||
version_path = root_path / version | ||
Path.mkdir(version_path, exist_ok=True) | ||
return output_path, downloaded_versions | ||
|
||
filename = url.split("/")[-1] | ||
tarball_path = version_path / filename | ||
urllib.request.urlretrieve(url, tarball_path) | ||
|
||
shutil.unpack_archive(tarball_path, version_path / "tarball") | ||
# TODO | ||
# def download_all_versions_community(): | ||
# data = get_json(COMMUNITY_URL) | ||
|
||
|
||
def download_all_versions_enterprise(): | ||
download_urls, reported_checksums = get_download_urls_enterprise() | ||
|
||
output_path, downloaded_versions = download_versions_from_urls( | ||
ENTERPRISE_DOWNLOAD_PATH, download_urls | ||
) | ||
|
||
# for local verification of the reported (Enterprise) (.pkg.tar.gz) checksums | ||
return downloaded_versions, reported_checksums | ||
return output_path, downloaded_versions, reported_checksums |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters