babeltrace: check header include guards in lint job
[lttng-ci.git] / scripts / jenkins_job_env.py
CommitLineData
1e65ccd9
KS
1#!/usr/bin/env python3
2#
3# SPDX-FileCopyrightText: 2024 Kienan Stewart <kstewart@efficios.com>
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import argparse
8import logging
9import os
10import pathlib
11import platform
12import re
13import shlex
14import subprocess
15import sys
16import tempfile
17import urllib
18
19
20_ENV_VARS = [
21 "BABELTRACE_PLUGIN_PATH",
22 "CPPFLAGS",
23 "LD_LIBRARY_PATH",
24 "LDFLAGS",
25 "PATH",
26 "PKG_CONFIG_PATH",
27 "PYTHONPATH",
28 "WORKSPACE",
29]
30
31
32def _get_argparser():
33 parser = argparse.ArgumentParser(
34 description="Fetch and create a stub environment from common job artifacts",
35 )
36 # Commands: fetch (implies activate), activate, deactivate
19562378 37 subparsers = parser.add_subparsers(dest="command")
1e65ccd9
KS
38 parser.add_argument(
39 "-v", "--verbose", action="count", help="Increase the verbosity"
40 )
41
42 fetch_parser = subparsers.add_parser("fetch")
43 fetch_parser.add_argument(
44 "directory",
45 help="The directory",
46 type=pathlib.Path,
47 )
48 fetch_parser.add_argument(
49 "-s",
50 "--server",
51 default="https://ci.lttng.org",
52 help="The jenkins server to use",
53 )
54 fetch_parser.add_argument(
55 "-j",
56 "--job",
57 help="The job name, eg. 'lttng-tools_master_root_slesbuild'",
58 default=None,
59 required=True,
60 )
61 fetch_parser.add_argument(
62 "-jc",
63 "--job-configuration",
64 help="An optional job configuration, eg. 'babeltrace_version=stable-2.0,build=std,conf=agents,liburcu_version=master,node=sles15sp4-amd64-rootnode,platform=sles15sp4-amd64'",
65 default=None,
66 )
67 fetch_parser.add_argument(
68 "-b", "--build-id", help="The build ID, eg. '28'", default=None, required=True
69 )
70 fetch_parser.add_argument(
71 "-n",
72 "--no-download",
73 help="Do not activate environment after fetching artifacts",
74 action="store_false",
75 dest="download",
76 default=True,
77 )
78
79 return parser
80
81
82def fetch(destination, server, job, build, job_configuration=None, download=True):
83 if destination.exists() and not destination.is_dir():
84 raise Exception("'{}' exists but is not a directory".format(str(destination)))
85 if not destination.exists():
86 destination.mkdir()
87
88 if download:
89 components = [
90 "job",
91 job,
92 job_configuration or "",
93 build,
94 "artifact",
95 "*zip*",
96 "archive.zip",
97 ]
98 url_components = [urllib.parse.quote_plus(x) for x in components]
99 url = "/".join([server] + url_components)
100 logging.info("Fetching archive from '{}'".format(url))
101
102 with tempfile.NamedTemporaryFile() as archive:
103 subprocess.run(["wget", url, "-O", archive.name])
104 subprocess.run(["unzip", "-d", str(destination), archive.name])
105
106 # The artifact archive doesn't include symlinks, so the the symlinks for
107 # the ".so" in libdir_arch must be rebuilt
108 lib_dir = "lib"
109 lib_dir_arch = lib_dir
110 if (
111 pathlib.Path("/etc/products.d/SLES.prod").exists()
112 or pathlib.Path("/etc/redhat-release").exists()
113 or pathlib.Path("/etc/yocto-release").exists()
19562378 114 ) and "64bit" in platform.architecture():
1e65ccd9
KS
115 lib_dir_arch = "{}64"
116
117 so_re = re.compile("^.*\.so\.\d+\.\d+\.\d+$")
118 for root, dirs, files in os.walk(
119 str(destination / "deps" / "build" / lib_dir_arch)
120 ):
121 for f in files:
122 if so_re.match(f):
123 bits = f.split(".")
124 alts = [
125 ".".join(bits[:-1]),
126 ".".join(bits[:-2]),
127 ".".join(bits[:-3]),
128 ]
129 for a in alts:
130 os.symlink(f, os.path.join(root, a))
131
132 env = create_activate(destination)
133 create_deactivate(destination, env)
134
135
136def create_activate(destination):
137 lib_dir = "lib"
138 lib_dir_arch = lib_dir
139 if (
140 pathlib.Path("/etc/products.d/SLES.prod").exists()
141 or pathlib.Path("/etc/redhat-release").exists()
142 or pathlib.Path("/etc/yocto-release").exists()
19562378 143 ) and "64bit" in platform.architecture():
1e65ccd9
KS
144 lib_dir_arch = "{}64"
145
146 env = {}
147 env["_JENKINS_ENV"] = destination.name
148 for var in _ENV_VARS:
149 original = os.getenv(var)
150 env["_JENKINS_{}".format(var)] = original if original else ""
151 if var == "BABELTRACE_PLUGIN_PATH":
152 env["BABELTRACE_PLUGIN_PATH"] = "{}{}".format(
153 "{}:".format(original) if original else "",
154 str(
155 (
156 destination
157 / "archive"
158 / "deps"
159 / "build"
160 / lib_dir_arch
161 / "babeltrace2"
162 / "plugins"
163 ).absolute()
164 ),
165 )
166 elif var == "CPPFLAGS":
167 env["CPPFLAGS"] = "{}-I{}".format(
168 "{} ".format(original) if original else "",
169 str(
170 (destination / "archive" / "deps" / "build" / "include").absolute()
171 ),
172 )
173 elif var == "LD_LIBRARY_PATH":
174 env["LD_LIBRARY_PATH"] = "{}{}".format(
175 "{}:".format(original) if original else "",
176 str(
177 (
178 destination / "archive" / "deps" / "build" / lib_dir_arch
179 ).absolute()
180 ),
181 )
182 elif var == "LDFLAGS":
183 env["LDFLAGS"] = "{}-L{}".format(
184 "{} ".format(original) if original else "",
185 str(
186 (
187 destination / "archive" / "deps" / "build" / lib_dir_arch
188 ).absolute()
189 ),
190 )
191 elif var == "PATH":
192 env["PATH"] = "{}:{}".format(
193 original,
194 str((destination / "archive" / "deps" / "build" / "bin").absolute()),
195 )
196 elif var == "PKG_CONFIG_PATH":
197 env["PKG_CONFIG_PATH"] = "{}{}".format(
198 "{}:" if original else "",
199 str(
200 (
201 destination
202 / "archive"
203 / "deps"
204 / "build"
205 / lib_dir_arch
206 / "pkgconfig"
207 ).absolute()
208 ),
209 )
210 elif var == "PYTHONPATH":
211 pass
212 elif var == "WORKSPACE":
213 env["WORKSPACE"] = str((destination / "archive").absolute())
214 else:
215 raise Exception("Unsupported environment variable '{}'".format(var))
216
217 args = ["{}={}".format(k, shlex.quote(v)) for k, v in env.items()]
218 with open(str(destination / "activate"), "w") as fp:
219 fp.writelines("#!/usr/bin/bash\n")
220 for arg in args:
221 fp.writelines("export {}\n".format(arg))
222 (destination / "activate").chmod(0o755)
223 return env
224
225
226def create_deactivate(destination, env):
227 with open(str(destination / "deactivate"), "w") as fp:
228 fp.writelines("#!/usr/bin/bash\n")
229 for k, v in env.items():
230 if k.startswith("_JENKINS_"):
231 fp.writelines("unset {}\n".format(k))
232 else:
233 original = env["_JENKINS_{}".format(k)]
234 fp.writelines("export {}={}\n".format(k, original))
235 (destination / "deactivate").chmod(0o755)
236
237
238if __name__ == "__main__":
239 logger = logging.getLogger()
240 parser = _get_argparser()
241 args = parser.parse_args()
242 logger.setLevel(max(1, 30 - (args.verbose or 0) * 10))
243 logging.debug("Initialized with log level: {}".format(logger.getEffectiveLevel()))
244
245 if args.command == "fetch":
246 fetch(
247 destination=args.directory,
248 server=args.server,
249 job=args.job,
250 build=args.build_id,
251 job_configuration=args.job_configuration,
252 download=args.download,
253 )
254 else:
255 raise Exception("Command '{}' unsupported".format(args.command))
256 sys.exit(0)
This page took 0.031252 seconds and 4 git commands to generate.