Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue removal duplicate code #48

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from 1 commit
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
180 changes: 94 additions & 86 deletions cubi-tools/prototypes/update_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ def main():
branch = args.branch
external = args.external


# The location of the 'template-metadata-files" folder holding branch/version tag
# needs to be parallel the project directory
metadata_branch = pathlib.Path(
pathlib.Path(f"{working_dir}").resolve().parents[0],
"template-metadata-files",
).resolve()


# detect if its a external workflow
if external:
metadata_target = define_metadata_target(working_dir, external, dryrun)
Expand Down Expand Up @@ -194,6 +192,80 @@ def clone(metadata_target, working_dir, ref_repo, branch, metadata_branch, dryru
return None


def git_pull_template(metadata_branch, branch):
"""
This function will pull updates from the remote template repository.
"""
command = [
"git",
"pull",
"--all",
"-q",
]
sp.run(
command,
cwd=metadata_branch,
check=False,
)
command_checkout = ["git", "checkout", "".join({branch}), "-q"]
checkout_cmd = sp.run(
command_checkout,
cwd=metadata_branch,
stderr=sp.PIPE,
check=False,
)
# If the 'template-metadata-files' folder is not a Git repo
# an error message that contains the string 'fatal:' will be thrown
warning = "fatal:"
assert warning not in str(checkout_cmd.stderr.strip()), (
svenwillger marked this conversation as resolved.
Show resolved Hide resolved
"The folder 'template-metadata-files' is not a git repository! "
"For this script to work either delete the folder or move it!!"
)
# If you try to clone a repo/branch/tag that doesn't exist
# Git will throw an error message that contains the string 'error:'
error = "error:"
assert error not in str(
svenwillger marked this conversation as resolved.
Show resolved Hide resolved
checkout_cmd.stderr.strip()
), f"The branch or version tag named '{branch}' doesn't exist"
return None


def git_clone_template(ref_repo, metadata_branch, metadata_target, branch):
"""
This function will clone the template repository into a folder parallel
to the folder to get updated.
"""
command = [
"git",
"clone",
"-q",
"-c advice.detachedHead=false",
ref_repo,
metadata_branch,
]
clone_cmd = sp.run(
command,
stdout=sp.PIPE,
stderr=sp.PIPE,
cwd=metadata_target,
check=False,
)
# If the 'template-metadata-files' folder is not a Git repo
# an error message that contains the string 'fatal:' will be thrown
warning = "fatal:"
assert warning not in str(clone_cmd.stderr.strip()), (
"The repository you entered or the branch or version tag "
f"named '{branch}' doesn't exist"
)
command_checkout = ["git", "checkout", "".join({branch}), "-q"]
sp.run(
command_checkout,
cwd=metadata_branch,
check=False,
)
return None


def calculate_md5_checksum(file_path):
"""
The MD5 checksum for the metadata files of the local folder or
Expand All @@ -214,8 +286,12 @@ def calculate_md5_checksum(file_path):
md5_hash = ""
return md5_hash


def update_file(metadata_target, metadata_branch, file_to_update, dryrun):
"""_summary_
"""
The MD5 checksum of the the metadata_target file(s) and the metadata_branch
file(s) are being compared. If they differ a question to update for each
different workflow file pops up. If an update is requested it will be performed.

Args:
metadata_target (pathlib.Path):
Expand Down Expand Up @@ -243,9 +319,10 @@ def update_file(metadata_target, metadata_branch, file_to_update, dryrun):
answer_is_pos = user_response(f"Update '{file_to_update}'")

if answer_is_pos:
shutil.copyfile(metadata_branch.joinpath(file_to_update),
metadata_target.joinpath(file_to_update)
)
shutil.copyfile(
metadata_branch.joinpath(file_to_update),
metadata_target.joinpath(file_to_update),
)
print(f"'{file_to_update}' was updated!")
else:
print(f"'{file_to_update}' was NOT updated!")
Expand Down Expand Up @@ -289,9 +366,11 @@ def update_pyproject_toml(metadata_target, metadata_branch, branch, dryrun):
metadata_version = get_metadata_versions(metadata_branch, metadata_target)
# Just to clearly state which information/files are generated by the
# function 'get_metadata_versions(metadata_branch, metadata_target)':
branch_version = metadata_version[0] # Metadata version of the branch (str)
target_version = metadata_version[1] # Metadata version of the target (str)
target_pyproject = metadata_version[2] # Target pyproject toml w/ updated metadata version (dict)
branch_version = metadata_version[0] # Metadata version of the branch (str)
target_version = metadata_version[1] # Metadata version of the target (str)
target_pyproject = metadata_version[
2
] # Target pyproject toml w/ updated metadata version (dict)

if branch_version != target_version:
if dryrun:
Expand Down Expand Up @@ -335,7 +414,6 @@ def update_pyproject_toml(metadata_target, metadata_branch, branch, dryrun):
return None



def user_response(question, attempt=0):
"""
Function to evaluate the user response to the Yes or No question refarding updating
Expand All @@ -346,19 +424,22 @@ def user_response(question, attempt=0):
answer = input(prompt).strip().lower()
pos = ["yes", "y", "yay"]
neg = ["no", "n", "nay"]
if attempt == 2:
print("YOU HAVE ONE LAST CHANCE TO ANSWER THIS (y/n) QUESTION!")
if attempt >= 3:
raise RuntimeError(
"You failed at least 3 times to answer a simple (y/n) question!"
"I warned you! You failed 3 times to answer a simple (y/n)"
" question! Please start over!"
)

if not (answer in pos or answer in neg):
print(f"That was a yes or no question, but you answered: {answer}")
return user_response(question, attempt)
return answer in pos


def get_metadata_versions(metadata_branch, metadata_target):
"""Read the metadata version strings in the respective
"""
Read the metadata version strings in the respective
pyproject.toml files from the metadata branch and target
directories.

Expand Down Expand Up @@ -437,78 +518,5 @@ def report_script_version():
return version


def git_pull_template(metadata_branch, branch):
"""
This function will pull updates from the remote template repository.
"""
command = [
"git",
"pull",
"--all",
"-q",
]
sp.run(
command,
cwd=metadata_branch,
check=False,
)
command_checkout = ["git", "checkout", "".join({branch}), "-q"]
checkout_cmd = sp.run(
command_checkout,
cwd=metadata_branch,
stderr=sp.PIPE,
check=False,
)
# If the 'template-metadata-files' folder is not a Git repo
# an error message that contains the string 'fatal:' will be thrown
warning = "fatal:"
assert warning not in str(checkout_cmd.stderr.strip()), (
"The folder 'template-metadata-files' is not a git repository! "
"For this script to work either delete the folder or move it!!"
)
# If you try to clone a repo/branch/tag that doesn't exist
# Git will throw an error message that contains the string 'error:'
error = "error:"
assert error not in str(
checkout_cmd.stderr.strip()
), f"The branch or version tag named '{branch}' doesn't exist"
return None


def git_clone_template(ref_repo, metadata_branch, metadata_target, branch):
"""
This function will clone the template repository into a folder parallel
to the folder to get updated.
"""
command = [
"git",
"clone",
"-q",
"-c advice.detachedHead=false",
ref_repo,
metadata_branch,
]
clone_cmd = sp.run(
command,
stdout=sp.PIPE,
stderr=sp.PIPE,
cwd=metadata_target,
check=False,
)
# If the 'template-metadata-files' folder is not a Git repo
# an error message that contains the string 'fatal:' will be thrown
warning = "fatal:"
assert warning not in str(clone_cmd.stderr.strip()), (
"The repository you entered or the branch or version tag "
f"named '{branch}' doesn't exist"
)
command_checkout = ["git", "checkout", "".join({branch}), "-q"]
sp.run(
command_checkout,
cwd=metadata_branch,
check=False,
)
return None

if __name__ == "__main__":
main()