build_for_wugang.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/report',
  45. 'apps/web/services',
  46. 'apps/web/superadmin',
  47. 'apps/web/test',
  48. 'apps/web/user',
  49. ]
  50. adapters = [
  51. 'base.py',
  52. 'xiyiji.py'
  53. ]
  54. eventers = [
  55. 'base.py',
  56. 'xiyiji.py'
  57. ]
  58. prjRoot = abspath(os.path.split(os.path.realpath(__file__))[0] + "/../..")
  59. nowTime = datetime.datetime.now()
  60. for d in dirs:
  61. delFiles = []
  62. curDir = prjRoot + '/' + d
  63. files = get_files(curDir)
  64. # 首先编译出来so文件
  65. for fileName in files:
  66. # 如果是adapter和eventer中的文件,需要挑选出来编译
  67. if 'web/core/adapter' in fileName:
  68. name = fileName.split('/')[-1]
  69. if name not in adapters:
  70. delFiles.append(fileName)
  71. print u'不必要的adapter插件文件,稍后会被脚本删除掉,%s' % fileName
  72. continue
  73. if 'web/eventer' in fileName:
  74. name = fileName.split('/')[-1]
  75. if name not in eventers:
  76. delFiles.append(fileName)
  77. print u'不必要的eventer插件文件,稍后会被脚本删除掉,%s' % fileName
  78. continue
  79. print u'开始编译文件 %s ' % fileName
  80. py2c(fileName)
  81. print u'完成编译,准备拷贝so文件到对应的目录'
  82. time.sleep(5)
  83. print u'开始拷贝so文件'
  84. cmdLine = 'cp -r %s/%s/%s %s/..' % (curDir,'UserServerTest',d,curDir)
  85. print cmdLine
  86. os.system(cmdLine)
  87. for fileName in files:
  88. print u'删除py文件:%s' % fileName
  89. os.system('rm -fr %s' % fileName)
  90. pycFile = fileName[:-2] + 'pyc'
  91. print u'删除pyc文件:%s' % pycFile
  92. os.system('rm -fr %s' % pycFile)
  93. cFile = fileName[:-2] + 'c'
  94. print u'删除c文件:%s' % cFile
  95. os.system('rm -fr %s' % cFile)
  96. if delFiles:
  97. print u'删除掉不需要的adapterFiles,以及eventerFiles'
  98. for fileName in delFiles:
  99. print u'删除不需要的设备插件py文件:%s' % fileName
  100. os.system('rm -fr %s' % fileName)
  101. spendTime = (datetime.datetime.now() - nowTime).seconds
  102. print u'恭喜您!用时:%s 秒,编译全部完成!!部署也全部完成,重启应用进程即可使用!!' % spendTime