# -*- coding: utf-8 -*-
"""
@File : publish_pyc.py
@Time : 2019/10/26 17:25
@Author : gaobo
@Description: publish_pyc
注:将此脚本拷贝到项目的根目录下
"""
import os
import shutil
from py_compile import compile
def publish_pyc():
path = os.path.dirname(os.path.abspath(__file__))
publish_pyc_file = os.path.join(path, 'publish')
if not os.path.exists(publish_pyc_file):
os.makedirs(publish_pyc_file)
path_index = len(path)
for parent, dirname, filename in os.walk(path):
new_parent1 = parent[path_index+1:]
new_parent2 = os.path.join(path, 'publish')
new_parent = os.path.join(new_parent2, new_parent1)
if parent[:path_index+8] == publish_pyc_file:
continue
else:
if not os.path.exists(new_parent):
os.mkdir(new_parent)
for cfile in filename:
fullname = os.path.join(parent, cfile)
if cfile[-3:] == '.py':
try:
pyc_name = cfile[:-3] + ".pyc"
if compile(fullname, cfile=os.path.join(new_parent, pyc_name)):
print("文件%s编译成功" % fullname)
else:
print("文件%s编译失败" % fullname)
except:
print("无法编译文件:%s" % fullname)
else:
shutil.copy(fullname, new_parent)
print("拷贝文件---》"+fullname+"至---》"+new_parent)
if __name__ == '__main__':
publish_pyc()
网友评论