-
Notifications
You must be signed in to change notification settings - Fork 0
/
compliepyc.py
52 lines (42 loc) · 1.47 KB
/
compliepyc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# TODO(huangyongchao): 2018-05-14.
# TODO: 将项目下的colletor下面的py编译成pyc,放到bin下面的collector下,改文件目录修改会引起寻找文件等异常.
import os
import shutil
import py_compile
currDir = os.path.dirname(os.path.abspath(__file__))
dstDir = currDir + "/bin/collector"
srcDir = currDir + "/collector"
srcConf = currDir + "/conf/task.json"
dstConf = currDir + "/bin/conf/task.json"
# 清空目录
def clean_dir(dir):
print('clean_dir ' + dir + '...')
for entry in os.scandir(dir):
if entry.name.startswith('.'):
continue
if entry.is_file():
os.remove(entry.path) # 删除文件
else:
shutil.rmtree(entry.path) # 删除目录
# 编译当前文件夹下所有.py文件
def comple_path():
global dstDir
global srcDir
if os.path.exists(dstDir): # 如果存在,清空
clean_dir(dstDir)
else: # 如果不存在,创建
os.mkdir(dstDir)
for filename in os.listdir(srcDir):
if not filename.endswith('.py'):
continue
srcFile = os.path.join(srcDir, filename)
if srcFile == os.path.abspath(__file__): # 自身
continue
dstFile = os.path.join(dstDir, filename + 'c')
print(srcFile + ' --> ' + dstFile)
py_compile.compile(srcFile, cfile=dstFile)
if __name__ == "__main__":
comple_path()
shutil.copyfile(srcConf, dstConf)
print(".....complie completed")