python3做自动编译/清除pyc的脚本
时间:2015-01-27 18:57 来源: 我爱IT技术网 作者:编者
python3做完项目,为了方便分发就做了两个脚本来实现这个功能
build.py可以编译后的pyc文件按照原目录输出到指定目录下
clean.py可以把编译后的pyc文件清除
java下有ant可以实现编译打包,在python下也有类似的工具,比如pant,pyant都可以,做法应该比我的做法更好一点。
build.py
#coding:utf-8
#!/usr/python3/bin/python3
import os
#把所有文件编译成pyc
#把所有pyc文件复制到dist目录下
#在原目录下清除多余的pyc文件
import os
def check_environment(outparams):
'''
检查环境变量
parameter
outparams
return
boolean
'''
if "APP_HOME" in os.environ:
home = os.environ["APP_HOME"]
outparams["home"]=home
return True
else:
print("APP_HOME undefine!")
return False
def asm_compiler(dirList):
'''
编译
parameter
homtRoot 工程根目录
return
boolean
'''
import compileall
for folder in dirList:
compileall.compile_dir(dir=folder, maxlevels=20, force=1)
def asm_clear_all_compiler_files(dirList):
'''
清除所有编译文件
parameter
dirList 原目录列表
return
boolean
'''
for path in dirList:
fList=[]
asm_ls_dirs(path, ".pyc", fList)
for f in fList:
print("remove ", f)
os.remove(f)
del fList[:]
def asm_copy_compiler_files(home, distRoot):
'''
复制编译文件
parameter
home 原目录
distRoot 目标根目录
return
boolean
'''
import shutil
from shutil import copytree, ignore_patterns
import os
ret = os.path.exists(home)
if ret:
if os.path.exists(distRoot):
shutil.rmtree(distRoot)
#设置忽略文件和目录
shutil.copytree(home, distRoot, ignore=ignore_patterns("*.py", "*.cer", "documents", "build", "dist", "tests"))
def asm_ls_dirs(path, fileext, fList):
for p in os.listdir(path):
f=path + os.path.sep + p
if os.path.isdir(f):
asm_ls_dirs(f, fileext, fList)
else:
if f.endswith(fileext):
fList.append(f)
if __name__ == "__main__":
outparams={}
ret = check_environment(outparams)
if ret:
home = outparams['home']
dirList = []
dirList.append("".join([home, "/device"]))
dirList.append("".join([home, "/hsm"]))
dirList.append("".join([home, "/main"]))
asm_compiler(dirList)
target= "".join([home, "/dist"])
print("copy compiler files...")
asm_copy_compiler_files(home, target)
print("clear original compiler files...")
asm_clear_all_compiler_files(dirList)
clear.py
#coding:utf-8
#!/usr/python3/bin/python3
import os
import os
#把dist目录下的pyc文件清除所有pyc文件
def check_environment(outparams):
'''
检查环境变量
parameter
outparams
return
boolean
'''
if "APP_HOME" in os.environ:
home = os.environ["APP_HOME"]
outparams["home"]=home
return True
else:
print("APP_HOME undefine!")
return False
def asm_clear_all_compiler_files(dirList):
'''
清除所有编译文件
parameter
dirList 原目录列表
return
boolean
'''
for path in dirList:
fList=[]
asm_ls_dirs(path, ".pyc", fList)
for f in fList:
print("remove ", f)
os.remove(f)
del fList[:]
def asm_ls_dirs(path, fileext, fList):
for p in os.listdir(path):
f=path + os.path.sep + p
if os.path.isdir(f):
asm_ls_dirs(f, fileext, fList)
else:
if f.endswith(fileext):
fList.append(f)
if __name__ == "__main__":
outparams={}
ret = check_environment(outparams)
if ret:
home = outparams['home']
dirList = []
dirList.append("".join([home, "/device"]))
dirList.append("".join([home, "/hsm"]))
dirList.append("".join([home, "/main"]))
print("clear original compiler files...")
asm_clear_all_compiler_files(dirList)
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
