forked from beeware/batavia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_stdlib.py
130 lines (112 loc) · 4.12 KB
/
compile_stdlib.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
#!/usr/bin/env python3
"""
Convert ouroboros modules to JavaScripted pyc files that we can load.
"""
# TODO: we should just import ourboros and generate the pycs from there
import argparse
import base64
import glob
import os
import os.path
import py_compile
import sys
import tempfile
IGNORE_MODULES = set([
'__builtins__',
'__init__',
'plat-aix4',
'plat-darwin',
'plat-freebsd4',
'plat-freebsd5',
'plat-freebsd6',
'plat-freebsd7',
'plat-freebsd8',
'plat-generic',
'plat-linux',
'plat-netbsd1',
'plat-next3',
'plat-sunos5',
'plat-unixware7',
'site-packages',
'tkinter',
'turtle',
'turtledemo',
])
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('modules', metavar='module', nargs='*',
help='source modules to compile')
parser.add_argument('--source', help='location of the ouroboros source files')
args = parser.parse_args()
# find the ouroboros directory
if os.path.exists('./node_modules/@pybee/ouroboros'):
ouroboros = './node_modules/@pybee/ouroboros'
else:
exit("Please install the development dependencies with `npm install`.")
if args.modules:
enabled_modules = args.modules
else:
native_modules = []
native_path = os.path.join(os.path.dirname(__file__), 'batavia', 'modules')
for name in os.listdir(native_path):
module_name, ext = os.path.splitext(name)
if ext == '.js':
native_modules.append(module_name)
enabled_modules = []
for name in os.listdir(os.path.join(ouroboros, 'ouroboros')):
module_name, ext = os.path.splitext(name)
if (ext == '.py' or
ext == '' and os.path.isdir(os.path.join(ouroboros, name))):
if (module_name not in IGNORE_MODULES
and module_name not in native_modules):
enabled_modules.append(module_name)
return ouroboros, enabled_modules
def convert_to_pyc(stdlibPath, module_path):
if os.path.isfile(module_path):
fp = tempfile.NamedTemporaryFile(delete=False)
fp.close()
try:
py_compile.compile(module_path, cfile=fp.name)
with open(fp.name, 'rb') as fin:
pyc=fin.read()
finally:
os.unlink(fp.name)
with open(stdlibPath, 'w') as fout:
fout.write("module.exports = '" + base64.b64encode(pyc).decode('utf8') + "'\n")
else:
if not os.path.exists(stdlibPath):
os.mkdir(stdlibPath)
for content in os.listdir(module_path):
contentPath = os.path.join(module_path, content)
if os.path.isdir(contentPath):
convert_to_pyc(os.path.join(stdlibPath, content), contentPath)
else:
convert_to_pyc(os.path.join(stdlibPath, os.path.splitext(content)[0] + '.js'), contentPath)
def compile_stdlib(ouroboros, enabled_modules):
for module in enabled_modules:
module_fname = os.path.join(ouroboros, 'ouroboros', module + '.py')
module_dirname = os.path.join(ouroboros, 'ouroboros', module)
fbool = os.path.isfile(module_fname)
dirbool = os.path.isdir(module_dirname)
if not (fbool or dirbool):
exit("Could not find file or directory for module " + module)
else:
if fbool:
convert_to_pyc(os.path.join('batavia', 'stdlib', module + '.js'), module_fname)
else:
convert_to_pyc(os.path.join('batavia', 'stdlib', module), module_dirname)
outfile = os.path.join('batavia', 'stdlib.js')
print("Compiling stdlib index %s" % outfile)
with open(outfile, 'w') as fout:
fout.write("module.exports = {\n ")
module_list = [
"'" + module + "': require('./stdlib/" + module + "')"
for module in enabled_modules
]
fout.write(',\n '.join(module_list))
fout.write("\n}\n")
def main():
ouroboros, enabled_modules = parse_args()
compile_stdlib(ouroboros, enabled_modules)
if __name__ == '__main__':
main()