build_pyd.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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()
  88. # #!/usr/bin/env python
  89. # # -*- coding:utf-8 -*-
  90. # # @time: 2021/5/26 14:17
  91. # # @File: create_pyd_file.py
  92. # import os
  93. # import shutil
  94. # import time
  95. # import sys
  96. # def func(path):
  97. # folder_path = os.path.dirname(path)
  98. # file_path = os.path.split(path)[1]
  99. # os.chdir(folder_path)
  100. # with open('setup.py', 'w') as f:
  101. # f.write('from setuptools import setup\n')
  102. # f.write('from Cython.Build import cythonize\n')
  103. # f.write('setup(\n')
  104. # f.write("name='test',\n")
  105. # f.write("ext_modules=cythonize('%s')\n" % file_path)
  106. # f.write(")\n")
  107. # os.system('python setup.py build_ext --inplace')
  108. # filename = file_path.split('.py')[0]
  109. # time.sleep(2)
  110. # # 这里的cp37-win_amd64需要注意一下,这个是依据python解释器类型以及windows版本生成的,建议是单个生成一个pyd文件然后相应修改一下
  111. # os.rename('%s\\%s.cp38-win_64.pyd' % (folder_path, filename), '%s\\%s.pyd' % (folder_path, filename))
  112. # # 这个是删除py源文件,测试的时候可以先注释掉查看效果
  113. # os.remove('%s.c' % filename)
  114. # build_folder_path = os.path.join(folder_path, 'build')
  115. # # 删除掉生成的build文件夹
  116. # shutil.rmtree(build_folder_path)
  117. # os.remove('setup.py')
  118. # os.remove(file_path)
  119. # def get_all_file(path):
  120. # for root, dirs, files in os.walk(path):
  121. # for name in files:
  122. # if name.endswith(".py"):
  123. # file_path = os.path.join(root, name)
  124. # func(file_path)
  125. # paths = sys.argv[1]
  126. # get_all_file(paths)