-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_test.py
182 lines (145 loc) · 5.87 KB
/
run_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# python script to update github repository of ArchPy and run the notebooks
import os
import sys
import subprocess
import shutil
"""
This script is used to run the notebooks in the examples folder of the ArchPy repository.
The script creates a new conda environment, installs the package and runs the notebooks.
The script can be run with the following arguments:
- env: to create the conda environment
- notebook: to run the notebooks
"""
env_name = "archpy"
package_name = "Geoarchpy"
# create a new conda environment and run setup.py
def create_env():
# Define the name of the new conda environment
# Create the new conda environment
subprocess.run(f"conda create --name {env_name} python=3.11 -yf", shell=True)
# Activate the new conda environment and install packages
subprocess.run(f"conda activate {env_name} && pip install .[all]", shell=True)
# function to run the notebooks and check if they run without errors
def run_notebooks():
# create a log file to store the output of the notebooks
with open("notebook_log.txt", "w") as f:
f.write("")
notebook_folder = "examples"
# Activate the new conda environment
alias = f"conda activate {env_name} && jupyter nbconvert --execute --to notebook --inplace"
# use an alias
# get list of all notebooks
notebooks_subfolders = ["01_basic", "02_3D_ArchPy", "04_hierarchies", "05_mps_surfaces", "06_cross_validation", "07_Geological_map", "10_rotation_grid"]
for folder in notebooks_subfolders:
for notebook in os.listdir(f"{notebook_folder}/{folder}"):
if notebook.endswith(".ipynb"):
print(f"\nRunning notebook: {notebook}")
# subprocess.run(f"{alias} {notebook_folder}/01_basic/{notebook}", shell=True)
output = subprocess.run(f"{alias} {notebook_folder}/{folder}/{notebook}", shell=True, capture_output=True)
if output.returncode != 0:
print("Error in notebook {}".format(notebook))
with open("notebook_log.txt", "a") as f:
f.write(f"###################################################\n")
f.write(f"Error in notebook: {notebook}\n")
f.write(output.stderr.decode("utf-8"))
# write yml file to store the environment
subprocess.run(f"conda env export --name {env_name} > env.yml", shell=True)
def run_script_notebooks():
"""
This function runs the script in sphinx folder to transfer the notebooks to the documentation folder
"""
subprocess.run("python sphinx_build/source/notebooks/script.py", shell=True)
def upload_to_pypi():
# remove previous build and egg-info folders
if os.path.exists(f"{package_name}.egg-info"):
shutil.rmtree(f"{package_name}.egg-info")
if os.path.exists("build"):
shutil.rmtree("build")
# install/upgrade build and twine
subprocess.run("pip install --upgrade build twine", shell=True)
# build the package
subprocess.run("python -m build", shell=True)
# upload to pypi
subprocess.run("twine upload dist/*", shell=True)
# remove build and egg-info folders
shutil.rmtree("Geoarchpy.egg-info")
shutil.rmtree("dist")
def upload_to_github():
# upload to github
subprocess.run("git add .", shell=True)
subprocess.run("git commit -m 'update'", shell=True)
subprocess.run("git push", shell=True)
def check_errors(flag):
with open("notebook_log.txt", "r") as f:
log = f.read()
if len(log) > 0:
flag = False
else:
print("All notebooks ran without errors")
return flag
def update_readme():
import subprocess
"""
Function to update the README.md file with the latest version of the used packaged
"""
with open("README.md", "r") as f:
readme = f.read()
list_packages = ["numpy", "pandas", "matplotlib", "scipy", "sklearn", "geopandas", "rasterio", "shapely", "pyvista", "yaml", "ipywidgets"]
import numpy
import pandas
import matplotlib
import scipy
import sklearn
import geopandas
import rasterio
import shapely
import pyvista
import yaml
import ipywidgets
for package in list_packages:
# find where the package is mentioned in the README
start = readme.find(f"{package}")
end = readme.find("\n", start)
# get the version of the package
version = eval(package).__version__
# replace the version
readme = readme[:start] + f"{package} (tested with {version})" + readme[end:]
with open("README.md", "w") as f:
f.write(readme)
##############################################################################
if __name__ == "__main__":
# clone the repository
flag = True
# depending on arguments, create the environment or run the notebooks
if len(sys.argv) > 1:
if "env" in sys.argv:
create_env()
if "notebook" in sys.argv:
run_notebooks()
run_script_notebooks()
# check if there are any errors in the notebooks
flag = check_errors(flag)
if "readme" in sys.argv:
update_readme()
if "pypi" in sys.argv:
check_errors(flag)
if flag:
upload_to_pypi()
# if "github" in sys.argv:
# upload_to_github()
else:
print("There are errors in the notebooks")
if "all" in sys.argv:
create_env()
run_notebooks()
run_script_notebooks()
check_errors(flag)
update_readme()
if flag:
run_script_notebooks()
upload_to_pypi()
# upload_to_github()
else:
print("There are errors in the notebooks")
else:
print("Please provide arguments to the script")