compile-py38.py 1.2 KB
# coding=utf-8
#author:        4N
#createtime:    2022/2/28
#email:         nheweijun@sina.com

import py_compile
import os
import shutil

def compile(des):


    project_dir = os.path.dirname(os.path.realpath(__file__))
    if os.path.normpath(project_dir) == os.path.normpath(des):
        raise Exception("目标目录不能与代码目录一样!")

    if os.path.exists(des):
        os.system("rd/s/q {}".format(des))
        shutil.rmtree(des, True)

    shutil.copytree(project_dir, des)
    git_path = os.path.join(des,".git")
    if os.path.exists(os.path.join(des,".git")):
        os.system("rd/s/q {}".format(git_path))

    for root, dirs, files in os.walk(os.path.join(des,"app")):
        for fn in files:
            if fn.endswith("py"):
                py_file = os.path.join(root, fn)
                py_compile.compile(py_file)
                os.remove(py_file)
                pyc_file = os.path.join(root, "__pycache__",fn.split(".")[0]+".cpython-38.pyc")
                des_pyc_file = os.path.join(root,fn.split(".")[0]+".pyc")
                shutil.copy(pyc_file,des_pyc_file)

if __name__ == '__main__':
    des="H:\DMapManager"
    compile(des)