-
Notifications
You must be signed in to change notification settings - Fork 1
/
opalc.py
324 lines (255 loc) · 12 KB
/
opalc.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
MIT License
Copyright (c) 2020 thatsOven
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os, sys, shutil, numpy, subprocess
from timeit import default_timer
from pathlib import Path
from setuptools import setup
from Cython.Build import cythonize
from Cython.Compiler import Options
from components.Compiler import *
RELEASE_COLLECT = ["__future__", "typeguard", "pygame", "unittest", "numpy", "json"]
PY_STDLIB = set(sys.stdlib_module_names) - {"antigravity"} # fun, but i don't wanna open the xkcd page every time i compile something
NO_INSTALL = {"opal"} | set(RELEASE_COLLECT) | PY_STDLIB
def build(file, debug = False):
Options.annotate = debug
oldArgs = sys.argv
sys.argv = [sys.argv[0], "build_ext", "--inplace"]
try:
setup(
name = os.path.basename(file).split(".")[0],
include_dirs = [numpy.get_include()],
ext_modules = cythonize(file, compiler_directives = {
"language_level": "3"
}),
zip_safe = False
)
except Exception:
print(traceback.format_exc())
ok = False
else:
ok = True
sys.argv = oldArgs
return ok
def compileBase(compiler, filename, name, top, time):
compiler.compileToPYX(filename, f"{name}.pyx", top)
if compiler.hadError: return False
print("opal -> Cython: Done in " + str(round(default_timer() - time, 4)) + " seconds")
ok = build(f"{name}.pyx", debug)
if os.path.exists(f"{name}.pyx"): os.remove(f"{name}.pyx")
if os.path.exists(f"{name}.c"): os.remove(f"{name}.c")
return ok
def compilePy(compiler, filename, _, endName, top, time, _0):
compiler.compileToPY(filename, f"{endName}.py", top)
if compiler.hadError: return None
print("opal -> Python: Done in " + str(round(default_timer() - time, 4)) + " seconds")
return f"{endName}.py"
def compileNormal(compiler, fileInput, name, endName, top, time, noModule):
if compileBase(compiler, fileInput, name, top, time):
if os.path.exists("build"): shutil.rmtree("build")
if (not compiler.module) or noModule:
if len(sys.argv) == 3:
filename = f"{endName}.py"
else:
filename = sys.argv[3]
name = os.path.basename(filename).split(".")[0]
with open(filename, "w") as py:
py.write(f"from os import environ\nenviron['_OPAL_RUN_AS_MAIN_']=''\nimport {name}\ndel environ['_OPAL_RUN_AS_MAIN_']")
return filename
def compileOne(libs, file, compiler):
time = default_timer()
filename = os.path.join(libs, file)
name = file.split(".")[0]
compiler.preConsts["HOME_DIR"] = f'r"{libs}"'
top = 'new dynamic HOME_DIR=r"' + libs + '";'
return compileBase(compiler, filename, name, top, time)
def getHomeDirFromFile(file):
return str(Path(file).parent.absolute())
def release(fn):
time = default_timer()
if findDir:
drt = getHomeDirFromFile(sys.argv[2])
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'package pathlib:import Path;new dynamic HOME_DIR=str(Path(__file__).parent.absolute());del Path;'
from ianthe import Ianthe
ianthe = Ianthe(sys.argv[2])
try: _ = ianthe.config
except: quit()
name = os.path.basename(os.path.basename(ianthe.config["source"])).split(".")[0]
for char in ILLEGAL_CHARS:
name = name.replace(char, "_")
ianthe.config["source"] = os.path.abspath(ianthe.config["source"])
if "destination" in ianthe.config:
ianthe.config["destination"] = os.path.abspath(ianthe.config["destination"])
else: ianthe.config["destination"] = getHomeDirFromFile(ianthe.config["source"])
dst = ianthe.config["destination"]
curr = getHomeDirFromFile(__file__)
os.chdir(curr)
before = set(os.listdir(curr))
compiler.preConsts["RELEASE_MODE"] = "True"
filename = fn(compiler, ianthe.config["source"], "opal_program", name, top, time, True)
if filename is not None:
if "copy" in ianthe.config:
ianthe.config["copy"][os.path.abspath("opal.py")] = "file"
ianthe.config["copy"][os.path.abspath("libs")] = "folder"
else:
ianthe.config["copy"] = {
os.path.abspath("opal.py"): "file",
os.path.abspath("libs"): "folder"
}
if "hidden-imports" in ianthe.config:
ianthe.config["hidden-imports"] = list(set(ianthe.config["hidden-imports"]) | PY_STDLIB)
else: ianthe.config["hidden-imports"] = list(PY_STDLIB)
if "keep" in ianthe.config:
ianthe.config["keep"] = list(set(ianthe.config["keep"]) | PY_STDLIB)
else: ianthe.config["keep"] = list(PY_STDLIB)
if "collect" in ianthe.config:
if "all" in ianthe.config["collect"]:
ianthe.config["collect"]["all"] = list(set(ianthe.config["collect"]["all"] + RELEASE_COLLECT) | PY_STDLIB)
else: ianthe.config["collect"]["all"] = list(set(RELEASE_COLLECT) | PY_STDLIB)
else: ianthe.config["collect"] = {"all": list(set(RELEASE_COLLECT) | PY_STDLIB)}
if "icon" not in ianthe.config:
ianthe.config["icon"] = str(os.path.join(curr, "runner", "icon.ico"))
if fn is compilePy: ianthe.config["scan"] = False
ianthe.config["source"] = os.path.abspath(filename)
ianthe.execute()
for module in compiler.imports:
if module not in NO_INSTALL: subprocess.run([
sys.executable, "-m", "pip", "install", "--isolated", "--exists-action", "w",
f"--target={str(os.path.join(dst, name))}", module,
])
os.chdir(curr)
for file in set(os.listdir(curr)) - before:
os.chmod(file, 0o777)
os.remove(file)
print("Compilation finished. Elapsed time: " + str(round(default_timer() - time, 4)) + " seconds")
quit()
print("Compilation failed.")
if __name__ == "__main__":
if len(sys.argv) == 1:
print(f"opal compiler v{'.'.join([str(x) for x in VERSION])} - thatsOven")
else:
compiler = Compiler()
compiler.handleArgs(sys.argv)
compiler.preConsts["RELEASE_MODE"] = "False"
compiler.preConsts["OPAL_DIR"] = f"r'{getHomeDirFromFile(__file__)}'"
compiler.initMain()
if "--debug" in sys.argv:
debug = True
sys.argv.remove("--debug")
else: debug = False
if "--dir" in sys.argv:
findDir = False
idx = sys.argv.index("--dir")
sys.argv.pop(idx)
drt = sys.argv.pop(idx)
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'new dynamic HOME_DIR=r"' + drt + '";'
else:
findDir = True
if sys.argv[1] == "pyxcompile":
if len(sys.argv) == 2:
print('input file required for command "pyxcompile"')
sys.exit(1)
time = default_timer()
if findDir:
drt = getHomeDirFromFile(sys.argv[2])
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'new dynamic HOME_DIR=r"' + drt + '";'
name = os.path.basename(sys.argv[2]).split(".")[0]
if len(sys.argv) == 3:
compiler.compileToPYX(sys.argv[2], f"{name}.pyx", top)
else:
compiler.compileToPYX(sys.argv[2], sys.argv[3], top)
if not compiler.hadError:
print("Compilation was successful. Elapsed time: " + str(round(default_timer() - time, 4)) + " seconds")
elif sys.argv[1] == "pycompile":
if len(sys.argv) == 2:
print('input file required for command "pycompile"')
sys.exit(1)
time = default_timer()
if findDir:
drt = getHomeDirFromFile(sys.argv[2])
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'new dynamic HOME_DIR=r"' + drt + '";'
name = os.path.basename(sys.argv[2]).split(".")[0]
if len(sys.argv) == 3:
compiler.compileToPY(sys.argv[2], f"{name}.py", top)
else:
compiler.compileToPY(sys.argv[2], sys.argv[3], top)
if not compiler.hadError:
print("Compilation was successful. Elapsed time: " + str(round(default_timer() - time, 4)) + " seconds")
elif sys.argv[1] == "compile":
if len(sys.argv) == 2:
print('input file required for command "compile"')
sys.exit(1)
time = default_timer()
if findDir:
drt = getHomeDirFromFile(sys.argv[2])
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'new dynamic HOME_DIR=r"' + drt + '";'
name = os.path.basename(sys.argv[2]).split(".")[0]
for char in ILLEGAL_CHARS:
name = name.replace(char, "_")
if compileNormal(compiler, sys.argv[2], name, name, top, time, False):
print("Compilation was successful. Elapsed time: " + str(round(default_timer() - time, 4)) + " seconds")
quit()
print("Compilation failed")
elif sys.argv[1] == "build":
libs = os.path.join(getHomeDirFromFile(__file__), "libs")
os.chdir(libs)
ok = compileOne(libs, "std.opal", compiler)
if not ok:
print("Compilation failed")
quit()
for file in os.listdir(libs):
if file.endswith(".opal") and not file in ("helpers.opal", "std.opal"):
ok = compileOne(libs, file, compiler)
if not ok:
print("Compilation failed")
break
if os.path.exists("build"): shutil.rmtree("build")
if ok:
os.chdir(os.path.join(getHomeDirFromFile(__file__), "runner"))
import runner.build
elif sys.argv[1] == "release":
if len(sys.argv) == 2:
print('input file required for command "release"')
sys.exit(1)
release(compileNormal)
elif sys.argv[1] == "pyrelease":
if len(sys.argv) == 2:
print('input file required for command "pyrelease"')
sys.exit(1)
release(compilePy)
elif sys.argv[1] == "path":
print(getHomeDirFromFile(__file__))
else:
sys.argv[1] = sys.argv[1]
if not os.path.exists(sys.argv[1]):
print('unknown command or nonexistent file "' + sys.argv[1] + '"')
sys.exit(1)
if findDir:
drt = getHomeDirFromFile(sys.argv[1])
compiler.preConsts["HOME_DIR"] = f'r"{drt}"'
top = 'new dynamic HOME_DIR=r"' + drt + '";'
result = compiler.compileFile(sys.argv[1], top)
if not compiler.hadError:
sys.argv = sys.argv[1:]
exec(result)