Moved remaining git operations in metadata_review_lib.py to git_ops.py and refactored accoringly

This commit is contained in:
2024-07-05 15:46:20 +02:00
parent 77386432f8
commit 92eca4d79e
2 changed files with 19 additions and 19 deletions

View File

@ -33,4 +33,11 @@ def add_files_to_git(files_to_add_list):
def commit_changes(message): def commit_changes(message):
commit_command = ['git', 'commit', '-m', message] commit_command = ['git', 'commit', '-m', message]
commit_output = subprocess.run(commit_command, capture_output=True, check=True) commit_output = subprocess.run(commit_command, capture_output=True, check=True)
print(commit_output.stdout) print(commit_output.stdout)
def get_status():
return subprocess.run(['git','status'],capture_output=True,text=True,check=True)
def show_current_branch():
current_branch_command = ['git','branch','--show-current']
subprocess.run(current_branch_command,capture_output=True,text=True,check=True)

View File

@ -10,14 +10,7 @@ import src.g5505_utils as utils
import src.hdf5_vis as hdf5_vis import src.hdf5_vis as hdf5_vis
import src.hdf5_lib as hdf5_lib import src.hdf5_lib as hdf5_lib
import src.git_ops as git_ops import src.git_ops as git_ops
# TODO: incorporate lines 14-18 in git_ops module and refactor code where needed
current_branch_command = ['git','branch','--show-current']
status_command = ['git','status']
add_command = lambda add_list: ['git','add'] + add_list
rm_command = lambda add_list: ['git','add'] + add_list
commit_command = lambda message: ['git','commit','-m', message]
#import input_files.config_file as config_file
import numpy as np import numpy as np
@ -97,8 +90,8 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F
#checkout_review_branch(branch_name) #checkout_review_branch(branch_name)
# Check you are working at the right branch # Check you are working at the right branch
current_branch_command = ['git','branch','--show-current']
curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) curr_branch = git_ops.show_current_branch()
if not branch_name in curr_branch.stdout: if not branch_name in curr_branch.stdout:
raise ValueError("Branch "+branch_name+" was not found. \nPlease open a Git Bash Terminal, and follow the below instructions: \n1. Change directory to your project's directory. \n2. Excecute the command: git checkout "+branch_name) raise ValueError("Branch "+branch_name+" was not found. \nPlease open a Git Bash Terminal, and follow the below instructions: \n1. Change directory to your project's directory. \n2. Excecute the command: git checkout "+branch_name)
@ -120,7 +113,7 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F
f.write('under review') f.write('under review')
# Stage untracked review files and commit them to local repository # Stage untracked review files and commit them to local repository
status = subprocess.run(status_command,capture_output=True,text=True,check=True) status = git_ops.get_status()
untracked_files = [] untracked_files = []
for line in status.stdout.splitlines(): for line in status.stdout.splitlines():
#tmp = line.decode("utf-8") #tmp = line.decode("utf-8")
@ -135,9 +128,9 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs, restart = F
untracked_files.append(line.strip()) untracked_files.append(line.strip())
if untracked_files: if untracked_files:
result = subprocess.run(add_command(untracked_files),capture_output=True,check=True) result = subprocess.run(git_ops.add_files_to_git(untracked_files),capture_output=True,check=True)
message = 'Initialized metadata review.' message = 'Initialized metadata review.'
commit_output = subprocess.run(commit_command(message),capture_output=True,check=True) commit_output = subprocess.run(git_ops.commit_changes(message),capture_output=True,check=True)
for line in commit_output.stdout.splitlines(): for line in commit_output.stdout.splitlines():
print(line.decode('utf-8')) print(line.decode('utf-8'))
@ -184,12 +177,12 @@ def second_save_metadata_review(review_yaml_file_path, reviewer_attrs):
#checkout_review_branch(repo_obj, branch_name) #checkout_review_branch(repo_obj, branch_name)
# Check you are working at the right branch # Check you are working at the right branch
curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) curr_branch = git_ops.show_current_branch()
if not branch_name in curr_branch.stdout: if not branch_name in curr_branch.stdout:
raise ValueError('Please checkout ' + branch_name + ' via Git Bash before submitting metadata review files. ') raise ValueError('Please checkout ' + branch_name + ' via Git Bash before submitting metadata review files. ')
# Collect modified review files # Collect modified review files
status = subprocess.run(status_command,capture_output=True,check=True) status = git_ops.get_status()
modified_files = [] modified_files = []
os.path.basename(review_yaml_file_path) os.path.basename(review_yaml_file_path)
for line in status.stdout.splitlines(): for line in status.stdout.splitlines():
@ -208,9 +201,9 @@ def second_save_metadata_review(review_yaml_file_path, reviewer_attrs):
modified_files.append(review_status_file_path) modified_files.append(review_status_file_path)
result = subprocess.run(add_command(modified_files),capture_output=True,check=True) result = subprocess.run(git_ops.add_files_to_git(modified_files),capture_output=True,check=True)
message = 'Submitted metadata review.' message = 'Submitted metadata review.'
commit_output = subprocess.run(commit_command(message),capture_output=True,check=True) commit_output = subprocess.run(git_ops.commit_changes(message),capture_output=True,check=True)
for line in commit_output.stdout.splitlines(): for line in commit_output.stdout.splitlines():
print(line.decode('utf-8')) print(line.decode('utf-8'))
@ -283,7 +276,7 @@ def last_submit_metadata_review(reviewer_attrs):
branch_name = '_'.join(['review',initials]) branch_name = '_'.join(['review',initials])
push_command = lambda repository,refspec: ['git','push',repository,refspec] push_command = lambda repository,refspec: ['git','push',repository,refspec]
current_branch_command = ['git','branch','--show-current']
list_branches_command = ['git','branch','--list'] list_branches_command = ['git','branch','--list']
branches = subprocess.run(list_branches_command,capture_output=True,text=True,check=True) branches = subprocess.run(list_branches_command,capture_output=True,text=True,check=True)
@ -292,7 +285,7 @@ def last_submit_metadata_review(reviewer_attrs):
print('Make sure to run data owner review workflow from the beginning without missing any steps.') print('Make sure to run data owner review workflow from the beginning without missing any steps.')
return return
curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True) curr_branch = git_ops.show_current_branch()
if not branch_name in curr_branch.stdout: if not branch_name in curr_branch.stdout:
print('Complete metadata review could not be completed.\n') print('Complete metadata review could not be completed.\n')
print('Make sure a data-owner workflow has already been started on branch '+branch_name+'\n') print('Make sure a data-owner workflow has already been started on branch '+branch_name+'\n')