refactor: move helper functions to separate module
🤖
This commit is contained in:
parent
7ca3da52d9
commit
72611fe619
3 changed files with 63 additions and 59 deletions
60
lib/helper.py
Normal file
60
lib/helper.py
Normal file
|
@ -0,0 +1,60 @@
|
|||
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:
|
||||
subprocess.run(command, cwd=workdir, shell=True, text=True)
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def update_git_repo(repo: str) -> bool:
|
||||
"""update repo and check for new commits
|
||||
|
||||
Args:
|
||||
repo (str): path to git repo
|
||||
|
||||
Returns:
|
||||
bool: False if any step fails
|
||||
"""
|
||||
# create repo instance if it exists
|
||||
try:
|
||||
repo = git.Repo(repo)
|
||||
except git.exc.NoSuchPathError:
|
||||
log.error("directory not found")
|
||||
return False
|
||||
|
||||
# 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
|
||||
|
||||
# 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
|
Loading…
Add table
Add a link
Reference in a new issue