2023-12-19 22:18:24 +00:00
|
|
|
import logging as log
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import git
|
|
|
|
|
|
|
|
|
|
|
|
def run_subprocess(command: str, workdir: str) -> bool:
|
|
|
|
"""execute subprocess and print output
|
|
|
|
|
|
|
|
Args:
|
|
|
|
cmd (str): command
|
|
|
|
cwd (str): working directory
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: False if subprocess fails
|
|
|
|
"""
|
|
|
|
try:
|
2023-12-20 13:00:13 +00:00
|
|
|
log.debug(subprocess.run(command, cwd=workdir, shell=True, text=True))
|
2023-12-19 22:18:24 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-12-21 14:54:55 +00:00
|
|
|
def update_git_repo(repo_path: str, ignore_git_status: bool) -> bool:
|
2023-12-19 22:18:24 +00:00
|
|
|
"""update repo and check for new commits
|
|
|
|
|
|
|
|
Args:
|
2023-12-21 14:54:55 +00:00
|
|
|
repo_path (str): path to git repo
|
|
|
|
ignore_git_status (bool): continue even if no changes
|
2023-12-19 22:18:24 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: False if any step fails
|
|
|
|
"""
|
|
|
|
# create repo instance if it exists
|
|
|
|
try:
|
2023-12-21 14:54:55 +00:00
|
|
|
repo = git.Repo(repo_path)
|
2023-12-19 22:18:24 +00:00
|
|
|
except git.exc.NoSuchPathError:
|
|
|
|
log.error("directory not found")
|
|
|
|
return False
|
|
|
|
|
2023-12-21 14:54:55 +00:00
|
|
|
if not ignore_git_status:
|
|
|
|
# try to fetch latest changes
|
|
|
|
try:
|
|
|
|
repo.git.fetch()
|
|
|
|
except git.exc.GitCommandError as e:
|
|
|
|
log.error(str(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
# check for new commits
|
|
|
|
if repo.rev_parse("HEAD") == repo.rev_parse(f"origin/{repo.active_branch}"):
|
|
|
|
log.info("no changes - skipping")
|
|
|
|
return False
|
2023-12-19 22:18:24 +00:00
|
|
|
|
|
|
|
# pull remote changes to local branch
|
|
|
|
try:
|
|
|
|
log.info(repo.git.pull())
|
|
|
|
except git.exc.GitCommandError as e:
|
|
|
|
log.error(str(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|