/
setup.py
executable file
·121 lines (80 loc) · 2.98 KB
/
setup.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
#!/usr/bin/env python
import os
import glob
from setuptools import setup, Command
class BuildNotes(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import os
import shutil
import tempfile
from IPython.nbconvert.nbconvertapp import NbConvertApp
import sys
for arg in range(len(sys.argv[1:])):
sys.argv.pop(-1)
# Convert the notebooks to HTML notebooks.
app = NbConvertApp()
app.initialize()
app.export_format = 'html'
for notebook in (glob.glob('notebooks/*.ipynb')):
app.notebooks = [notebook]
app.output_base = notebook.replace('.ipynb', '')
app.start()
# Make an index of all notes
f = open('notebooks.html', 'w')
f.write("<html>\n <body>\n")
f.write(" <h1>Solutions:</h1>\n")
f.write(" <ul>\n")
for page in glob.glob('notebooks/*.html'):
f.write(' <li><a href="{0}">{1}</a></li>\n'.format(page, os.path.basename(page).replace('.html', '')))
f.write(' </ul>\n')
f.write(' </body>\n</html>')
f.close()
class DeployNotes(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
SERVER = os.environ["PY4SCI_SERVER"]
USER = os.environ["PY4SCI_USER"]
import getpass
from ftplib import FTP
from astropy.utils.console import ProgressBar
ftp = FTP(SERVER)
ftp.login(user=USER, passwd=getpass.getpass())
ftp.cwd('/public_html/astropy4herts')
for slides in ProgressBar.iterate(glob.glob('notebooks/data/*')
+ glob.glob('notebooks/*.html')):
try:
remote_size = ftp.size(slides)
except:
remote_size = None
local_size = os.path.getsize(slides)
if local_size != remote_size:
ftp.storbinary('STOR ' + slides, open(slides, 'rb'))
ftp.storbinary('STOR notebooks.html', open('notebooks.html', 'rb'))
ftp.quit()
class RunNotes(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Now convert the lecture notes, problem sets, and practice problems to
# HTML notebooks.
from runipy.notebook_runner import NotebookRunner
start_dir = os.path.abspath('.')
for notebook in (glob.glob('notebooks/*.ipynb')):
os.chdir(os.path.dirname(notebook))
r = NotebookRunner(os.path.basename(notebook), pylab=True)
r.run_notebook(skip_exceptions=True)
r.save_notebook(os.path.basename(notebook))
os.chdir(start_dir)
setup(name='astropy4herts', cmdclass={'run':RunNotes, 'build': BuildNotes, 'deploy':DeployNotes})