Skip to content

Commit b55abb1

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 child to a `checkout` node, otherwise let the job definition prepare its workspace as needed. 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 <[email protected]>
1 parent 9df5d25 commit b55abb1

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
@@ -62,6 +62,9 @@ class BaseJob:
6262
def __init__(self, api_config, node, workspace):
6363
self._api = self._get_api(api_config)
6464
self._node = node
65+
self._parent = None
66+
if node.get('parent'):
67+
self._parent = self._api.node.get(self._node['parent'])
6568
self._workspace = workspace
6669

6770
def _get_api(self, api_config_yaml):
@@ -86,22 +89,27 @@ class BaseJob:
8689
storage_cred = os.getenv('KCI_STORAGE_CREDENTIALS')
8790
return kernelci.storage.get_storage(storage_config, storage_cred)
8891

89-
def _get_tarball_url(self, node):
90-
if node.get('artifacts') and 'tarball' in node['artifacts']:
91-
return node['artifacts']['tarball']
92+
def _get_artifact_url(self, node, artifact):
93+
if node.get('artifacts') and artifact in node['artifacts']:
94+
return node['artifacts'][artifact]
9295
if node.get('parent'):
9396
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")
97+
return self._get_artifact_url(parent, artifact)
98+
raise ValueError(f"'{artifact}' artifact not found in node {node['id']} ancestors")
9699

97-
def _get_source(self, url):
100+
def _get_tarball_url(self, node):
101+
return self._get_artifact_url(node, 'tarball')
102+
103+
def _get_source(self, url, path=None):
104+
if path is None:
105+
path = self._workspace
98106
resp = requests.get(url, stream=True)
99107
resp.raise_for_status()
100108
tarball_name = os.path.basename(urllib.parse.urlparse(url).path)
101109
base, ext = tarball_name.split('.tar.')
102110
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)
111+
tarball.extractall(path=path)
112+
return os.path.join(path, base)
105113

106114
def _run(self, src_path):
107115
raise NotImplementedError("_run() method required to run job")
@@ -119,10 +127,14 @@ class BaseJob:
119127
if self._node.get('debug') and 'dry_run' in self._node['debug']:
120128
return self.dry_run(self._node['debug']['result'])
121129

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}")
130+
if self._parent.get('kind') == 'checkout':
131+
print("Getting kernel source tree...")
132+
tarball_url = self._get_tarball_url(self._node)
133+
src_path = self._get_source(tarball_url)
134+
print(f"Source directory: {src_path}")
135+
else:
136+
src_path = self._workspace
137+
126138
print("Running job...")
127139
return self._run(src_path)
128140

0 commit comments

Comments
 (0)