-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_all_scripts.py
executable file
·70 lines (57 loc) · 2.54 KB
/
run_all_scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Run all examples, tests, modules and notebooks to see if they execute without errors.
"""
import os
import subprocess
# Directories containing the examples and tests
directories = ["examples", "tests", "invariants_py", "notebooks"]
#directories = ["notebooks"]
# Initialize an empty list to hold examples and tests that fail to execute
failed_scripts = []
# Set the matplotlib backend to Agg in the environment variable to prevent showing plots
my_env = os.environ.copy()
my_env["MPLBACKEND"] = "agg"
# Get the directory containing this script
script_dir = os.path.dirname(os.path.realpath(__file__))
# Function to run scripts in a directory
def run_scripts(directory):
failed_scripts_local = []
directory_path = os.path.join(script_dir, directory)
for root, dirs, files in os.walk(directory_path):
# Get a list of all Python scripts in the current directory
scripts = [f for f in files if f.endswith('.py') or f.endswith('.ipynb')]
# Iterate over each script
for script in scripts:
# Construct the full script path
script_path = os.path.join(root, script)
# Print the name of the script being run
print(f"Running script: {script_path}")
# Try to execute the script
try:
if script.endswith('.py'):
subprocess.check_output(["python", script_path], env=my_env)
elif script.endswith('.ipynb'):
subprocess.check_output(["jupyter", "nbconvert", "--execute", "--to", "notebook", script_path], env=my_env)
# Delete the generated notebook
os.remove(os.path.splitext(script_path)[0] + '.nbconvert.ipynb')
except subprocess.CalledProcessError:
# If an error occurs, add the script to the list of failed scripts
failed_scripts_local.append(script_path)
return failed_scripts_local
# Run scripts for each directory
for directory in directories:
failed_scripts.extend(run_scripts(directory))
# Print out the scripts that failed to execute
print("")
print("========================================")
if failed_scripts:
print("The following scripts failed to execute:")
for script in failed_scripts:
print(script)
print("========================================")
print("")
raise Exception('One or more scripts failed, check the above output to see which ones.')
else:
print("All scripts executed successfully.")
print("========================================")
print("")