Skip to content

Commit

Permalink
Merge pull request #135 from larsewi/mpfv
Browse files Browse the repository at this point in the history
CFE-4071: Added --masterfiles option for cfbs init
  • Loading branch information
olehermanse authored Oct 26, 2022
2 parents cf42ece + 6f1c681 commit f4236b8
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 16 deletions.
3 changes: 3 additions & 0 deletions cfbs/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ def _get_arg_parser():
help="Ignore versions.json. Necessary in case of a custom index or testing changes to the default index.",
action="store_true",
)
parser.add_argument(
"--masterfiles", help="Add masterfiles on cfbs init choose between"
)
return parser
2 changes: 1 addition & 1 deletion cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def save(self):
f.write(data)

def longest_module_name(self) -> int:
return max((len(m["name"]) for m in self["build"]))
return max((len(m["name"]) for m in self["build"])) if self["build"] else 0

def add_with_dependencies(self, module, remote_config=None, dependent=None):
if type(module) is list:
Expand Down
59 changes: 45 additions & 14 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
git_set_config,
git_init,
CFBSGitError,
ls_remote,
)
from cfbs.git_magic import Result, commit_after_command, git_commit_maybe_prompt
from cfbs.prompts import YES_NO_CHOICES, prompt_user
Expand Down Expand Up @@ -123,7 +124,7 @@ def pretty_command(filenames: list, check: bool, keep_order: bool) -> int:
return 0


def init_command(index=None, non_interactive=False) -> int:
def init_command(index=None, masterfiles=None, non_interactive=False) -> int:
if is_cfbs_repo():
user_error("Already initialized - look at %s" % cfbs_filename())

Expand Down Expand Up @@ -228,22 +229,52 @@ def init_command(index=None, non_interactive=False) -> int:
"""
CFBSConfig.reload()

if prompt_user(
non_interactive,
"Do you wish to build on top of the default policy set, masterfiles? (Recommended)",
choices=YES_NO_CHOICES,
default="yes",
) in ("yes", "y"):
to_add = "masterfiles"
else:
to_add = prompt_user(
branch = None
to_add = ""
if masterfiles is None:
if prompt_user(
non_interactive,
"Specify policy set to use instead (empty to skip)",
default="",
)
"Do you wish to build on top of the default policy set, masterfiles? (Recommended)",
choices=YES_NO_CHOICES,
default="yes",
) in ("yes", "y"):
to_add = "masterfiles"
else:
to_add = prompt_user(
non_interactive,
"Specify policy set to use instead (empty to skip)",
default="",
)
elif re.match(r"[0-9]+(\.[0-9]+){2}(\-[0-9]+)?", masterfiles):
log.debug("--masterfiles=%s appears to be a version number" % masterfiles)
to_add = "masterfiles@%s" % masterfiles
elif masterfiles != "no":
"""This appears to be a branch. Thus we'll add masterfiles normally
and try to do the necessary modifications needed afterwards. I.e.
changing the 'repo' attribute to be 'url' and changing the commit to
be the current HEAD of the upstream branch."""

log.debug("--masterfiles=%s appears to be a branch" % masterfiles)
branch = masterfiles
to_add = "masterfiles"

if to_add:
return add_command([to_add])
ret = add_command([to_add])
if ret != 0:
return ret

if branch is not None:
config = CFBSConfig.get_instance()
module = config.get_module_from_build("masterfiles")
remote = module["repo"]
commit = ls_remote(remote, branch)
if commit is None:
user_error("Failed to add masterfiles from branch %s" % branch)
log.debug("Current commit for masterfiles branch %s is %s" % (branch, commit))
module["url"] = remote
del module["repo"]
module["commit"] = commit
config.save()

return 0

Expand Down
18 changes: 18 additions & 0 deletions cfbs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ class CFBSGitError(Exception):
pass


def ls_remote(remote, branch):
"""Returns the hash of the commit that the current HEAD of a given branch
on a given remote is pointing to.
:param remote: the remote to list
:param branch: the branch on the remote
"""
try:
return (
check_output(["git", "ls-remote", remote, branch])
.decode()
.strip()
.split()[0]
)
except:
return None


def is_git_repo(path=None):
"""Is the given path a Git repository?)
Expand Down
9 changes: 8 additions & 1 deletion cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def main() -> int:
print("")
user_error("No command given")

if args.masterfiles and args.command != "init":
user_error(
"The option --masterfiles is only for cfbs init, not cfbs %s" % args.command
)

if args.non_interactive and args.command not in (
"init",
"add",
Expand Down Expand Up @@ -75,7 +80,9 @@ def main() -> int:

if args.command == "init":
return commands.init_command(
index=args.index, non_interactive=args.non_interactive
index=args.index,
masterfiles=args.masterfiles,
non_interactive=args.non_interactive,
)

if args.command == "search":
Expand Down
11 changes: 11 additions & 0 deletions tests/shell/026_init_no_masterfiles.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
touch cfbs.json && rm cfbs.json
rm -rf .git

cfbs --non-interactive init --masterfiles=no
!( grep '"name": "masterfiles"' cfbs.json )
cfbs build
12 changes: 12 additions & 0 deletions tests/shell/027_init_masterfiles_version_master.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
touch cfbs.json && rm cfbs.json
rm -rf .git

cfbs --non-interactive init --masterfiles=master
grep '"name": "masterfiles"' cfbs.json
grep '"url": "https://github.com/cfengine/masterfiles"' cfbs.json
cfbs build
12 changes: 12 additions & 0 deletions tests/shell/028_init_masterfiles_version_3.18.2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
touch cfbs.json && rm cfbs.json
rm -rf .git

cfbs --non-interactive init --masterfiles=3.18.2
grep '"name": "masterfiles"' cfbs.json
grep '"version": "3.18.2"' cfbs.json
cfbs build
12 changes: 12 additions & 0 deletions tests/shell/029_init_masterfiles_version_3.18.1-1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
touch cfbs.json && rm cfbs.json
rm -rf .git

cfbs --non-interactive init --masterfiles=3.18.1-1
grep '"name": "masterfiles"' cfbs.json
grep '"version": "3.18.1-1"' cfbs.json
cfbs build
4 changes: 4 additions & 0 deletions tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ bash tests/shell/022_update_input_fail_variable.sh
bash tests/shell/023_update_input_fail_number.sh
bash tests/shell/024_update_input_fail_bundle.sh
bash tests/shell/025_add_input_remove.sh
bash tests/shell/026_init_no_masterfiles.sh
bash tests/shell/027_init_masterfiles_version_master.sh
bash tests/shell/028_init_masterfiles_version_3.18.2.sh
bash tests/shell/029_init_masterfiles_version_3.18.1-1.sh

echo "All cfbs shell tests completed successfully!"
9 changes: 9 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from cfbs.git import ls_remote
from cfbs.utils import is_a_commit_hash


def test_ls_remote():
commit = ls_remote("https://github.com/cfengine/masterfiles.git", "master")
print(commit)
assert commit != None
assert is_a_commit_hash(commit)

0 comments on commit f4236b8

Please sign in to comment.