kivy-ios/tools/cythonize.py
Richard Larkin 64bd692632
Flake8 CI fixes (#451)
* Pep8 fixes

* tox Pep8 compliance

* Excluded external tools folder from flake 8 tests

* Added Flake 8 exclusions

* Pep8 fixes

* Pep8 fixes

* Corrected type

* Pep8 fixes

* Pep 8 compliance

* Pep8 fixes

* Pep8  fixes

* Pep8 fixes

* Pep8 fixes

* Pep 8 fixes

* Pep 8 fixes

* Pep8 fixes

* Pep8 fixes

* Pep8 fixes

* Pep8

* Pep8

* Pep 8

* Pep 8

* Pep8 fixes

* Pep8

* Pep8

* Pep8

* Pep8 fixes

* Pep8 fixes

* Pep8 fixes

* Pep8 fixes

* Pep8 fixes

* Revert chagnes

* Revert changes to kivy/__init.py

* Revert changes

* REvert changes

* Revert changes

* Revert changes to toolchain

* Add files exclusions to tox.ini

* Added exclusions for alias recipes

* Remove dead code

* Added py extension to recipes

* Removed recipe build skip

* Improves recipe matching

Previous expression was matching all the following three lines of a `git diff --name-only` output.
```
recipes/hostlibffi/__init__.py
recipes/hostpython.py
recipes/hostpython2/__init__.py
```
This was resulting to a bug when later splitting with `recipe = file_path.split('/')[1]` the `recipes/hostpython.py` string would return including the `\n` new line char, see:
 ```
>>> 'recipes/hostpython.py\n'.split('/')[1]
'hostpython.py\n'
>>> 'recipes/hostlibffi/__init__.py\n'.split('/')[1]
'hostlibffi'
>>> 
```

Co-authored-by: Andre Miras <AndreMiras@users.noreply.github.com>
2020-04-25 18:28:16 +02:00

58 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python
import os
import sys
import subprocess
# resolve cython executable
cython = None
def resolve_cython():
global cython
for executable in ('cython', 'cython-2.7'):
for path in os.environ['PATH'].split(':'):
if not os.path.exists(path):
continue
if executable in os.listdir(path):
cython = os.path.join(path, executable)
return
def do(fn):
print('cythonize:', fn)
assert(fn.endswith('.pyx'))
parts = fn.split('/')
if parts[0] == '.':
parts.pop(0)
modname = parts[-1][:-4]
package = '_'.join(parts[:-1])
# cythonize
subprocess.Popen([cython, fn], env=os.environ).communicate()
if not package:
print('no need to rewrite', fn)
else:
# get the .c, and change the initXXX
fn_c = fn[:-3] + 'c'
with open(fn_c) as fd:
data = fd.read()
modname = modname.split('.')[-1]
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))
data = data.replace(pat, sub)
print('rewrite', fn_c)
with open(fn_c, 'w') as fd:
fd.write(data)
if __name__ == '__main__':
print('-- cythonize', sys.argv)
resolve_cython()
for fn in sys.argv[1:]:
do(fn)