Skip to content

Commit

Permalink
Merge pull request #53 from cfengine/deps
Browse files Browse the repository at this point in the history
Fixed dependencies for modules added by URL
  • Loading branch information
olehermanse authored Oct 30, 2021
2 parents 44747e0 + 512aa7c commit e7651dc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
5 changes: 1 addition & 4 deletions JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ If you put your modules in a repo and don't have them in the index (yet), you ca
"type": "module",
"provides": {
"example-module": {
"name": "example-module",
"description": "Just an example",
"tags": ["local"],
"version": "1.0.0",
"dependencies": ["autorun"],
"steps": ["copy ./test.cf services/autorun/test.cf"]
}
Expand All @@ -165,8 +163,7 @@ cfbs init && cfbs add https://github.com/cfengine/some-repo
"name": "example-module",
"description": "Just an example",
"tags": ["local"],
"repo": "https://github.com/cfengine/some-repo",
"version": "1.0.0",
"url": "https://github.com/cfengine/some-repo",
"commit": "be3bc015f6a19e945bb7a9fa0ed78c97e2cecf61",
"dependencies": ["autorun"],
"steps": ["copy ./test.cf services/autorun/test.cf"],
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The modules you use can be written by the CFEngine team, others in the community

## Installation

Requires Python 3.6 or newer and `pip`.
Requires Python 3.5 or newer and `pip`.

```
pip install cfbs
Expand All @@ -30,12 +30,10 @@ pip install cf-remote

### Dependencies

`cfbs` is implemented in Python and has some dependencies on python version and libraries:

* Python 3.6 or newer

Additionally, some command line tools are required (not installed by pip):
`cfbs` is implemented in Python and has a few dependencies:

* Python 3.5 or newer
* `requests` python library
* `git` CLI installed and in PATH

## Usage
Expand Down
15 changes: 8 additions & 7 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,14 +501,14 @@ def _add_using_url(
checksum=None,
non_interactive=False,
):
url_repo_commit = None
url_commit = None
if url.endswith(_SUPPORTED_ARCHIVES):
config_path, url_repo_commit = _fetch_archive(url, checksum)
config_path, url_commit = _fetch_archive(url, checksum)
else:
assert url.startswith(("https://", "git://", "ssh://"))
config_path, url_repo_commit = _clone_url_repo(url)
config_path, url_commit = _clone_url_repo(url)

remote_config = CFBSConfig(path=config_path, url=url)
remote_config = CFBSConfig(path=config_path, url=url, url_commit=url_commit)
config = CFBSConfig(index_argument=index_path)

provides = remote_config.get_provides()
Expand All @@ -529,7 +529,7 @@ def _add_using_url(
modules = [provides[k] for k in to_add]

for module in modules:
config.add(module)
config.add(module, remote_config)

return 0

Expand Down Expand Up @@ -726,7 +726,7 @@ def get_download_path(module) -> str:
if not is_a_commit_hash(commit):
user_error("'%s' is not a commit reference" % commit)

url = module["repo"]
url = module.get("url") or module["repo"]
if url.endswith(_SUPPORTED_ARCHIVES):
url = os.path.dirname(url)
else:
Expand All @@ -753,7 +753,8 @@ def download_dependencies(prefer_offline=False, redownload=False):
if not is_a_commit_hash(commit):
user_error("'%s' is not a commit reference" % commit)

url = strip_right(module["repo"], ".git")
url = module.get("url") or module["repo"]
url = strip_right(url, ".git")
commit_dir = get_download_path(module)
if redownload:
rm(commit_dir, missing_ok=True)
Expand Down
75 changes: 58 additions & 17 deletions cfbs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ def get_modules(self) -> dict:
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 generate_index_for_local_module(module)
)
def get_build_step(self, name):
if name.startswith("./"):
return generate_index_for_local_module(name)

module = OrderedDict({"name": name})
module.update(self.get_modules()[name])
return module


def _expand_index(thing):
Expand All @@ -133,10 +134,34 @@ def _expand_index(thing):
return thing


def _construct_provided_module(name, data, url, url_commit):
module = OrderedDict()
module["name"] = name
module["description"] = data["description"]
module["url"] = url
module["commit"] = url_commit
subdirectory = data.get("subdirectory")
if subdirectory:
module["subdirectory"] = subdirectory
dependencies = data.get("dependencies")
if dependencies:
module["dependencies"] = dependencies
module["steps"] = data["steps"]
return module


class CFBSConfig:
def __init__(self, index_argument=None, path="./cfbs.json", data=None, url=None):
def __init__(
self,
index_argument=None,
path="./cfbs.json",
data=None,
url=None,
url_commit=None,
):
self.path = path
self.url = url
self.url_commit = url_commit
if data:
self._data = data
else:
Expand Down Expand Up @@ -164,22 +189,27 @@ def using_default_index(self):
return self._unexpanded_index == self._default_index

def __getitem__(self, key):
if key == "index":
return self.index
assert key != "index"
return self._data[key]

def __contains__(self, key):
return key in self._data

def get_provides(self):
print(pretty(self._data))
modules = OrderedDict()
for k, v in self._data["provides"].items():
module = OrderedDict()
module["name"] = k
module["description"] = v["description"]
module["provided_by"] = self.url
module["steps"] = v["steps"]
module = _construct_provided_module(k, v, self.url, self.url_commit)
modules[k] = module
return modules

def get_module_for_build(self, name, dependent):
if "provides" in self._data and name in self._data["provides"]:
module = self._data["provides"][name]
return _construct_provided_module(name, module, self.url, self.url_commit)
if name in self.index:
return self.index.get_build_step(name)
return None

def save(self, data=None):
if data:
if type(data) is CFBSConfig:
Expand All @@ -191,12 +221,23 @@ def save(self, data=None):
def _module_is_in_build(self, module):
return module["name"] in (m["name"] for m in self["build"])

def add(self, module):
def add(self, module, remote_config=None, dependent=None):
if type(module) is str:
module_str = module
module = (remote_config or self).get_module_for_build(module, dependent)
if not module:
user_error("Module '%s' not found" % module_str)
assert "name" in module
assert "steps" in module
if self._module_is_in_build(module):
print("Skipping already added module '%s'" % module["name"])
return
if "dependencies" in module:
for dep in module["dependencies"]:
self.add(dep, remote_config, module["name"])
self._data["build"].append(module)
self.save()
print("Added module: %s" % module["name"])
if dependent:
print("Added module: %s (Dependency of %s)" % (module["name"], dependent))
else:
print("Added module: %s" % module["name"])

0 comments on commit e7651dc

Please sign in to comment.