5 import subprocess
as sp
8 from urllib.parse
import urljoin
10 from fs.osfs
import OSFS
12 import gitlab.exceptions
13 from datetime
import datetime
14 from dateutil.parser
import parse
16 from concurrent.futures
import ThreadPoolExecutor, wait
18 from util
import get_lxplus_fs, def_arguments, Spinner, gitlab
26 p = argparse.ArgumentParser()
28 p.add_argument(
"--coverage-source", required=
True)
30 "--ref", default=os.getenv(
"CI_COMMIT_TAG", os.getenv(
"CI_COMMIT_SHA",
None))
33 "--coverage-commit-limit",
34 default=
int(os.getenv(
"COVERAGE_COMMIT_LIMIT", 10)),
40 "COVERAGE_WEBSITE_ROOT",
"/eos/user/a/atsjenkins/www/ACTS/coverage"
44 "--website-public-url",
46 "COVERAGE_WEBSITE_URL",
"https://acts.web.cern.ch/ACTS/coverage/"
49 p.add_argument(
"--project-id", default=3031, type=int)
50 p.add_argument(
"--dry-run",
"-s", action=
"store_true")
57 listdir = www_fs.listdir(
".")
59 print(
"Unable to establish SSH connection to lxplus")
60 print(
"This might indicate a problem with the credentials")
61 print(
"or a temporary connection / configuration problem")
67 project = gl.projects.get(args.project_id)
69 if len(args.ref) == 40:
71 deploy_name = args.ref[:8]
74 deploy_name = args.ref
76 coverage_dest = os.path.join(args.coverage_root, deploy_name)
77 print(
"Going to deploy coverage for", deploy_name,
"to", coverage_dest)
79 "Will be publicly available under",
80 urljoin(args.website_public_url, deploy_name),
83 src_fs = OSFS(args.coverage_source)
85 with
Spinner(f
"Publishing ref {deploy_name}"):
87 fs.copy.copy_dir(src_fs,
".", www_fs, deploy_name)
91 with
Spinner(text=
"Getting deployed commits"):
92 deployed_commits = set()
93 for item
in www_fs.listdir(
"."):
94 if not www_fs.isdir(item):
96 if item.startswith(
"v"):
98 deployed_commits.add(item)
100 with
Spinner(text=
"Getting info for deployed commits"):
101 with ThreadPoolExecutor(max_workers=20)
as tp:
103 futures = [tp.submit(project.commits.get, c)
for c
in deployed_commits]
106 deployed_commits_with_time = []
107 for commit, future
in zip(deployed_commits, futures):
109 info = future.result()
110 date =
parse(info.committed_date)
111 deployed_commits_with_time.append((commit, date))
112 except gitlab.exceptions.GitlabGetError
as e:
113 print(
"Commit", commit,
"not found, will remove")
115 deployed_commits_with_time = list(
116 reversed(sorted(deployed_commits_with_time, key=
lambda i: i[1]))
120 commits_to_keep = set(
121 h
for h, _
in deployed_commits_with_time[: args.coverage_commit_limit]
124 print(
"Currently deployed commits:")
125 for idx, (h, t)
in enumerate(deployed_commits_with_time):
126 if idx < args.coverage_commit_limit:
127 print(
" o", h,
"-", t)
129 print(
" x", h,
"-", t)
131 print(
"Keeping commits:",
", ".join(commits_to_keep))
133 commits_to_delete = deployed_commits - commits_to_keep
135 if len(commits_to_delete) > 0:
136 with
Spinner(
"Removing: %s" %
", ".join(commits_to_delete)):
138 for commit
in commits_to_delete:
139 www_fs.removetree(commit)
142 latest_commit = deployed_commits_with_time[0][0]
143 latest_coverage_url = urljoin(args.website_public_url, latest_commit)
148 <meta http-equiv="refresh" content="0; url={0}" />
151 Redirecting to <a href"{0}">{0}</a>
158 with
Spinner(
"Writing index file redirecting to %s" % latest_coverage_url):
160 with www_fs.open(
"index.html",
"w")
as f:
161 f.write(index_content)
164 if "__main__" == __name__: