Skip to content

Commit

Permalink
Reformatted files with black
Browse files Browse the repository at this point in the history
  • Loading branch information
vpodzime committed Mar 21, 2022
1 parent 147fead commit 405c27f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 48 deletions.
4 changes: 3 additions & 1 deletion cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def validate_added_module(module):
def get_instance(cls, index=None):
if cls.instance is not None:
if index is not None:
raise RuntimeError("Instance of %s already exists, cannot specify index" % cls.__name__)
raise RuntimeError(
"Instance of %s already exists, cannot specify index" % cls.__name__
)
else:
cls.instance = cls(index)
return cls.instance
Expand Down
114 changes: 71 additions & 43 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
SUPPORTED_ARCHIVES,
)
from cfbs.index import _VERSION_INDEX
from cfbs.git import (is_git_repo,
git_get_config,
git_set_config,
git_init,
git_commit,
git_discard_changes_in_file,
CFBSGitError,
)
from cfbs.git import (
is_git_repo,
git_get_config,
git_set_config,
git_init,
git_commit,
git_discard_changes_in_file,
CFBSGitError,
)


_MODULES_URL = "https://archive.build.cfengine.com/modules"
Expand All @@ -70,15 +71,18 @@ def prompt_user(prompt, choices=None, default=None):

if config.non_interactive:
if default is None:
raise ValueError("Missing default value for prompt '%s' in non-interactive mode" % prompt)
raise ValueError(
"Missing default value for prompt '%s' in non-interactive mode" % prompt
)
else:
return default

prompt_separator = " " if prompt.endswith("?") else ": "
if choices:
assert default is None or str(default) in choices
choices_str = "/".join(choice.upper() if choice == str(default) else choice
for choice in choices)
choices_str = "/".join(
choice.upper() if choice == str(default) else choice for choice in choices
)
prompt += " [%s]%s" % (choices_str, prompt_separator)
elif default is not None:
prompt += " [%s]%s" % (default, prompt_separator)
Expand All @@ -100,11 +104,14 @@ def prompt_user(prompt, choices=None, default=None):
return answer


def with_git_commit(successful_returns, files_to_commit, commit_msg,
positional_args_lambdas=None, failed_return=False):

def with_git_commit(
successful_returns,
files_to_commit,
commit_msg,
positional_args_lambdas=None,
failed_return=False,
):
def decorator(fn):

def decorated_fn(*args, **kwargs):
ret = fn(*args, **kwargs)

Expand All @@ -114,14 +121,15 @@ def decorated_fn(*args, **kwargs):

if ret in successful_returns:
if positional_args_lambdas:
positional_args = (l_fn(args, kwargs) for l_fn in positional_args_lambdas)
positional_args = (
l_fn(args, kwargs) for l_fn in positional_args_lambdas
)
msg = commit_msg % tuple(positional_args)
else:
msg = commit_msg

try:
git_commit(msg, not config.non_interactive,
files_to_commit)
git_commit(msg, not config.non_interactive, files_to_commit)
except CFBSGitError as e:
print(str(e))
try:
Expand Down Expand Up @@ -209,7 +217,10 @@ def init_command(index_path=None, non_interactive=False) -> int:
user_error("Already initialized - look at %s" % cfbs_filename())

name = prompt_user("Please enter name of this CFBS repository", default="Example")
description = prompt_user("Please enter description of this CFBS repository", default="Example description")
description = prompt_user(
"Please enter description of this CFBS repository",
default="Example description",
)

