From 72611fe6195c41c746c170f078bfb06750be336c Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Tue, 19 Dec 2023 23:18:24 +0100 Subject: [PATCH] refactor: move helper functions to separate module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 --- .gitignore | 1 + dc-ops | 61 ++------------------------------------------------- lib/helper.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 59 deletions(-) create mode 100644 lib/helper.py diff --git a/.gitignore b/.gitignore index 1d3ed4c..68a127f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.yml +**/__pycache__ diff --git a/dc-ops b/dc-ops index b354e28..a4f7c8f 100755 --- a/dc-ops +++ b/dc-ops @@ -6,11 +6,11 @@ # for civil use only import logging as log -import subprocess from pathlib import Path import yaml -from git import GitCommandError, NoSuchPathError, Repo + +from lib.helper import run_subprocess, update_git_repo # read config file configfile = Path(__file__).with_name("config.yml") @@ -21,63 +21,6 @@ with configfile.open("r") as f: loglevel = cfg.get("loglevel", "INFO").upper() log.basicConfig(format="%(message)s", level=loglevel) - -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 = Repo(stackdir) - except NoSuchPathError: - log.error("directory not found") - return False - - # try to fetch latest changes - try: - repo.git.fetch() - except 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 GitCommandError as e: - log.error(str(e)) - return False - - return True - - # iterate all stacks for stack in cfg["stacks"]: # skip disabled stacks diff --git a/lib/helper.py b/lib/helper.py new file mode 100644 index 0000000..39ed0d9 --- /dev/null +++ b/lib/helper.py @@ -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