1+ #!/usr/bin/env python3
2+ import os , shutil , glob , sys , subprocess , re
3+ from os .path import join as j
4+
5+
6+ # update version numbers in pyreact.py and pyreact-js.js
7+ VERSION = "1.0.0"
8+
9+ # to build this, you need a `js-npm` folder where you've done the following:
10+ # npm install -g browserify
11+ # npm install -g uglify
12+ # npm install jsx-transform
13+ # pyreact-js.js should be in the directory
14+ # package.json should have: "main": "PyReact.js",
15+
16+ SROOT = 'src'
17+ SSTYLES = 'styles'
18+ SSCRIPTS = 'scripts'
19+ SSCRIPTS_PY = j ('scripts' , '__javascript__' )
20+ SSCRIPTS_EXT = 'scripts-ext'
21+
22+ DROOT = 'dist'
23+ DSTYLES = 'styles'
24+ DSCRIPTS = 'scripts'
25+
26+
27+ #########################
28+ ### Main program
29+
30+ def main ():
31+
32+ # dist folder
33+ print ('Initialize {} folder' .format (DROOT ))
34+ if os .path .exists (DROOT ):
35+ shutil .rmtree (DROOT )
36+ os .mkdir (DROOT )
37+ os .mkdir (j (DROOT , DSTYLES ))
38+ os .mkdir (j (DROOT , DSCRIPTS ))
39+
40+ # html files
41+ log ('Copy HTML' )
42+ for src in glob .glob (j (SROOT , '*.html' )):
43+ _ , dest = os .path .split (src )
44+ with open (src , 'r' ) as fin :
45+ with open (j (DROOT , dest ), 'w' ) as fout :
46+ for line in fin :
47+ line = re .sub (j (SSCRIPTS , '([^/]+)\.js' ), j (DSCRIPTS , '\\ 1.min.js' ), line )
48+ line = re .sub (j (SSCRIPTS_EXT , '([^/]+)\.js' ), j (DSCRIPTS , '\\ 1.min.js' ), line )
49+ line = re .sub (j (SSCRIPTS_PY , '([^/]+)\.js' ), j (DSCRIPTS , '\\ 1.min.js' ), line )
50+ fout .write (line )
51+
52+ # css files
53+ log ('Copy CSS' )
54+ for src in glob .glob (j (SROOT , SSTYLES , '*.css' )):
55+ shutil .copy (src , j (DROOT , DSTYLES ))
56+
57+ # js files
58+ log ('Copy JS' )
59+ for src in glob .glob (j (SROOT , SSCRIPTS , '*.js' )):
60+ shutil .copy (src , j (DROOT , DSCRIPTS ))
61+ for src in glob .glob (j (SROOT , SSCRIPTS_EXT , '*.min.js' )):
62+ shutil .copy (src , j (DROOT , DSCRIPTS ))
63+
64+ # transcrypt files
65+ log ('Transpile .py scripts' )
66+ cwd = os .getcwd ()
67+ os .chdir (SROOT )
68+ for src in glob .glob (j (SSCRIPTS , '*.py' )):
69+ run ('transcrypt --map --build --esv 6 {}' .format (src ))
70+ os .chdir (cwd )
71+ shutil .move (j (SROOT , SSCRIPTS_PY , 'index.min.js' ), j (DROOT , DSCRIPTS ))
72+ shutil .rmtree (j (SROOT , SSCRIPTS_PY ))
73+
74+
75+
76+
77+ #########################
78+ ### Helper functions
79+
80+ def log (msg ):
81+ print ()
82+ print ('=== {} ===' .format (msg ))
83+
84+ def run (cmd ):
85+ print ('\t ' + cmd )
86+ subprocess .run (cmd , shell = True , check = True )
87+
88+ def minext (fn ):
89+ filename , ext = os .path .splitext (os .path .split (fn )[1 ])
90+ return filename + '.min' + ext
91+
92+
93+
94+ #########################
95+ ### Start the program
96+
97+ if __name__ == '__main__' :
98+ main ()
0 commit comments