Implemented a git operations module for automated git ops, based on subprocess.
This commit is contained in:
36
src/git_ops.py
Normal file
36
src/git_ops.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def perform_git_operations(hdf5_upload):
|
||||||
|
status_command = ['git', 'status']
|
||||||
|
status = subprocess.run(status_command, capture_output=True, check=True)
|
||||||
|
|
||||||
|
if hdf5_upload:
|
||||||
|
upload_ext = ['.h5', '.yaml']
|
||||||
|
else:
|
||||||
|
upload_ext = ['.yaml']
|
||||||
|
|
||||||
|
files_to_add_list = extract_files_to_add(status.stdout, upload_ext)
|
||||||
|
if files_to_add_list:
|
||||||
|
add_files_to_git(files_to_add_list)
|
||||||
|
commit_changes('Updated hdf5 file with yaml review file.')
|
||||||
|
else:
|
||||||
|
print("There were no found h5 and yaml files, needing to be saved. This action will not have effect on the review process' commit history.")
|
||||||
|
|
||||||
|
def extract_files_to_add(git_status_output, upload_ext):
|
||||||
|
files_to_add_list = []
|
||||||
|
for line in git_status_output.splitlines():
|
||||||
|
tmp = line.decode("utf-8")
|
||||||
|
if 'modified' in tmp:
|
||||||
|
if any(ext in tmp for ext in upload_ext):
|
||||||
|
files_to_add_list.append(tmp.split()[1])
|
||||||
|
return files_to_add_list
|
||||||
|
|
||||||
|
def add_files_to_git(files_to_add_list):
|
||||||
|
add_command = ['git', 'add'] + files_to_add_list
|
||||||
|
subprocess.run(add_command, capture_output=True, check=True)
|
||||||
|
|
||||||
|
def commit_changes(message):
|
||||||
|
commit_command = ['git', 'commit', '-m', message]
|
||||||
|
commit_output = subprocess.run(commit_command, capture_output=True, check=True)
|
||||||
|
print(commit_output.stdout)
|
Reference in New Issue
Block a user