11import argparse
22import copy
33import multiprocess
4+ import os
5+ import shutil
46import string
7+ import subprocess
58import textwrap
69from functools import partial
710from pathlib import Path
1316NOTEBOOKS_PATH = "../notebooks"
1417INSTALL_RELEASE_VERSION_COMMAND = "! pip install podium-nlp"
1518INSTALL_SOURCE_VERSION_COMMAND = "# ! pip install git+https://github.com/TakeLab/podium.git"
19+ INSTALL_SST_COMMAND = "python -c \" from podium.datasets import SST; SST.get_dataset_splits()\" "
1620TRANS_TABLE = str .maketrans (dict .fromkeys (string .whitespace ))
1721
1822
23+ def inject_sst ():
24+ delim = "&" if os .name == "nt" else ";"
25+ subprocess .call (
26+ delim .join ([INSTALL_SOURCE_VERSION_COMMAND [4 :], INSTALL_SST_COMMAND ]),
27+ shell = True ,
28+ cwd = Path (NOTEBOOKS_PATH ).absolute (),
29+ stdout = subprocess .DEVNULL ,
30+ stderr = subprocess .STDOUT ,
31+ )
32+
33+
34+ def cleanup (snap_before_exec , snap_after_exec ):
35+ created_paths = set (snap_after_exec ) - set (snap_before_exec )
36+ for path in created_paths :
37+ if path .is_dir ():
38+ shutil .rmtree (path )
39+ else :
40+ path .unlink ()
41+
42+
1943def print_notebook_name_with_error (func ):
2044 def wrapper (* args , ** kwargs ):
2145 if args :
@@ -101,6 +125,7 @@ def check_notebook_output(notebook_path, env="python3", ignore_whitespace=False)
101125 parser .add_argument ("--env" , default = "python3" , help = "kernel that executes the notebook" )
102126 parser .add_argument ("--num_proc" , help = "number of processes for parallel execution" )
103127 parser .add_argument ("--ignore_whitespace" , action = "store_true" , help = "ignore whitespace when comparing cell outputs" )
128+ parser .add_argument ("--keep_artifacts" , action = "store_true" , help = "save files/directories created during execution" )
104129 args = parser .parse_args ()
105130
106131 if args .num_proc is None :
@@ -116,16 +141,24 @@ def check_notebook_output(notebook_path, env="python3", ignore_whitespace=False)
116141 if not notebook_path .name .endswith ("-checkpoint.ipynb" )
117142 ]
118143
144+ snap_before_exec = list (Path (NOTEBOOKS_PATH ).iterdir ())
145+
119146 num_proc = min (min (num_proc , multiprocess .cpu_count ()), len (notebook_paths ))
120147 if num_proc == 1 :
121148 reports = []
122149 for notebook_path in notebook_paths :
123150 report = check_notebook_output (notebook_path , env = args .env , ignore_whitespace = args .ignore_whitespace )
124151 reports .append (report )
125152 else :
153+ # inject the SST dataset to prevent parallel download
154+ inject_sst ()
126155 with multiprocess .Pool (num_proc ) as pool :
127156 reports = pool .map (partial (check_notebook_output , env = args .env , ignore_whitespace = args .ignore_whitespace ), notebook_paths )
128157
158+ if args .keep_artefacts is False :
159+ snap_after_exec = list (Path (NOTEBOOKS_PATH ).iterdir ())
160+ cleanup (snap_before_exec , snap_after_exec )
161+
129162 if any (report for _ , report in reports ):
130163 reports_str = "\n \n " .join ([
131164 f"In notebook { notebook } :\n " + textwrap .indent (
0 commit comments