build_pyd.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. import sys
  4. import os
  5. import shutil
  6. import time
  7. from distutils.core import setup
  8. from Cython.Build import cythonize
  9. # import zipextimporter
  10. start_time = time.time()
  11. curr_dir = os.path.abspath('.')
  12. parent_path = sys.argv[1] if len(sys.argv) > 1 else ""
  13. setup_file = __file__.replace('/', '')
  14. build_dir = "build"
  15. build_tmp_dir = build_dir + "/temp"
  16. s = "# cython: language_level=3"
  17. """
  18. 获取py文件的路径
  19. :param base_path: 根路径
  20. :param parent_path: 父路径
  21. :param excepts: 排除文件
  22. :return: py文件的迭代器
  23. """
  24. def get_py(base_path=os.path.abspath('.'), parent_path='', name='', excepts=(), copyOther=False, delC=False):
  25. full_path = os.path.join(base_path, parent_path, name)
  26. for filename in os.listdir(full_path):
  27. full_filename = os.path.join(full_path, filename)
  28. if os.path.isdir(full_filename) and filename != build_dir and not filename.startswith('.'):
  29. for f in get_py(base_path, os.path.join(parent_path, name), filename, excepts, copyOther, delC):
  30. yield f
  31. elif os.path.isfile(full_filename):
  32. ext = os.path.splitext(filename)[1]
  33. if ext == ".c":
  34. if delC and os.stat(full_filename).st_mtime > start_time:
  35. os.remove(full_filename)
  36. elif full_filename not in excepts and os.path.splitext(filename)[1] not in ('.pyc', '.pyx'):
  37. if os.path.splitext(filename)[1] in ('.py', '.pyx') and not filename.startswith('__'):
  38. path = os.path.join(parent_path, name, filename)
  39. yield path
  40. else:
  41. pass
  42. def pack_pyd():
  43. # 获取py列表
  44. module_list = list(get_py(base_path=curr_dir, parent_path=parent_path, excepts=(setup_file,)))
  45. try:
  46. setup(
  47. ext_modules=cythonize(module_list, language_level="3"),
  48. script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir],
  49. )
  50. except Exception as ex:
  51. print("error! ", str(ex))
  52. else:
  53. module_list = list(get_py(base_path=curr_dir, parent_path=parent_path, excepts=(setup_file,), copyOther=True))
  54. module_list = list(get_py(base_path=curr_dir, parent_path=parent_path, excepts=(setup_file,), delC=True))
  55. if os.path.exists(build_tmp_dir):
  56. shutil.rmtree(build_tmp_dir)
  57. print("complate! time:", time.time() - start_time, 's')
  58. """
  59. 删除编译过程中生成的.c文件
  60. :param path:
  61. :param excepts:
  62. :return:
  63. """
  64. def delete_c(path='.', excepts=(setup_file,)):
  65. dirs = os.listdir(path)
  66. for dir in dirs:
  67. new_dir = os.path.join(path, dir)
  68. if os.path.isfile(new_dir):
  69. ext = os.path.splitext(new_dir)[1]
  70. if ext == '.c':
  71. os.remove(new_dir)
  72. elif os.path.isdir(new_dir):
  73. delete_c(new_dir)
  74. if __name__ == '__main__':
  75. try:
  76. pack_pyd()
  77. except Exception as e:
  78. print(str(e))
  79. finally:
  80. delete_c()