-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.py
executable file
·216 lines (142 loc) · 5.65 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
import os
import glob
import shutil
from setuptools import setup, Command
import string
def strip_punctuation(text):
return ''.join(ch for ch in text if ch not in string.punctuation)
class BuildTOC(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from IPython.nbformat.current import read, write
with open('template.rst', 'r') as f:
template = f.read()
toc = ""
from urllib import quote
for notebook in (glob.glob('lectures/*.ipynb')):
with open(notebook, 'r') as f:
nb = read(f, 'json')
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'heading':
if cell['level'] > 1:
continue
toc += (" " * (cell['level'] - 1) +
"* `{0} <_static/{1}.html#{2}>`__\n".format(cell['source'].replace('`', ''),
quote(os.path.basename(notebook)).replace('.ipynb', ''),
strip_punctuation(cell['source']).replace(' ', '-')))
with open('www/index.rst', 'w') as f:
f.write(template.format(lectures_toc=toc))
class ClearOutput(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from nbformat import read, write
for notebook in glob.glob('?.???/*.ipynb'):
with open(notebook, 'r') as f:
nb = read(f, 4)
for cell in nb['cells']:
if cell.cell_type == 'code':
cell.outputs = []
if 'prompt_number' in cell:
cell.pop('prompt_number')
with open(notebook, 'w') as f:
write(nb, f)
class BuildNotes(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
import os
import sys
from IPython.nbconvert.nbconvertapp import NbConvertApp
self.reinitialize_command('run', inplace=True)
self.run_command('run')
for arg in range(len(sys.argv[1:])):
sys.argv.pop(-1)
if not os.path.exists(os.path.join('www', '_static')):
os.mkdir(os.path.join('www', '_static'))
# Now convert the lecture notes, problem sets, and practice problems to
# HTML notebooks.
app = NbConvertApp()
app.initialize()
app.export_format = 'html'
for notebook in glob.glob('?.???/*.ipynb'):
print("Rendering {0}...".format(notebook))
app.notebooks = [notebook]
app.output_base = os.path.join('..', 'www', '_static', os.path.basename(notebook.replace('.ipynb', '')))
app.start()
data_dir = os.path.join('www', '_static', 'data')
if not os.path.exists(data_dir):
os.mkdir(data_dir)
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())
for root, dirnames, filenames in os.walk('www/_build/html/'):
print("Uploading files from {0}".format(root))
ftp.cwd('/public_html/PY4SCI_WS_2015_16')
for directory in root.split('/')[3:]:
# Try and change to directory, make if not present
try:
ftp.cwd(directory)
except:
ftp.mkd(directory)
ftp.cwd(directory)
for filename in ProgressBar(filenames):
local_file = os.path.join(root, filename)
try:
remote_size = ftp.size(filename)
except:
remote_size = None
local_size = os.path.getsize(local_file)
if local_size != remote_size:
ftp.storbinary('STOR ' + filename, open(local_file, '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
from IPython.nbformat.current import read, write
start_dir = os.path.abspath('.')
for notebook in glob.glob('?.???/*.ipynb'):
print("Running {0}...".format(notebook))
os.chdir(os.path.dirname(notebook))
with open(os.path.basename(notebook)) as f:
r = NotebookRunner(read(f, 'json'), pylab=False)
r.run_notebook(skip_exceptions=True)
with open(os.path.basename(notebook), 'w') as f:
write(r.nb, f, 'json')
os.chdir(start_dir)
r.shutdown_kernel()
setup(name='py4sci', cmdclass={'run': RunNotes,
'build': BuildNotes,
'deploy': DeployNotes,
'clear': ClearOutput,
'toc': BuildTOC})