Skip to content

Commit 58d176f

Browse files
committed
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 9df5d25 commit 58d176f

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

config/runtime/base/python.jinja2

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,33 @@ 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
98103
resp = requests.get(url, stream=True)
99104
resp.raise_for_status()
100105
tarball_name = os.path.basename(urllib.parse.urlparse(url).path)
101106
base, ext = tarball_name.split('.tar.')
102107
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)
108+
tarball.extractall(path=path)
109+
return os.path.join(path, base)
110+
111+
def _get_kernel_source(self):
112+
print("Getting kernel source tree...")
113+
tarball_url = self._get_tarball_url(self._node)
114+
src_path = self._get_source(tarball_url)
115+
print(f"Source directory: {src_path}")
105116

106117
def _run(self, src_path):
107118
raise NotImplementedError("_run() method required to run job")
@@ -119,10 +130,11 @@ class BaseJob:
119130
if self._node.get('debug') and 'dry_run' in self._node['debug']:
120131
return self.dry_run(self._node['debug']['result'])
121132

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}")
133+
if self._node.get('kind') == 'kbuild':
134+
src_path = self._get_kernel_source()
135+
else:
136+
src_path = self._workspace
137+
126138
print("Running job...")
127139
return self._run(src_path)
128140

0 commit comments

Comments
 (0)