Skip to content

Commit 2c50105

Browse files
author
Azure Pipelines
committed
Merge remote-tracking branch 'origin/main' into publication
2 parents bb5689a + 5c1c103 commit 2c50105

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

.actions/assistant.py

+63-9
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"""
8989

9090

91-
def default_requirements(path_req: str = PATH_REQ_DEFAULT) -> list:
91+
def load_requirements(path_req: str = PATH_REQ_DEFAULT) -> list:
92+
"""Load the requirements from a file."""
9293
with open(path_req) as fp:
9394
req = fp.readlines()
9495
req = [r[: r.index("#")] if "#" in r else r for r in req]
@@ -98,6 +99,7 @@ def default_requirements(path_req: str = PATH_REQ_DEFAULT) -> list:
9899

99100

100101
def get_running_cuda_version() -> str:
102+
"""Extract the version of actual CUDA for this runtime."""
101103
try:
102104
import torch
103105

@@ -107,6 +109,7 @@ def get_running_cuda_version() -> str:
107109

108110

109111
def get_running_torch_version():
112+
"""Extract the version of actual PyTorch for this runtime."""
110113
try:
111114
import torch
112115

@@ -129,6 +132,7 @@ def get_running_torch_version():
129132

130133

131134
class AssistantCLI:
135+
"""Collection of handy CLI commands."""
132136

133137
DEVICE_ACCELERATOR = os.environ.get("ACCELERATOR", "cpu").lower()
134138
DATASET_FOLDER = os.environ.get("PATH_DATASETS", "_datasets").lower()
@@ -161,13 +165,24 @@ class AssistantCLI:
161165

162166
@staticmethod
163167
def _find_meta(folder: str) -> str:
168+
"""Search for a meta file in given folder and return its path.
169+
170+
Args:
171+
folder: path to the folder with python script, meta and artefacts
172+
"""
164173
files = glob.glob(os.path.join(folder, AssistantCLI._META_FILE_REGEX), flags=glob.BRACE)
165174
if len(files) == 1:
166175
return files[0]
167176
return ""
168177

169178
@staticmethod
170179
def _load_meta(folder: str, strict: bool = False) -> Optional[dict]:
180+
"""Loading meta data for a particular notebook with given folder path.
181+
182+
Args:
183+
folder: path to the folder with python script, meta and artefacts
184+
strict: raise error if meta is missing required feilds
185+
"""
171186
fpath = AssistantCLI._find_meta(folder)
172187
assert fpath, f"Missing meta file in folder: {folder}"
173188
meta = yaml.safe_load(open(fpath))
@@ -180,6 +195,11 @@ def _load_meta(folder: str, strict: bool = False) -> Optional[dict]:
180195

181196
@staticmethod
182197
def _valid_conf_folder(folder: str) -> Tuple[str, str]:
198+
"""Validate notebook folder if it has required meta file and optional thumb.
199+
200+
Args:
201+
folder: path to the folder with python script, meta and artefacts
202+
"""
183203
meta_files = [os.path.join(folder, f".meta.{ext}") for ext in ("yml", "yaml")]
184204
meta_files = [pf for pf in meta_files if os.path.isfile(pf)]
185205
if len(meta_files) != 1:
@@ -193,6 +213,13 @@ def _valid_conf_folder(folder: str) -> Tuple[str, str]:
193213

194214
@staticmethod
195215
def _valid_folder(folder: str, ext: str) -> Tuple[str, str, str]:
216+
"""Validate notebook folder if it has required meta file, python script or ipython notebook (depending on
217+
the stage) and optional thumb.
218+
219+
Args:
220+
folder: path to the folder with python script, meta and artefacts
221+
ext: extension determining the stage - ".py" for python script nad ".ipynb" for notebook
222+
"""
196223
files = glob.glob(os.path.join(folder, f"*{ext}"))
197224
if len(files) != 1:
198225
names = list(map(os.path.basename, files))
@@ -202,9 +229,10 @@ def _valid_folder(folder: str, ext: str) -> Tuple[str, str, str]:
202229

203230
@staticmethod
204231
def _valid_accelerator(folder: str) -> bool:
205-
"""Parse standard requirements from meta file
232+
"""Parse standard requirements from meta file.
233+
206234
Args:
207-
folder: path to the folder
235+
folder: path to the folder with python script, meta and artefacts
208236
"""
209237
meta = AssistantCLI._load_meta(folder)
210238
meta_accels = [acc.lower() for acc in meta.get("accelerator", AssistantCLI._META_ACCEL_DEFAULT)]
@@ -213,9 +241,10 @@ def _valid_accelerator(folder: str) -> bool:
213241

