forked from gammapy/gammapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_notebooks.py
99 lines (79 loc) · 2.8 KB
/
test_notebooks.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
#!/usr/bin/env python
"""
Test if IPython notebooks work.
"""
import os
import sys
import subprocess
import logging
from pprint import pprint
import yaml
logging.basicConfig(level=logging.INFO)
status_codes = dict(
success=0,
error_nb_failed=1,
error_no_gammapy_extra=2,
)
if 'GAMMAPY_EXTRA' not in os.environ:
logging.info('GAMMAPY_EXTRA environment variable not set.')
logging.info('Running notebook tests requires gammapy-extra.')
logging.info('Exiting now.')
sys.exit(status_codes['error_no_gammapy_extra'])
status_code = status_codes['success']
def get_notebooks():
"""Read `notebooks.yaml` info."""
filename = os.environ['GAMMAPY_EXTRA'] + '/notebooks/notebooks.yaml'
logging.info('')
with open(filename) as fh:
notebooks = yaml.safe_load(fh)
return notebooks
def requirement_missing(notebook):
"""Check if one of the requirements is missing."""
if notebook['requires'] is None:
return False
for package in notebook['requires'].split():
try:
__import__(package)
except ImportError:
logging.warning('Skipping notebook {} because dependency {} is missing.'
''.format(notebook['name'], package))
return True
return False
notebooks = get_notebooks()
pprint(notebooks)
logging.info('Python executable: {}'.format(sys.executable))
logging.info('Python version: {}'.format(sys.version))
logging.info('Testing IPython notebooks ...')
for notebook in notebooks:
if not notebook['test']:
logging.info('Skipping notebook {} because test=false.'.format(notebook['name']))
continue
if requirement_missing(notebook):
continue
# For testing how `subprocess.Popen` works:
# cmd = 'pwd && echo "hi" && asdf'
cmd = 'which python; '
cmd += 'echo $GAMMAPY_EXTRA; '
cmd += 'pwd; '
# cmd += 'export GAMMAPY_EXTRA={}; '.format(os.environ['GAMMAPY_EXTRA'])
# cmd += 'cd $GAMMAPY_EXTRA/notebooks; '
cmd += 'runipy {}.ipynb'.format(notebook['name'])
logging.info('Executing: {}'.format(cmd))
proc = subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=os.environ.copy(),
cwd=os.environ['GAMMAPY_EXTRA'] + '/notebooks',
)
stdout, stderr = proc.communicate()
logging.info('Exit status code: {}'.format(proc.returncode))
logging.info('stdout:\n{}'.format(stdout.decode('utf8')))
logging.info('stderr:\n{}'.format(stderr.decode('utf8')))
if proc.returncode != status_codes['success']:
status_code = status_codes['error_nb_failed']
logging.info('... finished testing IPython notebooks.')
logging.info('Total exit status code of test_notebook.py is: {}'.format(status_code))
sys.exit(status_code)