build_for_wugang.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import distutils.core
  4. import Cython.Build
  5. import datetime
  6. import os
  7. import time
  8. from os.path import abspath
  9. def get_files(filePath):
  10. os.chdir(filePath)
  11. print(os.path.abspath(os.curdir))
  12. allFile = os.listdir(filePath)
  13. files = []
  14. for f in allFile:
  15. if os.path.isdir(f):
  16. files.extend(get_files(filePath+'/'+f))
  17. os.chdir(filePath)
  18. else:
  19. if f[-2:] == 'py' and f != '__init__.py':
  20. files.append(filePath + '/' + f)
  21. return files
  22. def py2c(fileName):
  23. cpy = Cython.Build.cythonize(fileName) # 返回distutils.extension.Extension对象列表
  24. distutils.core.setup(
  25. name = 'pyd的编译', # 包名称
  26. version = "1.0", # 包版本号
  27. ext_modules= cpy, # 扩展模块
  28. author = "vivstone",#作者
  29. author_email='cj@vevestone.com'#作者邮箱
  30. )
  31. return
  32. # 遍历项目下的所有的py文件,然后编译出来
  33. dirs = [
  34. 'apps/web/ad',
  35. 'apps/web/agent',
  36. 'apps/web/api',
  37. 'apps/web/common',
  38. 'apps/web/core',
  39. 'apps/web/dealer',
  40. 'apps/web/device',
  41. 'apps/web/eventer',
  42. 'apps/web/knowledge',
  43. 'apps/web/management',
  44. 'apps/web/miniuser',
  45. 'apps/web/report',
  46. 'apps/web/services',
  47. 'apps/web/superadmin',
  48. 'apps/web/test',
  49. 'apps/web/user',
  50. ]
  51. adapters = [
  52. 'base.py',
  53. 'xiyiji.py'
  54. ]
  55. eventers = [
  56. 'base.py',
  57. 'xiyiji.py'
  58. ]
  59. prjRoot = abspath(os.path.split(os.path.realpath(__file__))[0] + "/../..")
  60. nowTime = datetime.datetime.now()
  61. for d in dirs:
  62. delFiles = []
  63. curDir = prjRoot + '/' + d
  64. files = get_files(curDir)
  65. # 首先编译出来so文件
  66. for fileName in files:
  67. # 如果是adapter和eventer中的文件,需要挑选出来编译
  68. if 'web/core/adapter' in fileName:
  69. name = fileName.split('/')[-1]
  70. if name not in adapters:
  71. delFiles.append(fileName)
  72. print u'不必要的adapter插件文件,稍后会被脚本删除掉,%s' % fileName
  73. continue
  74. if 'web/eventer' in fileName:
  75. name = fileName.split('/')[-1]
  76. if name not in eventers:
  77. delFiles.append(fileName)
  78. print u'不必要的eventer插件文件,稍后会被脚本删除掉,%s' % fileName
  79. continue
  80. print u'开始编译文件 %s ' % fileName
  81. py2c(fileName)
  82. print u'完成编译,准备拷贝so文件到对应的目录'
  83. time.sleep(5)
  84. print u'开始拷贝so文件'
  85. cmdLine = 'cp -r %s/%s/%s %s/..' % (curDir,'UserServerTest',d,curDir)
  86. print cmdLine
  87. os.system(cmdLine)
  88. for fileName in files:
  89. print u'删除py文件:%s' % fileName
  90. os.system('rm -fr %s' % fileName)
  91. pycFile = fileName[:-2] + 'pyc'
  92. print u'删除pyc文件:%s' % pycFile
  93. os.system('rm -fr %s' % pycFile)
  94. cFile = fileName[:-2] + 'c'
  95. print u'删除c文件:%s' % cFile
  96. os.system('rm -fr %s' % cFile)
  97. if delFiles:
  98. print u'删除掉不需要的adapterFiles,以及eventerFiles'
  99. for fileName in delFiles:
  100. print u'删除不需要的设备插件py文件:%s' % fileName
  101. os.system('rm -fr %s' % fileName)
  102. spendTime = (datetime.datetime.now() - nowTime).seconds
  103. print u'恭喜您!用时:%s 秒,编译全部完成!!部署也全部完成,重启应用进程即可使用!!' % spendTime