[关闭]
@cyysu 2017-10-17T08:33:10.000000Z 字数 2193 阅读 968

python转so脚本

  • 时间:2017年10月17日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:Python文件转成动态库脚本,需要安装cython

脚本编写


脚本内容

  1. #-* -coding: UTF-8 -* -
  2. __author__ = 'Arvin'
  3. """
  4. 执行前提:
  5. 系统安装python-devel 和 gcc
  6. Python安装cython
  7. 编译整个当前目录:
  8. python py-setup.py
  9. 编译某个文件夹:
  10. python py-setup.py BigoModel
  11. 生成结果:
  12. 目录 build 下
  13. 生成完成后:
  14. 启动文件还需要py/pyc担当,须将启动的py/pyc拷贝到编译目录并删除so文件
  15. """
  16. import sys, os, shutil, time
  17. from distutils.core import setup
  18. from Cython.Build import cythonize
  19. starttime = time.time()
  20. currdir = os.path.abspath('.')
  21. parentpath = sys.argv[1] if len(sys.argv)>1 else ""
  22. setupfile= os.path.join(os.path.abspath('.'), __file__)
  23. build_dir = "build"
  24. build_tmp_dir = build_dir + "/temp"
  25. def getpy(basepath=os.path.abspath('.'), parentpath='', name='', excepts=(), copyOther=False,delC=False):
  26. """
  27. 获取py文件的路径
  28. :param basepath: 根路径
  29. :param parentpath: 父路径
  30. :param name: 文件/夹
  31. :param excepts: 排除文件
  32. :param copy: 是否copy其他文件
  33. :return: py文件的迭代器
  34. """
  35. fullpath = os.path.join(basepath, parentpath, name)
  36. for fname in os.listdir(fullpath):
  37. ffile = os.path.join(fullpath, fname)
  38. #print basepath, parentpath, name,file
  39. if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'):
  40. for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):
  41. yield f
  42. elif os.path.isfile(ffile):
  43. ext = os.path.splitext(fname)[1]
  44. if ext == ".c":
  45. if delC and os.stat(ffile).st_mtime > starttime:
  46. pass
  47. #os.remove(ffile)
  48. elif ffile not in excepts and os.path.splitext(fname)[1] not in('.pyc', '.pyx'):
  49. if os.path.splitext(fname)[1] in('.py', '.pyx') and not fname.startswith('__'):
  50. yield os.path.join(parentpath, name, fname)
  51. elif copyOther:
  52. dstdir = os.path.join(basepath, build_dir, parentpath, name)
  53. if not os.path.isdir(dstdir): os.makedirs(dstdir)
  54. shutil.copyfile(ffile, os.path.join(dstdir, fname))
  55. else:
  56. pass
  57. #获取py列表
  58. module_list = list(getpy(basepath=currdir,parentpath=parentpath, excepts=(setupfile)))
  59. try:
  60. setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
  61. except Exception, ex:
  62. print "error! ", ex.message
  63. else:
  64. module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), copyOther=True))
  65. module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), delC=True))
  66. if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)
  67. print "complate! time:", time.time()-starttime, 's'

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注