Skip to content

Commit dfc942f

Browse files
a-waiJenySadadia
authored andcommitted
config: runtime: base: python: make more generic
The base `python` template was designed with post-checkout jobs (`kbuild`, `kunit`...) in mind and doesn't work too well with test or post-processing jobs due to the following issues: * it assumes we need the kernel source from the parent `checkout` node * it doesn't provide a way to retrieve arbitrary artifacts from a node or its parent * the `_get_source()` function doesn't allow specifying an arbitrary target folder to extract tarballs to Ensure we only download/extract/cd to the kernel source if the current node is a `kbuild` one, otherwise let the job definition prepare its workspace as needed. In order to make things easier to jobs needing to work on the original kernel source, add a new `_get_kernel_source()` so such jobs (e.g. kunit) need a single function call. Also add a generic `_get_artifact_url()` for downloading arbitrary artifacts and make the existing `_get_tarball_url()` use it to avoid duplicating code. Finally, add an optional `path` parameter to `_get_source()` so the job can specify the destination folder if needed. Signed-off-by: Arnaud Ferraris <arnaud.ferraris@collabora.com>
1 parent 8dc0b7e commit dfc942f

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

config/runtime/base/python.jinja2

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,36 @@ class BaseJob:
8686
storage_cred = os.getenv('KCI_STORAGE_CREDENTIALS')
8787
return kernelci.storage.get_storage(storage_config, storage_cred)
8888

89-
def _get_tarball_url(self, node):
90-
if node.get('artifacts') and 'tarball' in node['artifacts']:
91-
return node['artifacts']['tarball']
89+
def _get_artifact_url(self, node, artifact):
90+
if node.get('artifacts') and artifact in node['artifacts']:
91+
return node['artifacts'][artifact]
9292
if node.get('parent'):
9393
parent = self._api.node.get(node['parent'])
94-
return self._get_tarball_url(parent)
95-
raise ValueError(f"'tarball' artifact not found in node {self._node['id']} ancestors")
94+
return self._get_artifact_url(parent, artifact)
95+
raise ValueError(f"'{artifact}' artifact not found in node {node['id']} ancestors")
96+
97+
def _get_tarball_url(self, node):
98+
return self._get_artifact_url(node, 'tarball')
9699

97-
def _get_source(self, url):
100+
def _get_source(self, url, path=None):
101+
if path is None:
102+
path = self._workspace
103+
# Ensure our destination folder exists
104+
os.makedirs(path, exist_ok=True)
98105
resp = requests.get(url, stream=True)
99106
resp.raise_for_status()
100107
tarball_name = os.path.basename(urllib.parse.urlparse(url).path)
101108
base, ext = tarball_name.split('.tar.')
102109
with tarfile.open(fileobj=resp.raw, mode=f'r|{ext}') as tarball:
103-
tarball.extractall(path=self._workspace)
104-
return os.path.join(self._workspace, base)
110+
tarball.extractall(path=path)
111+
return os.path.join(path, base)
112+
113+
def _get_kernel_source(self):
114+
print("Getting kernel source tree...")
115+
tarball_url = self._get_tarball_url(self._node)
116+
src_path = self._get_source(tarball_url)
117+
print(f"Source directory: {src_path}")
118+
return src_path
105119

106120
def _run(self, src_path):
107121
raise NotImplementedError("_run() method required to run job")
@@ -119,10 +133,14 @@ class BaseJob:
119133
if self._node.get('debug') and 'dry_run' in self._node['debug']:
120134
return self.dry_run(self._node['debug']['result'])
121135

122-
print("Getting kernel source tree...")
123-
tarball_url = self._get_tarball_url(self._node)
124-
src_path = self._get_source(tarball_url)
125-
print(f"Source directory: {src_path}")
136+
# Ensure our workspace folder exists
137+
os.makedirs(self._workspace, exist_ok=True)
138+
139+
if self._node.get('kind') == 'kbuild':
140+
src_path = self._get_kernel_source()
141+
else:
142+
src_path = self._workspace
143+
126144
print("Running job...")
127145
return self._run(src_path)
128146

0 commit comments

Comments
 (0)