2013-01-05 12:17:52 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2013-02-28 19:07:05 +01:00
|
|
|
import os
|
2013-01-05 12:17:52 +01:00
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
|
2013-02-28 19:07:05 +01:00
|
|
|
# resolve cython executable
|
|
|
|
cython = None
|
|
|
|
|
|
|
|
def resolve_cython():
|
|
|
|
global cython
|
|
|
|
for executable in ('cython', 'cython-2.7'):
|
|
|
|
for path in os.environ['PATH'].split(':'):
|
2013-05-07 08:48:16 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
continue
|
2013-02-28 19:07:05 +01:00
|
|
|
if executable in os.listdir(path):
|
|
|
|
cython = os.path.join(path, executable)
|
|
|
|
return
|
|
|
|
|
2013-01-05 12:17:52 +01:00
|
|
|
def do(fn):
|
2014-02-03 06:53:30 +01:00
|
|
|
print('cythonize:', fn)
|
2017-04-14 02:39:07 +02:00
|
|
|
assert(fn.endswith('.pyx'))
|
2013-01-05 12:17:52 +01:00
|
|
|
parts = fn.split('/')
|
|
|
|
if parts[0] == '.':
|
|
|
|
parts.pop(0)
|
|
|
|
modname = parts[-1][:-4]
|
|
|
|
package = '_'.join(parts[:-1])
|
|
|
|
|
|
|
|
# cythonize
|
2013-10-22 13:29:45 +02:00
|
|
|
subprocess.Popen([cython, fn], env=os.environ).communicate()
|
2013-01-05 12:17:52 +01:00
|
|
|
|
|
|
|
if not package:
|
2014-02-03 06:53:30 +01:00
|
|
|
print('no need to rewrite', fn)
|
2013-01-05 12:17:52 +01:00
|
|
|
else:
|
|
|
|
# get the .c, and change the initXXX
|
|
|
|
fn_c = fn[:-3] + 'c'
|
|
|
|
with open(fn_c) as fd:
|
|
|
|
data = fd.read()
|
2013-01-23 02:03:22 +01:00
|
|
|
modname = modname.split('.')[-1]
|
2017-04-14 02:39:07 +02:00
|
|
|
pac_mod = '{}_{}'.format(package, modname)
|
|
|
|
fmts = ('init{}(void)', 'PyInit_{}(void)', 'Pyx_NAMESTR("{}")', '"{}"')
|
|
|
|
for i, fmt in enumerate(fmts):
|
|
|
|
pat = fmt.format(modname)
|
|
|
|
sub = fmt.format(pac_mod)
|
|
|
|
print('{}: {} -> {}'.format(i + 1, pat, sub))
|
2017-04-14 02:41:48 +02:00
|
|
|
data = data.replace(pat, sub)
|
2014-02-03 06:53:30 +01:00
|
|
|
print('rewrite', fn_c)
|
2013-01-05 12:17:52 +01:00
|
|
|
with open(fn_c, 'w') as fd:
|
|
|
|
fd.write(data)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-02-03 06:53:30 +01:00
|
|
|
print('-- cythonize', sys.argv)
|
2013-02-28 19:07:05 +01:00
|
|
|
resolve_cython()
|
2013-01-05 12:17:52 +01:00
|
|
|
for fn in sys.argv[1:]:
|
|
|
|
do(fn)
|