-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from cfengine/index
CFE-3762: Added options to specify alternate index
- Loading branch information
Showing
3 changed files
with
109 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import sys | ||
|
||
from cf_remote.paths import cfengine_dir | ||
|
||
from cfbs.utils import ( | ||
user_error, | ||
get_json, | ||
strip_left, | ||
strip_right, | ||
pad_right, | ||
write_json, | ||
read_json, | ||
merge_json, | ||
mkdir, | ||
touch, | ||
rm, | ||
cp, | ||
sh, | ||
) | ||
|
||
from cfbs.pretty import pretty_file, pretty | ||
|
||
|
||
class Index: | ||
def __init__(self, path): | ||
self.path = path | ||
if not self.path: | ||
self.path = "https://raw.githubusercontent.com/cfengine/cfbs-index/master/index.json" | ||
self._data = None | ||
|
||
def __contains__(self, key): | ||
return key in self.get_modules() | ||
|
||
def __getitem__(self, key): | ||
return self.get_modules()[key] | ||
|
||
def _cache_path(self) -> str: | ||
return cfbs_dir("index.json") | ||
|
||
def _get(self) -> dict: | ||
path = self.path | ||
if path.startswith("https://"): | ||
index = get_json(path) | ||
if not index: | ||
index = read_json(self._cache_path()) | ||
if index: | ||
print("Warning: Downloading index failed, using cache") | ||
else: | ||
if not os.path.isfile(path): | ||
sys.exit(f"Index does not exist at: '{path}'") | ||
index = read_json(path) | ||
if not index: | ||
sys.exit("Could not download or find module index") | ||
if "modules" not in index: | ||
sys.exit("Empty or invalid module index") | ||
return index | ||
|
||
def get(self) -> dict: | ||
if not self._data: | ||
self._data = self._get() | ||
return self._data | ||
|
||
def get_modules(self) -> dict: | ||
return self.get()["modules"] | ||
|
||
def exists(self, module): | ||
return os.path.exists(module) or (module in self) | ||
|
||
def get_build_step(self, module): | ||
return ( | ||
self.get_modules()[module] | ||
if not module.startswith("./") | ||
else local_module_data(module) | ||
) |
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