repo-cloner/repo_cloner/process_repository_dir.py
Václav Valíček 6463a6bb95
Update, repo tool cloner: recursive clones
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
2022-07-29 17:03:15 +02:00

124 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python3
import argparse
import logging
import os
import logging as l
console_logger = l.StreamHandler()
console_formatter = l.Formatter(
"%(asctime)-15s :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)",
"%Y-%m-%d %H:%M:%S")
# setup logger
console_logger.setFormatter(console_formatter)
log = l.getLogger("rc")
log.addHandler(console_logger)
log.setLevel(logging.DEBUG)
from repo_cloner.lib.repo_dir_structure import RepoDirStructure
from repo_cloner.lib.cloner import Cloner
from repo_cloner.lib.repo_tool import RepoTool
from git.config import GitConfigParser
from git.repo import Repo
from typing import Union
def config_try_override(config_writer: GitConfigParser, section: str, option: str, value: str):
if not section in config_writer.sections():
log.debug(f"CFG Creating section: {section}")
config_writer.add_section(section)
if not config_writer.has_option(section, option):
log.debug(f"CFG Creating option: {option}")
config_writer.add_value(section, option, "")
log.debug(f"Setting {section}.{option} = {value}")
config_writer.set(section, option, value)
def main() -> int:
def update(op_code: int, cur_count: Union[str, float], max_count: Union[str, float, None] = None,
message: str = ''):
log.debug(f"op: {op_code}; cur: {cur_count}/{max_count}; mess: {message}")
# parse input arguments
parser = argparse.ArgumentParser(description = "repo-cloner entering script")
parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True,
default = None, type = str)
args = parser.parse_args()
log.info(f"Started processing git group in folder: {args.base_dir}")
dirs = RepoDirStructure(args.base_dir)
log.debug(f"Patching XDG_CONFIG_HOME to mock up git config")
os.environ['XDG_CONFIG_HOME'] = dirs.conf_dir
# check dir existence
try:
assert dirs.dirs_exist
except Exception as e:
log.critical(f"Dir structure problem: {e.__str__()}")
return 1
# does config exist?
try:
assert dirs.has_config
except Exception as e:
log.critical(f"Config file not found!?")
log.critical(e.__str__())
return 1
# ignore invalid config lines
# check if there is project name & git url supplied
config = dirs.config
if len(config.cloner_repo_url) == 0:
log.critical("Config directive cloner_repo_url is missing/empty! Cannot continue!")
return 1
if len(config.cloner_project_name) == 0:
log.warning("Config directive cloner_project_name should not be omitted!")
# cloner = Cloner(dirs)
# cloner.check_interval()
import subprocess
subprocess.run(["/usr/bin/rm", "-Rf", "/tmp/test/repos"])
subprocess.run(["/usr/bin/mkdir", "/tmp/test/repos"])
rt = RepoTool("/tmp/test/repos/main.git")
# rt.clone("https://github.com/u-boot/u-boot.git")
x = rt.clone_recursive("file:///home/vasek/dev/repo-cloner/tests/_support_data/test-repo-submodules-multilevel")
print(x)
# url = ""
# for x in rt._repo.remote("origin").urls:
# url = x
# url = url.replace("test-repo-base", "test-repo-reduced")
# url = url.replace("test-repo-base", "test-repo-changed-branches")
# rt._repo.remote("origin").set_url(url)
return 0
# from git import Repo
r = Repo("file:///home/vasek/dev/repo-cloner")
path: str = r._get_config_path("user")
print(path)
path = os.path.dirname(path)
print(path)
if not os.path.isdir(path):
os.mkdir(path)
cred_store: str = os.path.join(path, "git-credentials")
ssh_identity: str = os.path.join(dirs.conf_dir, "ssh", "identity")
with r.config_writer("user") as cfgw:
config_try_override(cfgw, "credential", "helper", f"store --file={cred_store}")
config_try_override(cfgw, "core", "sshcommand",
f"ssh -i {ssh_identity} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -q")
from repo_cloner.lib.cloner_config import ClonerConfigParser
ClonerConfigParser(os.path.join(dirs.conf_dir, "cloner.cfg"))
if __name__ == "__main__":
exit(main())