-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyc.py
executable file
·74 lines (59 loc) · 1.6 KB
/
cyc.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
#!/usr/bin/env python
# coding: utf-8
"""
Cython-based compiler
"""
# Python 2.7 Standard Library
import contextlib
import os.path
import sys
import shutil
import tempfile
# Third-Party Libraries
import pbs
import script
# Metadata
__author__ = u"Sébastien Boisgérault <Sebastien.Boisgerault@mines-paristech.fr>"
__license__ = "MIT License"
__url__ = "https://github.com/boisgera/cyc"
__version__ = None
@contextlib.contextmanager
def temp_dir():
dir = tempfile.mkdtemp()
try:
yield dir
finally:
shutil.rmtree(dir)
CFLAGS = str(pbs.pkg_config("python", cflags=True)).strip()
LIBS = str(pbs.pkg_config("python", libs=True)).strip()
def compile(filename, main=False, build_dir=""):
name = ".".join(filename.split(".")[:-1])
basename = os.path.basename(name)
cname = os.path.join(build_dir, basename + ".c")
pbs.cython(filename, "--embed", o=cname)
if main:
args = [CFLAGS]
args.append(["-o", name, cname])
args.append(LIBS)
else:
args = ["-shared", "-pthread", "-fPIC", CFLAGS]
args.append(["-o", name + ".so", cname])
pbs.gcc(*args)
def main(args=None):
args = args or sys.argv[1:]
spec = "help main"
options, filenames = script.parse(spec, args)
if options.help or not filenames:
print help()
else:
main = bool(options.main)
with temp_dir() as build_dir:
for filename in filenames:
compile(filename, main=main, build_dir=build_dir)
def help():
"""usage:
cyc [-m/--main] [FILENAMES]
"""
return help.__doc__
if __name__ == "__main__":
main()