# -*- coding: utf-8 -*- # !/usr/bin/env python import distutils.core import Cython.Build import datetime import os import time from os.path import abspath def get_files(filePath): os.chdir(filePath) print(os.path.abspath(os.curdir)) allFile = os.listdir(filePath) files = [] for f in allFile: if os.path.isdir(f): files.extend(get_files(filePath+'/'+f)) os.chdir(filePath) else: if f[-2:] == 'py' and f != '__init__.py': files.append(filePath + '/' + f) return files def py2c(fileName): cpy = Cython.Build.cythonize(fileName) # 返回distutils.extension.Extension对象列表 distutils.core.setup( name = 'pyd的编译', # 包名称 version = "1.0", # 包版本号 ext_modules= cpy, # 扩展模块 author = "vivstone",#作者 author_email='cj@vevestone.com'#作者邮箱 ) return # 遍历项目下的所有的py文件,然后编译出来 dirs = [ 'apps/web/ad', 'apps/web/agent', 'apps/web/api', 'apps/web/common', 'apps/web/core', 'apps/web/dealer', 'apps/web/device', 'apps/web/eventer', 'apps/web/knowledge', 'apps/web/management', 'apps/web/report', 'apps/web/services', 'apps/web/superadmin', 'apps/web/test', 'apps/web/user', ] adapters = [ 'base.py', 'xiyiji.py' ] eventers = [ 'base.py', 'xiyiji.py' ] prjRoot = abspath(os.path.split(os.path.realpath(__file__))[0] + "/../..") nowTime = datetime.datetime.now() for d in dirs: delFiles = [] curDir = prjRoot + '/' + d files = get_files(curDir) # 首先编译出来so文件 for fileName in files: # 如果是adapter和eventer中的文件,需要挑选出来编译 if 'web/core/adapter' in fileName: name = fileName.split('/')[-1] if name not in adapters: delFiles.append(fileName) print u'不必要的adapter插件文件,稍后会被脚本删除掉,%s' % fileName continue if 'web/eventer' in fileName: name = fileName.split('/')[-1] if name not in eventers: delFiles.append(fileName) print u'不必要的eventer插件文件,稍后会被脚本删除掉,%s' % fileName continue print u'开始编译文件 %s ' % fileName py2c(fileName) print u'完成编译,准备拷贝so文件到对应的目录' time.sleep(5) print u'开始拷贝so文件' cmdLine = 'cp -r %s/%s/%s %s/..' % (curDir,'UserServerTest',d,curDir) print cmdLine os.system(cmdLine) for fileName in files: print u'删除py文件:%s' % fileName os.system('rm -fr %s' % fileName) pycFile = fileName[:-2] + 'pyc' print u'删除pyc文件:%s' % pycFile os.system('rm -fr %s' % pycFile) cFile = fileName[:-2] + 'c' print u'删除c文件:%s' % cFile os.system('rm -fr %s' % cFile) if delFiles: print u'删除掉不需要的adapterFiles,以及eventerFiles' for fileName in delFiles: print u'删除不需要的设备插件py文件:%s' % fileName os.system('rm -fr %s' % fileName) spendTime = (datetime.datetime.now() - nowTime).seconds print u'恭喜您!用时:%s 秒,编译全部完成!!部署也全部完成,重启应用进程即可使用!!' % spendTime