2022-05-04 15:02:55 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-08-06 02:11:44 +02:00
|
|
|
from repo_cloner.lib.logger_setup import log
|
2022-05-04 15:02:55 +02:00
|
|
|
import argparse
|
2022-07-24 21:14:05 +02:00
|
|
|
import logging
|
2022-06-26 05:22:19 +02:00
|
|
|
import os
|
2022-08-06 02:11:44 +02:00
|
|
|
import subprocess
|
|
|
|
import base64
|
|
|
|
from repo_cloner.lib.repo_dir_structure import RepoDirStructure
|
|
|
|
from repo_cloner.lib import Cloner, DetectedCommit, prepare_git_auth, init_gh_token
|
2022-05-04 15:02:55 +02:00
|
|
|
|
2022-08-08 11:54:57 +02:00
|
|
|
laminar_binary: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
def determine_binary() -> str:
|
|
|
|
global laminar_binary
|
|
|
|
log.debug(f"Looking for laminar binary")
|
|
|
|
rc = subprocess.run(["/usr/bin/which", "laminarc", "echo"], stdout = subprocess.PIPE, stderr = subprocess.DEVNULL)
|
|
|
|
binary = rc.stdout.decode().splitlines()[0].strip()
|
|
|
|
if rc.returncode == 0:
|
|
|
|
log.debug(f"Laminar binary found in {binary}")
|
|
|
|
else:
|
|
|
|
log.warning(f"laminarc binary was not found - using {binary} as replacement for debugging")
|
|
|
|
return binary
|
|
|
|
|
2022-06-26 05:22:19 +02:00
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
def detector_executor(commit: DetectedCommit):
|
|
|
|
message = base64.b64encode(commit.log.encode()).decode()
|
|
|
|
env = {
|
|
|
|
"sha": commit.commit,
|
|
|
|
"abbrev": commit.abbrev,
|
|
|
|
"author": commit.author,
|
|
|
|
"has_tags": commit.is_tag,
|
|
|
|
"has_branches": commit.is_branch,
|
|
|
|
"tags": commit.tags,
|
|
|
|
"branches": commit.branches,
|
|
|
|
"date": commit.date,
|
|
|
|
"log": message,
|
|
|
|
}
|
2022-07-24 21:14:05 +02:00
|
|
|
|
2022-08-08 11:54:57 +02:00
|
|
|
arg_list = [laminar_binary, "queue", commit.project]
|
2022-06-26 05:22:19 +02:00
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
for key, val in env.items():
|
|
|
|
arg_list.append(f"COMMIT_{key.upper()}={val}")
|
2022-06-26 05:22:19 +02:00
|
|
|
|
2022-08-10 08:56:25 +02:00
|
|
|
reason = commit.abbrev
|
|
|
|
if commit.is_branch:
|
2022-08-10 08:59:37 +02:00
|
|
|
reason += f"/{commit.branches}"
|
2022-08-10 08:56:25 +02:00
|
|
|
if commit.is_tag:
|
|
|
|
reason += f" tags: {commit.tags}"
|
2022-08-10 08:59:37 +02:00
|
|
|
reason = f"[{reason}] " + commit.log.splitlines()[0]
|
2022-08-10 08:56:25 +02:00
|
|
|
|
|
|
|
env = os.environ.copy()
|
|
|
|
env['LAMINAR_REASON'] = reason
|
|
|
|
|
|
|
|
subprocess.run(arg_list, env = env)
|
2022-05-04 15:02:55 +02:00
|
|
|
|
|
|
|
|
2022-08-07 21:44:31 +02:00
|
|
|
def clone_or_fetch(base_dir: str, clone_init: bool = False, detector_init: bool = False):
|
2022-08-08 11:54:57 +02:00
|
|
|
global laminar_binary
|
2022-08-07 21:44:31 +02:00
|
|
|
log.info(f"Started processing git group in folder: {base_dir}")
|
|
|
|
dirs = RepoDirStructure(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-08-06 02:11:44 +02:00
|
|
|
init_gh_token()
|
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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!")
|
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
cloner = Cloner(dirs)
|
2022-08-07 21:44:31 +02:00
|
|
|
prepare_git_auth(dirs.conf_dir)
|
|
|
|
|
|
|
|
if clone_init:
|
|
|
|
log.info(f"Initial cloning of repositories")
|
|
|
|
if not cloner.clone():
|
|
|
|
return 1
|
|
|
|
if detector_init:
|
|
|
|
cloner.detector_init()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
# regular run
|
2022-08-06 02:11:44 +02:00
|
|
|
if not cloner.sync():
|
|
|
|
log.warning(f"Repo sync did not succeed")
|
2022-07-29 17:03:15 +02:00
|
|
|
|
2022-08-08 11:54:57 +02:00
|
|
|
laminar_binary = determine_binary()
|
|
|
|
|
2022-08-06 02:11:44 +02:00
|
|
|
if cloner.detector_enabled:
|
|
|
|
cloner.detector_run(detector_executor)
|
2022-07-24 22:24:42 +02:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2022-06-26 05:22:19 +02:00
|
|
|
|
2022-08-07 21:44:31 +02:00
|
|
|
def main() -> int:
|
|
|
|
# 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)
|
|
|
|
parser.add_argument('--debug', '-d', help = "enable debug output", action = 'store_true')
|
2022-08-10 08:56:25 +02:00
|
|
|
parser.add_argument('--colored', help = "enable colored log output even tty() is not detected",
|
|
|
|
action = "store_true")
|
2022-08-07 21:44:31 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
if args.debug:
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
return clone_or_fetch(args.base_dir)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:05 +02:00
|
|
|
if __name__ == "__main__":
|
2022-07-24 22:24:42 +02:00
|
|
|
exit(main())
|