include_gen.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. import os
  3. import shutil
  4. from os.path import dirname, exists, join, relpath
  5. template = '''\
  6. #ifdef USE_PYTHON3
  7. #{}include <{}>
  8. #else
  9. #{}include <{}>
  10. #endif
  11. '''
  12. def main():
  13. os.chdir(dirname(__file__))
  14. if exists('include'):
  15. shutil.rmtree('include')
  16. include_gen('contrib/python/numpy', ['numpy'])
  17. def include_gen(root, subpaths):
  18. for path in list_subpaths(subpaths):
  19. out = join('include', path)
  20. py2 = join('py2', path)
  21. py3 = join('py3', path)
  22. makedir(dirname(out))
  23. with open(out, 'w') as f:
  24. f.write(template.format(
  25. '' if exists(py3) else 'error #',
  26. join(root, py3),
  27. '' if exists(py2) else 'error #',
  28. join(root, py2),
  29. ))
  30. def is_header(s):
  31. return s.endswith(('.h', '.hpp'))
  32. def list_subpaths(subpaths, roots=('py2', 'py3'), test=is_header):
  33. seen = set()
  34. for root in roots:
  35. for subpath in subpaths:
  36. for dirpath, _, filenames in os.walk(join(root, subpath)):
  37. rootrel = relpath(dirpath, root)
  38. for filename in filenames:
  39. if test(filename):
  40. seen.add(join(rootrel, filename))
  41. if dirpath.endswith('numpy/core/src/umath') and filename == 'funcs.inc':
  42. seen.add(join(rootrel, filename))
  43. if dirpath.endswith('numpy/core/include/numpy') and filename in ('__multiarray_api.c', '__ufunc_api.c', '__umath_generated.c'):
  44. seen.add(join(rootrel, filename))
  45. if filename.endswith(('.dispatch.c', '.dispatch.cpp')):
  46. seen.add(join(rootrel, filename))
  47. return seen
  48. def makedir(path):
  49. if not exists(path):
  50. os.makedirs(path)
  51. if __name__ == '__main__':
  52. main()