definition = {
"name": name,
Expand All @@ -222,22 +233,31 @@ def init_command(index_path=None, non_interactive=False) -> int:

is_git = is_git_repo()
if is_git:
git_ans = prompt_user("This is a git repository. Do you want cfbs to make commits to it?",
choices=YES_NO_CHOICES, default="yes")
git_ans = prompt_user(
"This is a git repository. Do you want cfbs to make commits to it?",
choices=YES_NO_CHOICES,
default="yes",
)
else:
git_ans = prompt_user("Do you want cfbs to initialize a git repository and make commits to it?",
choices=YES_NO_CHOICES, default="yes")
git_ans = prompt_user(
"Do you want cfbs to initialize a git repository and make commits to it?",
choices=YES_NO_CHOICES,
default="yes",
)
do_git = git_ans in ("yes", "y")

if do_git:
user_name = git_get_config("user.name")
user_email = git_get_config("user.email")
user_name = prompt_user("Please enter user name to use for git commits",
default=user_name or "cfbs")
user_name = prompt_user(
"Please enter user name to use for git commits", default=user_name or "cfbs"
)

node_name = os.uname().nodename
user_email = prompt_user("Please enter user email to use for git commits",
default=user_email or ("cfbs@%s" % node_name))
user_email = prompt_user(
"Please enter user email to use for git commits",
default=user_email or ("cfbs@%s" % node_name),
)

if not is_git:
try:
Expand All @@ -246,8 +266,9 @@ def init_command(index_path=None, non_interactive=False) -> int:
print(str(e))
return 1
else:
if (not git_set_config("user.name", user_name) or
not git_set_config("user.email", user_email)):
if not git_set_config("user.name", user_name) or not git_set_config(
"user.email", user_email
):
print("Failed to set Git user name and email")
return 1

Expand All @@ -258,8 +279,11 @@ def init_command(index_path=None, non_interactive=False) -> int:

if do_git:
try:
git_commit("Initialized a new cfbs repository", not non_interactive,
[cfbs_filename()])
git_commit(
"Initialized a new cfbs repository",
not non_interactive,
[cfbs_filename()],
)
except CFBSGitError as e:
print(str(e))
os.unlink(cfbs_filename())
Expand Down Expand Up @@ -340,6 +364,7 @@ def search_command(terms: list) -> int:

return 0 if any(results) else 1


@commit_after_command("Added module%s %s", [PLURAL_S, FIRST_ARG_SLIST])
def add_command(
to_add: list,
Expand Down Expand Up @@ -380,9 +405,11 @@ def _get_modules_by_url(name) -> list:
if not matches:
user_error("Could not find module with URL '%s'" % name)
for module in matches:
answer = prompt_user("Do you wish to remove '%s'?" % module["name"],
choices=YES_NO_CHOICES,
default="yes")
answer = prompt_user(
"Do you wish to remove '%s'?" % module["name"],
choices=YES_NO_CHOICES,
default="yes",
)
if answer.lower() in ("yes", "y"):
print("Removing module '%s'" % module["name"])
modules.remove(module)
Expand Down Expand Up @@ -439,8 +466,9 @@ def _someone_needs_me(this) -> bool:
added_by = module["added_by"] if "added_by" in module else ""
print("%s - %s - added by: %s" % (name, description, added_by))

answer = prompt_user("Do you wish to remove these modules?",
choices=YES_NO_CHOICES, default="yes")
answer = prompt_user(
"Do you wish to remove these modules?", choices=YES_NO_CHOICES, default="yes"
)
if answer.lower() in ("yes", "y"):
for module in to_remove:
modules.remove(module)
Expand Down Expand Up @@ -510,13 +538,13 @@ def update_command():
# same commit => user modifications, don't revert them
if commit_differs:
ans = prompt_user(
"Module %s has different build steps now\n" % module["name"]
+ "old steps: %s\n" % module["steps"]
+ "new steps: %s\n" % index_info["steps"]
+ "Do you want to use the new build steps?",
choices=YES_NO_CHOICES,
default="yes"
)
"Module %s has different build steps now\n" % module["name"]
+ "old steps: %s\n" % module["steps"]
+ "new steps: %s\n" % index_info["steps"]
+ "Do you want to use the new build steps?",
choices=YES_NO_CHOICES,
default="yes",
)
if ans.lower() in ["y", "yes"]:
module["steps"] = index_info["steps"]
else:
Expand Down
15 changes: 11 additions & 4 deletions cfbs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import tempfile
from subprocess import check_call, check_output, run, PIPE, CalledProcessError


class CFBSGitError(Exception):
pass


def is_git_repo(path=None):
"""Is the given path a Git repository?)
Expand Down Expand Up @@ -44,9 +46,12 @@ def git_init(user_name=None, user_email=None, description=None):
`None`) or none can be given (both must be `None`).
"""
if ((user_name is None and user_email is not None) or
(user_name is not None and user_email is None)):
raise AttributeError("Both user_name and user_email must be given or none can be given")
if (user_name is None and user_email is not None) or (
user_name is not None and user_email is None
):
raise AttributeError(
"Both user_name and user_email must be given or none can be given"
)

if is_git_repo():
raise CFBSGitError("Already an initialized git repository")
Expand Down Expand Up @@ -120,4 +125,6 @@ def git_discard_changes_in_file(file_name):
try:
check_call(["git", "checkout", "--", file_name])
except CalledProcessError as cpe:
raise CFBSGitError("Failed to discard changes in file '%s'" % file_name) from cpe
raise CFBSGitError(
"Failed to discard changes in file '%s'" % file_name
) from cpe

0 comments on commit 405c27f

Please sign in to comment.