214242
@staticmethod
215243
def _parse_requirements(folder: str) -> Tuple[str, str]:
216-
"""Parse standard requirements from meta file
244+
"""Parse standard requirements from meta file.
245+
217246
Args:
218-
folder: path to the folder
247+
folder: path to the folder with python script, meta and artefacts
219248
"""
220249
meta = AssistantCLI._load_meta(folder)
221250
req = meta.get("requirements", [])
@@ -237,6 +266,11 @@ def _parse_requirements(folder: str) -> Tuple[str, str]:
237266

238267
@staticmethod
239268
def _bash_download_data(folder: str) -> List[str]:
269+
"""Generate sequence of commands fro optional downloading dataset specified in the meta file.
270+
271+
Args:
272+
folder: path to the folder with python script, meta and artefacts
273+
"""
240274
cmd = ["HERE=$PWD", f"cd {AssistantCLI.DATASET_FOLDER}"]
241275
meta = AssistantCLI._load_meta(folder)
242276
datasets = meta.get("datasets", {})
@@ -260,6 +294,14 @@ def _bash_download_data(folder: str) -> List[str]:
260294

261295
@staticmethod
262296
def bash_render(folder: str) -> str:
297+
"""Prepare bash script for running rendering of a particular notebook.
298+
299+
Args:
300+
folder: name/path to a folder with notebook files
301+
302+
Returns:
303+
string with nash script content
304+
"""
263305
cmd = list(AssistantCLI._BASH_SCRIPT_BASE) + [f"# Rendering: {folder}"]
264306
if not AssistantCLI.DRY_RUN:
265307
cmd += AssistantCLI._bash_download_data(folder)
@@ -295,6 +337,14 @@ def bash_render(folder: str) -> str:
295337

296338
@staticmethod
297339
def bash_test(folder: str) -> str:
340+
"""Prepare bash script for running tests of a particular notebook.
341+
342+
Args:
343+
folder: name/path to a folder with notebook files
344+
345+
Returns:
346+
string with nash script content
347+
"""
298348
cmd = list(AssistantCLI._BASH_SCRIPT_BASE) + [f"# Testing: {folder}"]
299349
cmd += AssistantCLI._bash_download_data(folder)
300350
ipynb_file, _, _ = AssistantCLI._valid_folder(folder, ext=".ipynb")
@@ -337,7 +387,7 @@ def augment_script(folder: str) -> None:
337387
meta["description"] = meta["description"].replace(os.linesep, f"{os.linesep}# ")
338388

339389
header = TEMPLATE_HEADER % meta
340-
requires = set(default_requirements() + meta["requirements"])
390+
requires = set(load_requirements() + meta["requirements"])
341391
setup = TEMPLATE_SETUP % dict(requirements=" ".join([f'"{req}"' for req in requires]))
342392
py_script = [header + setup] + py_script + [TEMPLATE_FOOTER]
343393

@@ -350,7 +400,8 @@ def augment_script(folder: str) -> None:
350400

351401
@staticmethod
352402
def _replace_images(lines: list, local_dir: str) -> list:
353-
"""Update images by URL to GitHub raw source
403+
"""Update images by URL to GitHub raw source.
404+
354405
Args:
355406
lines: string lines from python script
356407
local_dir: relative path to the folder with script
@@ -381,6 +432,7 @@ def _replace_images(lines: list, local_dir: str) -> list:
381432

382433
@staticmethod
383434
def _is_ipynb_parent_dir(dir_path: str) -> bool:
435+
"""Determine in recursive fasion of a folder is valid notebook file or any of sub-folders is."""
384436
if AssistantCLI._find_meta(dir_path):
385437
return True
386438
sub_dirs = [d for d in glob.glob(os.path.join(dir_path, "*")) if os.path.isdir(d)]
@@ -395,7 +447,8 @@ def group_folders(
395447
strict: bool = True,
396448
root_path: str = "",
397449
) -> None:
398-
"""Group changes by folders
450+
"""Group changes by folders.
451+
399452
Args:
400453
fpath_gitdiff: raw git changes
401454
@@ -557,7 +610,8 @@ def copy_notebooks(
557610

558611
@staticmethod
559612
def update_env_details(dir_path: str):
560-
"""Export the actual packages used in runtime
613+
"""Export the actual packages used in runtime.
614+
561615
Args:
562616
dir_path: path to the folder
563617
"""

0 commit comments

Comments
 (0)