62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: UTF-8 -*-
|
||
"""
|
||
@Project :IoD_data_analysis_tool
|
||
@File :project_util.py
|
||
@IDE :PyCharm
|
||
@Author :rengengchen
|
||
@Time :2022/9/15 9:45
|
||
"""
|
||
import compileall
|
||
import os
|
||
import re
|
||
import shutil
|
||
from os.path import join
|
||
|
||
from lib.analysis_package.utils.file_util import walk
|
||
|
||
re_pyc = re.compile(r'cpython-\d+\.')
|
||
|
||
|
||
def compile_project(source, target=None):
|
||
"""
|
||
编译项目为pyc文件到指定目录
|
||
@param source: 项目路径
|
||
@param target: 编译文件存放路径
|
||
"""
|
||
source = os.path.abspath(source)
|
||
if target is None:
|
||
target = source
|
||
else:
|
||
target = os.path.abspath(target)
|
||
compileall.compile_dir(source)
|
||
pycache_paths = set()
|
||
if target == source:
|
||
for root, dirname, filename in walk(source):
|
||
if root[-11:] == '__pycache__':
|
||
pycache_paths.add(root)
|
||
shutil.move(join(root, filename), join(root, '../', re_pyc.sub('', filename)))
|
||
if filename and filename.endswith('py'):
|
||
os.remove(join(root, filename))
|
||
else:
|
||
if target is None:
|
||
target = join(source, 'dist')
|
||
len_t = len(target)
|
||
for root, dirname, filename in walk(source):
|
||
t_root = root.replace(source, target)
|
||
if target == root[:len_t]:
|
||
continue
|
||
if dirname and dirname != '__pycache__':
|
||
t_root = join(t_root, dirname)
|
||
if not os.path.exists(t_root) and join(source, dirname) != target:
|
||
os.makedirs(t_root)
|
||
elif filename and not filename.endswith('py'):
|
||
if root[-11:] == '__pycache__':
|
||
pycache_paths.add(root)
|
||
t_root = t_root[:-11]
|
||
shutil.move(join(root, filename), join(t_root, re_pyc.sub('', filename)))
|
||
else:
|
||
shutil.copyfile(join(root, filename), join(t_root, filename))
|
||
for p in pycache_paths:
|
||
os.rmdir(p)
|