Debian Codegraph @

pypy/tool/import_cffi.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """ A simple tool for importing the cffi version into pypy, should sync
3 whatever version you provide. Usage:
4
5 python3.9 pypy/tool/import_cffi.py <path-to-cffi>
6 """
7 from __future__ import print_function
8
9 import sys, pathlib
10
11 def mangle(lines, ext):
12 if ext == '.py':
13 yield "# Generated by pypy/tool/import_cffi.py\n"
14 for line in lines:
15 line = line.replace('from testing', 'from extra_tests.cffi_tests')
16 yield line
17 elif ext in ('.c', '.h'):
18 yield "/* Generated by pypy/tool/import_cffi.py */\n"
19 for line in lines:
20 yield line
21 else:
22 raise AssertionError(ext)
23
24 def fixeol(s):
25 s = s.replace('\r\n', '\n')
26 return s
27
28 def main(cffi_dir):
29 cffi_dir = pathlib.Path(cffi_dir)
30 rootdir = pathlib.Path(__file__).parent.parent.parent
31 cffi_dest = rootdir / 'lib_pypy' / 'cffi'
32 cffi_dest.mkdir(exist_ok=True)
33 test_dest = rootdir / 'extra_tests' / 'cffi_tests'
34 test_dest.ensure(dir=1)
35 for p in (list(cffi_dir.join('src', 'cffi').visit(fil='*.py')) +
36 list(cffi_dir.join('src', 'cffi').visit(fil='*.h'))):
37 cffi_dest.join(p.relto(cffi_dir.join('src', 'cffi'))).write_binary(fixeol(p.read()))
38 for p in (list(cffi_dir.join('testing').visit(fil='*.py')) +
39 list(cffi_dir.join('testing').visit(fil='*.h')) +
40 list(cffi_dir.join('testing').visit(fil='*.c'))):
41 path = test_dest.join(p.relto(cffi_dir.join('testing')))
42 path.join('..').ensure(dir=1)
43 path.write_binary(fixeol(''.join(mangle(p.readlines(), p.ext))))
44 path = test_dest.join('test_c.py')
45 path.write_binary(fixeol(cffi_dir.join('src', 'c', 'test_c.py').read()))
46 #
47 # hack around a little bit...
48 os.system("cd '%s' && patch -p0 < pypy/tool/import_cffi.patch" % str(rootdir))
49
50 if __name__ == '__main__':
51 if sys.version_info < (3, 0):
52 print(__doc__)
53 print("must not use python2")
54 sys.exit(2)
55 if len(sys.argv) != 2:
56 print(__doc__)
57 sys.exit(2)
58 if not os.path.exists(sys.argv[1]):
59 print(__doc__)
60 print("'%s' is not a valid path" % sys.argv[1])
61 sys.exit(2)
62 main(sys.argv[1])