From bdbf665e50990e9ba7d471baea648a9513a844cc Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Fri, 2 Oct 2020 11:41:32 +0200 Subject: [PATCH] improve loggin if repos were skipped --- CHANGELOG.md | 6 ++++-- gitbatch/Cli.py | 26 ++++++++++++++++++++++++-- gitbatch/__init__.py | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1af4ac..523ce8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,4 @@ -- INTERNAL - - publish to dockerhub and quay.io +- ENHANCEMENT + - add log levels: + Log levels can be set by cli flags `-vvv` or `-qqq`. The default log level is `ERROR`. + - add warning if repos were skipped in case of not empty destination directories diff --git a/gitbatch/Cli.py b/gitbatch/Cli.py index 986ef45..ff1352a 100644 --- a/gitbatch/Cli.py +++ b/gitbatch/Cli.py @@ -31,11 +31,27 @@ class GitBatch: parser.add_argument( "--version", action="version", version="%(prog)s {}".format(__version__) ) + parser.add_argument( + "-v", dest="logging.level", action="append_const", const=-1, help="increase log level" + ) + parser.add_argument( + "-q", dest="logging.level", action="append_const", const=1, help="decrease log level" + ) return parser.parse_args() def _config(self): config = defaultdict(dict) + + # Override correct log level from argparse + levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] + log_level = levels.index("ERROR") + tmp_dict = self.args.__dict__ + if tmp_dict.get("logging.level"): + for adjustment in tmp_dict["logging.level"]: + log_level = min(len(levels) - 1, max(log_level + adjustment, 0)) + config["logging"]["level"] = levels[log_level] + input_file_raw = os.environ.get("GIT_BATCH_INPUT_FILE", ".batchfile") config["input_file"] = normalize_path(input_file_raw) @@ -66,6 +82,7 @@ class GitBatch: repo["url"] = url repo["branch"] = branch or "master" repo["name"] = os.path.basename(url_parts.path) + repo["rel_dest"] = dest repo["dest"] = normalize_path(dest) or normalize_path( "./{}".format(repo["name"]) ) @@ -85,10 +102,14 @@ class GitBatch: except git.exc.GitCommandError as e: passed = False err_raw = e.stderr.strip().splitlines()[:-1] - err = [x.split(":", 1)[1].strip() for x in err_raw] + err = [ + x.split(":", 1)[1].strip().replace(repo["dest"], repo["rel_dest"]) + for x in err_raw + ] if any(["already exists and is not an empty directory" in item for item in err]): if self.config["ignore_existing"]: + self.logger.warn("Git error: {}".format("\n".join(err))) passed = True if any(["Could not find remote branch" in item for item in err]): @@ -96,9 +117,10 @@ class GitBatch: passed = True if not passed: - self.log.sysexit_with_message("Git error: " + "\n".join(err)) + self.log.sysexit_with_message("Git error: {}".format("\n".join(err))) def run(self): + self.log.set_level(self.config["logging"]["level"]) if os.path.isfile(self.config["input_file"]): repos = self._repos_from_file(self.config["input_file"]) self._repos_clone(repos, self.config["ignore_existing"]) diff --git a/gitbatch/__init__.py b/gitbatch/__init__.py index 7667014..2df6f2b 100644 --- a/gitbatch/__init__.py +++ b/gitbatch/__init__.py @@ -2,7 +2,7 @@ __author__ = "Robert Kaussow" __project__ = "git-batch" -__version__ = "0.2.1" +__version__ = "0.3.1" __license__ = "MIT" __maintainer__ = "Robert Kaussow" __email__ = "mail@thegeeklab.de"