Refactored a few git statemets in terms of subprocess.run

This commit is contained in:
2024-04-04 11:02:24 +02:00
parent 0417ac6deb
commit fa4fe691d0

View File

@ -35,16 +35,25 @@ def get_review_status(filename_path):
workflow_steps.append(line)
return workflow_steps[-1]
def checkout_review_branch(repo_obj,branch_name):
def checkout_review_branch(branch_name):
# Create a new branch
#branch_name = 'metadata-review-by-'+initials
head_commit = repo_obj.head.peel()# Get the commit hash associated with HEAD
if not branch_name in repo_obj.branches:
branch = repo_obj.create_branch(branch_name, head_commit)
else:
branch = repo_obj.branches[branch_name]
repo_obj.checkout(branch)
checkout_branch_command = lambda branch_name : ['git','checkout', branch_name]
output = subprocess.run(checkout_branch_command(branch_name), capture_output=True,text=True,check=True)
print(output.stdout)
#if not branch_name in repo_obj.branches:
# branch = repo_obj.create_branch(branch_name, head_commit)
#else:
# branch = repo_obj.branches[branch_name]
#repo_obj.checkout(branch)
status_command = ['git','status']
add_command = lambda add_list: ['git','add'] + add_list
commit_command = lambda message: ['git','commit','-m', message]
def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs):
@ -70,34 +79,54 @@ def first_initialize_metadata_review(hdf5_file_path, reviewer_attrs):
# Initialize metadata review workflow
print("Create branch metadata-review-by-"+initials+"\n")
checkout_review_branch(branch_name)
current_branch_command = ['git','branch','--show-current']
curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True)
if not branch_name in curr_branch.stdout:
print('Fail to checkout branch. ')
# Check if review file already exists and then check if it is still untracked
review_yaml_file_path = os.path.join("review/",filename+YAML_EXT)
if not os.path.exists(review_yaml_file_path):
review_yaml_file_path = utils.make_file_copy(os.path.join(hdf5_file_path_tail,filename+YAML_EXT), 'review')
#else:
# raise Warning("the file " + os.path.join("review/",filename+YAML_EXT)+ " already exists. Delete this file to reinitialize the metadata review process.")
review_yaml_file_path_tail, ext = os.path.splitext(review_yaml_file_path)
with open(os.path.join(review_yaml_file_path_tail+"-review_status"+".txt"),'w') as f:
f.write('under review')
checkout_review_branch(repo_obj, branch_name)
# Stage review files and commit them to local repository
status = subprocess.run(status_command,capture_output=True,text=True,check=True)
untracked_files_for_review = []
for line in status.stdout.splitlines():
if 'review/' in line.decode('utf8'):
untracked_files_for_review.append(line)
status_dict = repo_obj.status()
for filepath, file_status in status_dict.items():
result = subprocess.run(add_command(untracked_files_for_review),capture_output=True,check=True)
message = 'Initialized metadata review.'
commit_output = subprocess.run(commit_command(message),capture_output=True,check=True)
print(commit_output.stdout)
#status_dict = repo_obj.status()
#for filepath, file_status in status_dict.items():
# Identify keys associated to review files and stage them
if 'review/'+filename in filepath:
# if 'review/'+filename in filepath:
# Stage changes
repo_obj.index.add(filepath)
# repo_obj.index.add(filepath)
author = config_file.author #default_signature
committer = config_file.committer
message = "Initialized metadata review process."
tree = repo_obj.index.write_tree()
oid = repo_obj.create_commit('HEAD', author, committer, message, tree, [repo_obj.head.peel().oid])
#author = config_file.author #default_signature
#committer = config_file.committer
#message = "Initialized metadata review process."
#tree = repo_obj.index.write_tree()
#oid = repo_obj.create_commit('HEAD', author, committer, message, tree, [repo_obj.head.peel().oid])
#print("Add and commit"+"\n")
@ -119,7 +148,13 @@ def second_submit_metadata_review(review_yaml_file_path, reviewer_attrs):
initials = reviewer_attrs['initials']
branch_name = '-'.join([reviewer_attrs['type'],'review','by',initials])
# TODO: replace with subprocess + git
checkout_review_branch(repo_obj, branch_name)
#checkout_review_branch(repo_obj, branch_name)
current_branch_command = ['git','branch','--show-current']
curr_branch = subprocess.run(current_branch_command,capture_output=True,text=True,check=True)
if not branch_name in curr_branch.stdout:
raise ValueError('Make sure you run initial workflow step. If you are running the workflow using the jupyter notebook. Excecute Cell associated with Step 1.')
#if any([status in get_review_status(filename_path) for status in ['under review','submitted']]):
@ -129,9 +164,7 @@ def second_submit_metadata_review(review_yaml_file_path, reviewer_attrs):
##
status_command = ['git','status']
add_command = lambda add_list: ['git','add'] + add_list
commit_command = lambda message: ['git','commit','-m', message]
#push_command = lambda repository,refspec: ['git','push',repository,refspec]
status = subprocess.run(status_command,capture_output=True,check=True)