2022-05-04 15:02:55 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2022-07-24 21:14:05 +02:00
|
|
|
import logging
|
2022-06-26 05:22:19 +02:00
|
|
|
import os
|
2022-05-04 15:02:55 +02:00
|
|
|
|
2022-06-26 05:22:19 +02:00
|
|
|
import logging as l
|
|
|
|
|
2022-07-24 22:24:42 +02:00
|
|
|
# l.basicConfig(level = 0)
|
2022-07-24 21:14:05 +02:00
|
|
|
|
|
|
|
# create console handler with a higher log level
|
|
|
|
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 git.config import GitConfigParser
|
2022-06-26 05:22:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
def config_try_override(config_writer: GitConfigParser, section: str, option: str, value: str):
|
|
|
|
if not section in config_writer.sections():
|
2022-07-24 21:14:05 +02:00
|
|
|
log.debug(f"CFG Creating section: {section}")
|
2022-06-26 05:22:19 +02:00
|
|
|
config_writer.add_section(section)
|
|
|
|
if not config_writer.has_option(section, option):
|
2022-07-24 21:14:05 +02:00
|
|
|
log.debug(f"CFG Creating option: {option}")
|
2022-06-26 05:22:19 +02:00
|
|
|
config_writer.add_value(section, option, "")
|
|
|
|
|
2022-07-24 21:14:05 +02:00
|
|
|
log.debug(f"Setting {section}.{option} = {value}")
|
2022-06-26 05:22:19 +02:00
|
|
|
config_writer.set(section, option, value)
|
2022-05-04 15:02:55 +02:00
|
|
|
|
|
|
|
|
2022-07-24 22:24:42 +02:00
|
|
|
def main() -> int:
|
2022-05-04 15:02:55 +02:00
|
|
|
# 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()
|
|
|
|
|
2022-07-24 22:24:42 +02:00
|
|
|
log.info(f"Started processing git group in folder: {args.base_dir}")
|
2022-05-04 15:02:55 +02:00
|
|
|
dirs = RepoDirStructure(args.base_dir)
|
2022-07-24 22:24:42 +02:00
|
|
|
log.debug(f"Patching XDG_CONFIG_HOME to mock up git config")
|
2022-06-26 05:22:19 +02:00
|
|
|
os.environ['XDG_CONFIG_HOME'] = dirs.conf_dir
|
|
|
|
|
2022-07-24 22:24:42 +02:00
|
|
|
# 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!")
|
|
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2022-06-26 05:22:19 +02:00
|
|
|
from git import Repo
|
|
|
|
r = Repo("/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",
|
2022-07-24 21:14:05 +02:00
|
|
|
f"ssh -i {ssh_identity} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -q")
|
2022-05-04 15:02:55 +02:00
|
|
|
|
2022-07-24 21:14:05 +02:00
|
|
|
from repo_cloner.lib.cloner_config import ClonerConfigParser
|
|
|
|
ClonerConfigParser(os.path.join(dirs.conf_dir, "cloner.cfg"))
|
2022-05-04 15:02:55 +02:00
|
|
|
|
2022-06-26 05:22:19 +02:00
|
|
|
|
2022-07-24 21:14:05 +02:00
|
|
|
if __name__ == "__main__":
|
2022-07-24 22:24:42 +02:00
|
|
|
exit(main())
|