forked from k-sys/covid-19
-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.py
72 lines (61 loc) · 2.14 KB
/
run.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
import subprocess
import argparse
import sys
import sentry_sdk
import nbformat
from datetime import datetime
from datetime import date
from nbconvert.preprocessors import ExecutePreprocessor
parser = argparse.ArgumentParser()
parser.add_argument("--force", action="store_true")
parser.add_argument("--sentry-url")
args = parser.parse_args()
flagForce = args.force
sentryUrl = args.sentry_url
now = datetime.now()
print ("Running on:", now.strftime("%Y-%m-%d %H:%M:%S"))
# sentry init
sentry_sdk.init(dsn=sentryUrl)
# Execution function
run_path = "./"
data_path = "Rt Data Preparation.ipynb"
model_path = "Rainier.ipynb"
def execute(path,kernel):
with open(f"{run_path}{path}") as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=14400, kernel_name=kernel)
try:
out = ep.preprocess(nb, {'metadata': {'path': run_path}})
except CellExecutionError:
msg = 'Error executing the notebook "%s".\n\n' % path
msg += 'See notebook "%s" for the traceback.' % f"executed{path}"
print(msg)
raise
finally:
with open(f"{run_path}executed_{path}", 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
try:
commandLatest = """git log --author="cronjob" -1 --date=short --pretty=format:%cd"""
cp = subprocess.run(commandLatest, shell=True, check=True, text=True, capture_output=True)
dateOfCommit = date.fromisoformat(cp.stdout)
if not flagForce:
print("Last data push on", dateOfCommit)
today = date.today()
if dateOfCommit == today and not flagForce:
print("Already pushed today!")
quit()
print("Execution on", today)
if flagForce:
print("Forcing!")
now = datetime.now()
# Running data preparation and model notebooks
execute(data_path,'python3')
execute(model_path,'rainier')
# Pushing everything to git repository
#commandConfig = """git config user.name "cronjob" && git config user.email [email protected]"""
commandExecution = f"""git commit --author="cronjob <[email protected]>" -a -m"Updating rt-rainier.csv on {now}" && git push"""
#subprocess.run(commandConfig, shell=True, check=True)
subprocess.run(commandExecution, shell=True, check=True)
except Exception as e:
sentry_sdk.capture_exception(e)
raise e