git-batch/gitbatch/cli.py

173 lines
6.3 KiB
Python
Raw Normal View History

2019-12-02 08:53:35 +00:00
#!/usr/bin/env python3
"""Main program."""
import argparse
import os
import shutil
import tempfile
2019-12-02 08:53:35 +00:00
from collections import defaultdict
from pathlib import Path
2019-12-02 08:53:35 +00:00
from urllib.parse import urlparse
import git
from gitbatch import __version__
2021-01-03 19:45:07 +00:00
from gitbatch.logging import SingleLog
from gitbatch.utils import normalize_path, to_bool
2019-12-02 08:53:35 +00:00
class GitBatch:
2020-04-11 11:17:01 +00:00
"""Handles git operations."""
2019-12-02 08:53:35 +00:00
def __init__(self):
self.log = SingleLog()
self.logger = self.log.logger
self.args = self._cli_args()
self.config = self._config()
self.run()
def _cli_args(self):
parser = argparse.ArgumentParser(
2020-04-11 11:17:01 +00:00
description=("Clone single branch from all repositories listed in a file")
)
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
2020-10-02 09:41:32 +00:00
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"
)
2019-12-02 08:53:35 +00:00
return parser.parse_args()
def _config(self):
config = defaultdict(dict)
2020-10-02 09:41:32 +00:00
# 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]
2019-12-02 09:01:21 +00:00
input_file_raw = os.environ.get("GIT_BATCH_INPUT_FILE", ".batchfile")
2019-12-02 08:53:35 +00:00
config["input_file"] = normalize_path(input_file_raw)
config["ignore_existing"] = to_bool(os.environ.get("GIT_BATCH_IGNORE_EXISTING", True))
2019-12-02 08:53:35 +00:00
config["ignore_missing"] = to_bool(os.environ.get("GIT_BATCH_IGNORE_MISSING_REMOTE", True))
return config
def _repos_from_file(self, src):
repos = []
with open(src) as f:
2019-12-02 08:53:35 +00:00
for num, line in enumerate(f, start=1):
repo = {}
line = line.strip()
if line and not line.startswith("#"):
try:
url, src, dest = (x.strip() for x in line.split(";"))
branch, *_ = (x.strip() for x in src.split(":"))
path = None
if len(_) > 0:
path = Path(_[0])
path = path.relative_to(path.anchor)
2019-12-02 08:53:35 +00:00
except ValueError as e:
2020-04-11 11:17:01 +00:00
self.log.sysexit_with_message(
f"Wrong numer of delimiters in line {num}: {e}"
2020-04-11 11:17:01 +00:00
)
2019-12-02 08:53:35 +00:00
if url:
url_parts = urlparse(url)
repo["url"] = url
repo["branch"] = branch or "main"
repo["path"] = path
2019-12-02 08:53:35 +00:00
repo["name"] = os.path.basename(url_parts.path)
2020-10-02 09:41:32 +00:00
repo["rel_dest"] = dest
2020-04-11 11:17:01 +00:00
repo["dest"] = normalize_path(dest) or normalize_path(
"./{}".format(repo["name"])
)
2019-12-02 08:53:35 +00:00
repos.append(repo)
else:
self.log.sysexit_with_message(f"Repository Url is not set on line {num}")
2019-12-02 08:53:35 +00:00
return repos
def _repos_clone(self, repos):
2019-12-02 08:53:35 +00:00
for repo in repos:
with tempfile.TemporaryDirectory(prefix="gitbatch_") as tmp:
try:
options = ["--branch={}".format(repo["branch"]), "--single-branch"]
git.Repo.clone_from(repo["url"], tmp, multi_options=options)
os.makedirs(repo["dest"], 0o750, self.config["ignore_existing"])
except git.exc.GitCommandError as e:
skip = False
err_raw = e.stderr.strip().splitlines()[:-1]
err = [
x.split(":", 1)[1].strip().replace(repo["dest"], repo["rel_dest"])
for x in err_raw
]
if (
any("could not find remote branch" in item for item in err)
and self.config["ignore_missing"]
):
skip = True
if not skip:
self.log.sysexit_with_message("Error: {}".format("\n".join(err)))
except FileExistsError:
self._file_exist_handler()
try:
path = tmp
if repo["path"]:
path = normalize_path(os.path.join(tmp, repo["path"]))
if not os.path.isdir(path):
raise FileNotFoundError(Path(path).relative_to(tmp))
shutil.copytree(
path,
repo["dest"],
ignore=shutil.ignore_patterns(".git"),
dirs_exist_ok=self.config["ignore_existing"]
)
except FileExistsError:
self._file_exist_handler()
except FileNotFoundError as e:
self.log.sysexit_with_message(
"Error: directory '{}' not found in repository '{}'".format(
e, repo["name"]
)
)
def _file_exist_handler(self):
skip = False
err = ["direcory already exists"]
if self.config["ignore_existing"]:
self.logger.warning("Error: {}".format("\n".join(err)))
skip = True
if not skip:
self.log.sysexit_with_message("Error: {}".format("\n".join(err)))
2019-12-02 08:53:35 +00:00
def run(self):
2020-10-02 09:41:32 +00:00
self.log.set_level(self.config["logging"]["level"])
2019-12-02 08:53:35 +00:00
if os.path.isfile(self.config["input_file"]):
repos = self._repos_from_file(self.config["input_file"])
self._repos_clone(repos)
2019-12-02 08:53:35 +00:00
else:
2020-04-11 11:17:01 +00:00
self.log.sysexit_with_message(
"The given batch file at '{}' does not exist".format(
os.path.relpath(os.path.join("./", self.config["input_file"]))
)
)
2021-01-03 19:45:07 +00:00
def main():
GitBatch()