-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfetch-my-conf.py
executable file
·69 lines (56 loc) · 2.35 KB
/
fetch-my-conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/python3
"""Clone given git repository and copy local rpminspect configuration file to the current working directory."""
import shutil
import tempfile
from pathlib import Path
from typing import List
import sys
import click
import git
from retry import retry
@retry((git.exc.GitCommandError), delay=60, tries=10, log_traceback=True)
def clone_and_copy(repo_url: str, branches: List[str], commit: str, strategy: str) -> None:
"""Clone given git repository and copy local rpminspect configuration file to the current working directory."""
git_cmd = git.cmd.Git()
for branch in branches:
# Stop here if the branch exists
if [x for x in git_cmd.ls_remote("--heads", repo_url, f"refs/heads/{branch}").split("\n") if x]:
break
else:
if strategy == 'branch':
print(f"Given branches ({branches}) don't exist...")
sys.exit(1)
# None of the branches exist -- we will need to use the commit hash
branch = None
with tempfile.TemporaryDirectory() as tmp_dir:
if branch and strategy in ('branch', 'fallback'):
print(f"Cloning {repo_url} (branch: {branch})...")
git.Repo.clone_from(url=repo_url, to_path=tmp_dir, single_branch=True, branch=branch)
else:
print(f"Cloning {repo_url} (commit: {commit})...")
git_repo = git.Repo.clone_from(url=repo_url, to_path=tmp_dir)
git_repo.git.checkout(commit)
for t in ["yaml", "json", "dson"]:
cfgfile = "rpminspect.%s" % t
rpminspect_cfg_path = Path(tmp_dir, cfgfile)
if rpminspect_cfg_path.is_file():
print("%s file found!" % cfgfile)
shutil.copy(rpminspect_cfg_path, Path(Path.cwd(), cfgfile))
else:
print("No %s in the repository..." % cfgfile)
@click.command()
@click.option(
'--strategy',
type=click.Choice(['branch', 'commit', 'fallback'], case_sensitive=False),
default='branch',
required=False,
help='Where to look for the rpminspect.yaml (default: "fallback")'
)
@click.argument("repo-url")
@click.argument("branches")
@click.argument("commit")
def main(repo_url: str, branches: str, commit: str, strategy: str) -> None:
branches = branches.split(",")
clone_and_copy(repo_url, branches, commit, strategy)
if __name__ == "__main__":
main()