build_pyd.py 3.1 KB

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