forked from morecobol/stingrayreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·303 lines (277 loc) · 8.95 KB
/
build.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
# .. _`build`:
#
# #########################
# Stingray Build
# #########################
#
# The source for :py:mod:`stingray` is a Sphinx project that depends on PyLit.
# Yes. The documentation spawns the code.
#
# .. note:: PyLit Feature
#
# The first line of each file should be ``.. #!/usr/bin/env python3``.
#
# The four spaces between ``..`` and ``#!`` defines the indent used for
# each line of code in the file.
#
# Four spaces.
#
# Stingray depends on the following
#
# - xlrd. http://www.lexicon.net/sjmachin/xlrd.htm
#
# Version 0.9.2 is Python 3 compatible. https://pypi.python.org/pypi/xlrd/0.9.2
#
# In addition to Python 3.3, there are two other projects used to build.
#
# - PyLit. https://github.com/slott56/PyLit-3
#
# - Sphinx. http://sphinx.pocoo.org/
#
# The PyLit install is little more than a download and move the :file:`pylit.py` file to
# the Python :file:`site-packages` directory.
#
# Sphinx and XLRD should be
# installed with `easy_install <http://peak.telecommunity.com/DevCenter/EasyInstall>`_.
#
# .. code-block:: bash
#
# easy_install xlrd
# easy_install sphinx
#
# In the case of having Python2 and Python3 installed, ``easy_install-3.3`` may be required.
# On most systems, ``sudo`` is also required.
#
# The diagrams are done with YUML. See http://yuml.me.
#
# Build Procedure
# ==================
#
# 1. Bootstrap the :file:`build.py` script by running PyLit.
#
# .. code-block:: bash
#
# python3 -m pylit -t source/build.rst build.py
#
# This reports that an extract was written to :file:`build.py`.
#
# 2. Use the :file:`build.py` script to create the ``stingray`` source, unit
# tests, demonstration applications.
# Build the Sphinx documentation.
# And run the unit test, too.
#
# .. code-block:: bash
#
# python3 build.py
#
# At the end of this step, the directory tree will include the following.
#
# - :file:`build`. The documentation. In HTML.
# - :file:`demo`. Some demonstration applications that use ``stingray``.
# See :ref:`demo`.
# - :file:`stingray`. The Python library, ready for installation.
# - :file:`test`. The unit test script.
#
# This reports, also, that 174 tests were run.
#
# In general (i.e., any OS except Windows), it's sensible to do this:
#
# .. code-block:: bash
#
# chmod +x build.py
#
# This allows us to use the following for a rebuild:
#
# .. code-block:: bash
#
# ./build.py
#
#
# Build Script Design
# =====================
#
# This is a platform-independent :file:`build.py` file for the build script.
# This can use ``from sphinx.application import Sphinx``
# and ``import pylit`` to access these modules from within Python
# instead of using command-line shell script techniques.
#
# Overheads
# -------------
#
# We're going to make use of three "applications".
#
# - Sphinx top-level application.
#
# - PyLit top-level application.
#
# - Unittest top-level test runner.
#
# ::
"""Platform-independent build script"""
from __future__ import print_function
import os
import sys
import errno
from sphinx.application import Sphinx
import pylit
import unittest
import logging
import shutil
# Sphinx Build
# ---------------
#
# .. py:function:: sphinx_build( srcdir, outdir, buildername='html' )
#
# Handle the simple use case for the ``sphinx-build`` script.
#
# ::
def sphinx_build( srcdir, outdir, buildername='html' ):
"""Essentially: ``sphinx-build $* -b html source build/html``"""
confdir= srcdir= os.path.abspath( srcdir )
outdir= os.path.abspath( outdir )
doctreedir = os.path.join(outdir, '.doctrees')
app = Sphinx(srcdir, confdir, outdir, doctreedir, buildername)
app.build(force_all=False, filenames=[])
return app.statuscode
# PyLit Build
# ---------------
#
# .. py:function:: pylit_build( srcdir, outdir )
#
# Handle the simple use case for PyLit.
#
# This also handles the necessary rewrite to modify standard paths to Windows paths.
#
# ::
def pylit_build( infile, outfile ):
"""Essentially: ``python3 -m pylit -t source/demo/data_quality.rst demo/test.py``
The issue here is that we need to provide platform-specific paths.
"""
if os.sep != '/':
# Fix windows paths.
infile= os.path.join( *infile.split('/') )
outfile= os.path.join( *outfile.split('/') )
pylit.main( txt2code= True, overwrite="yes", infile= infile, outfile= outfile )
# Make Directories
# -------------------
#
# .. py:function:: mkdir( path )
#
# Handles the simple use case for assuring that the directory
# tree exists.
#
# This also handles a rewrite to modify standard paths to Windows paths.
#
# ::
def mkdir( path ):
if os.sep != '/':
# Fix windows paths.
path= os.path.join( *path.split('/') )
try:
os.makedirs( path )
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
# Copy Data File
# ---------------
#
# .. py:function:: copy_file( srcdir, outdir )
#
# Handles the simple use case for copying a file.
#
# ::
def copy_file( srcdir, outdir ):
"""Essentially: ``cp srcdir outdir``"""
shutil.copy2( srcdir, outdir )
# Run the Test Script
# -----------------------
#
# .. py:function:: run_test( )
#
# In effect, this does ``python3 test/main.py``
#
# ::
def run_test():
import test.main
result= test.main.main()
if result.failures:
sys.exit(result.failures)
# The Build Sequence
# ---------------------
#
# ::
def build():
mkdir( 'stingray/schema' )
mkdir( 'stingray/cobol' )
mkdir( 'stingray/workbook' )
pylit_build( 'source/stingray_init.rst', 'stingray/__init__.py' )
pylit_build( 'source/cell.rst', 'stingray/cell.py' )
pylit_build( 'source/sheet.rst', 'stingray/sheet.py' )
pylit_build( 'source/workbook/init.rst', 'stingray/workbook/__init__.py' )
pylit_build( 'source/workbook/base.rst', 'stingray/workbook/base.py' )
pylit_build( 'source/workbook/csv.rst', 'stingray/workbook/csv.py' )
pylit_build( 'source/workbook/xls.rst', 'stingray/workbook/xls.py' )
pylit_build( 'source/workbook/xlsx.rst', 'stingray/workbook/xlsx.py' )
pylit_build( 'source/workbook/ods.rst', 'stingray/workbook/ods.py' )
pylit_build( 'source/workbook/numbers_09.rst', 'stingray/workbook/numbers_09.py' )
pylit_build( 'source/workbook/numbers_13.rst', 'stingray/workbook/numbers_13.py' )
pylit_build( 'source/workbook/fixed.rst', 'stingray/workbook/fixed.py' )
pylit_build( 'source/schema.rst', 'stingray/schema/__init__.py' )
pylit_build( 'source/schema_loader.rst', 'stingray/schema/loader.py' )
pylit_build( 'source/cobol_init.rst', 'stingray/cobol/__init__.py' )
pylit_build( 'source/cobol_loader.rst', 'stingray/cobol/loader.py' )
pylit_build( 'source/cobol_defs.rst', 'stingray/cobol/defs.py' )
pylit_build( 'source/snappy.rst', 'stingray/snappy.py' )
pylit_build( 'source/protobuf.rst', 'stingray/protobuf.py' )
pylit_build( 'source/installation.rst', 'setup.py' )
copy_file( 'source/Numbers.json', 'stingray/Numbers.json' )
copy_file( 'source/Common.json', 'stingray/Common.json' )
mkdir( 'test' )
pylit_build( 'source/testing/test_init.rst', 'test/__init__.py' )
pylit_build( 'source/testing/main.rst', 'test/main.py' )
pylit_build( 'source/testing/cell.rst', 'test/cell.py' )
pylit_build( 'source/testing/sheet.rst', 'test/sheet.py' )
pylit_build( 'source/testing/schema.rst', 'test/schema.py' )
pylit_build( 'source/testing/schema_loader.rst', 'test/schema_loader.py' )
pylit_build( 'source/testing/workbook.rst', 'test/workbook.py' )
pylit_build( 'source/testing/cobol.rst', 'test/cobol.py' )
pylit_build( 'source/testing/cobol_loader.rst', 'test/cobol_loader.py' )
pylit_build( 'source/testing/cobol_2.rst', 'test/cobol_2.py' )
pylit_build( 'source/testing/snappy_protobuf.rst', 'test/snappy_protobuf.py' )
mkdir( 'demo' )
pylit_build( 'source/demo/data_quality.rst', 'demo/test.py' )
pylit_build( 'source/demo/validation.rst', 'demo/app.py' )
pylit_build( 'source/demo/profile.rst', 'demo/profile.py' )
pylit_build( 'source/demo/cobol_reader.rst', 'demo/cobol_reader.py' )
run_test()
sphinx_build( 'source', 'build/html', 'html' )
sphinx_build( 'source', 'build/latex', 'latex' )
# Main Program Switch
# ---------------------
#
# When the :file:`build.py` script is run from the command line,
# it will execute the :py:func:`build` function. When it is imported,
# however, it will do nothing special.
#
# ::
if __name__ == "__main__":
build()
# Additional Builds
# =====================
#
# Sometimes it's desriable to refresh the documentation.
#
# The HTML pages are built with this command.
#
# .. code-block:: bash
#
# sphinx-build $* -b html source build/html
#
# The LaTeX document is built with this command.
#
# .. code-block:: bash
#
# sphinx-build $* -b latex source build/latex