Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cfbs/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
fetch_url,
file_sha256,
get_json,
get_or_read_json,
immediate_subdirectories,
mkdir,
read_json,
user_error,
)

Expand Down Expand Up @@ -131,7 +131,12 @@ def mpf_vcf_dicts(offline=False):
"https://api.github.com/repos/" + REPO_OWNERNAME + "/releases/latest"
)

latest_release_data = get_json(LATEST_RELEASE_API_URL)
try:
latest_release_data = get_json(LATEST_RELEASE_API_URL)
except FetchError as e:
user_error(
"Downloading CFEngine release information failed - check your Wi-Fi / network settings."
)

latest_release_name = latest_release_data["name"]
ri_archive_url = REPO_URL + "/archive/refs/tags/" + latest_release_name + ".zip"
Expand Down Expand Up @@ -169,13 +174,14 @@ def mpf_vcf_dicts(offline=False):
mpf_checkfiles_json_path = os.path.join(mpf_vcf_path, "checksums.json")
mpf_files_json_path = os.path.join(mpf_vcf_path, "files.json")

mpf_versions_dict = get_or_read_json(mpf_versions_json_path)
mpf_versions_dict = read_json(mpf_versions_json_path)

mpf_versions_dict = mpf_versions_dict["versions"]

mpf_checksums_dict = get_or_read_json(mpf_checkfiles_json_path)
mpf_checksums_dict = read_json(mpf_checkfiles_json_path)
mpf_checksums_dict = mpf_checksums_dict["checksums"]

mpf_files_dict = get_or_read_json(mpf_files_json_path)
mpf_files_dict = read_json(mpf_files_json_path)
mpf_files_dict = mpf_files_dict["files"]

return mpf_versions_dict, mpf_checksums_dict, mpf_files_dict
Expand Down
8 changes: 7 additions & 1 deletion cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from cfbs.args import get_args

from cfbs.utils import (
FetchError,
cfbs_dir,
cfbs_filename,
is_cfbs_repo,
Expand Down Expand Up @@ -905,7 +906,12 @@ def _download_dependencies(
sh("git clone %s %s" % (url, commit_dir))
sh("(cd %s && git checkout %s)" % (commit_dir, commit))
else:
versions = get_json(_VERSION_INDEX)
try:
versions = get_json(_VERSION_INDEX)
except FetchError as e:
user_error(
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
)
try:
checksum = versions[name][module["version"]]["archive_sha256"]
except KeyError:
Expand Down
25 changes: 21 additions & 4 deletions cfbs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import OrderedDict

from cfbs.module import Module
from cfbs.utils import get_or_read_json, user_error, get_json
from cfbs.utils import FetchError, get_or_read_json, user_error, get_json
from cfbs.internal_file_management import local_module_name

_DEFAULT_INDEX = (
Expand Down Expand Up @@ -87,7 +87,13 @@ def _expand_index(self):

assert type(index) is str

self._data = get_or_read_json(index)
try:
self._data = get_or_read_json(index)
except FetchError as e:
user_error(
"Downloading index '%s' failed - check your Wi-Fi / network settings."
% index
)

if not self._data:
sys.exit("Could not download or find module index")
Expand Down Expand Up @@ -121,7 +127,13 @@ def exists(self, module):
return True
if not version:
return name in self
versions = get_json(_VERSION_INDEX)
try:
versions = get_json(_VERSION_INDEX)
except FetchError as e:
user_error(
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
)

return name in versions and version in versions[name]

def check_existence(self, modules: list):
Expand Down Expand Up @@ -162,7 +174,12 @@ def get_module_object(self, module, added_by=None):
else:
object = self[name]
if version:
versions = get_json(_VERSION_INDEX)
try:
versions = get_json(_VERSION_INDEX)
except FetchError as e:
user_error(
"Downloading CFEngine Build Module Index failed - check your Wi-Fi / network settings."
)
new_values = versions[name][version]
specifics = {
k: v for (k, v) in new_values.items() if k in Module.attributes()
Expand Down
15 changes: 13 additions & 2 deletions cfbs/masterfiles/download_all_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ def get_download_urls_enterprise(min_version=None):

print("* gathering download URLs...")

data = get_json(ENTERPRISE_RELEASES_URL)
try:
data = get_json(ENTERPRISE_RELEASES_URL)
except FetchError as e:
user_error(
"Downloading CFEngine release data failed - check your Wi-Fi / network settings."
)

for release_data in data["releases"]:
version = release_data["version"]
Expand All @@ -38,7 +43,13 @@ def get_download_urls_enterprise(min_version=None):
continue

release_url = release_data["URL"]
subdata = get_json(release_url)
try:
subdata = get_json(release_url)
except FetchError as e:
user_error(
"Downloading CFEngine release data for version %s failed - check your Wi-Fi / network settings."
% version
)
artifacts_data = subdata["artifacts"]

if "Additional Assets" not in artifacts_data:
Expand Down
9 changes: 6 additions & 3 deletions cfbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def user_error(msg: str):


def get_json(url: str) -> OrderedDict:
with urllib.request.urlopen(url) as r:
assert r.status >= 200 and r.status < 300
return json.loads(r.read().decode(), object_pairs_hook=OrderedDict)
try:
with urllib.request.urlopen(url) as r:
assert r.status >= 200 and r.status < 300
return json.loads(r.read().decode(), object_pairs_hook=OrderedDict)
except urllib.error.URLError as e:
raise FetchError("Failed to get JSON from '%s'" % url) from e


def get_or_read_json(path: str) -> OrderedDict:
Expand Down