-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
140 lines (48 loc) · 1.47 KB
/
build.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os;
import json;
folder = r"./build/_dist_"
def file_filter(f):
if f.endswith(".json"):
return True
else:
return False
def all_path(dirname):
result = []#所有的文件
for maindir, subdir, file_name_list in os.walk(dirname):
#print("1:",maindir) #当前主目录
#print("2:",subdir) #当前主目录下的所有目录
#print("3:",file_name_list) #当前主目录下的所有文件
for filename in file_name_list:
apath = os.path.join(maindir, filename)#合并成一个完整路径
result.append(apath)
return result
def read_file(files):
dict = []
for file in files:
with open(file,'r') as load_file:
load_dict = json.load(load_file)
dict.append(load_dict)
return dict
def file_replace(file,dict):
with open(file,'+r') as f:
t = f.read()
for key in dict:
t = t.replace(dict[key],key)
#读写偏移位置移到最开始处
f.seek(0, 0)
f.write(t)
# 设置文件结尾 EOF
f.truncate()
if __name__ == '__main__':
files = list(filter(file_filter, all_path(folder)))
for file in files:
print(file)
dictArray = read_file(files)
#过滤文件名,获取index.js
targetFiles = os.listdir('./build/js')
for tfile in targetFiles:
print(tfile)
for dict in dictArray:
for key in dict:
print(key +":" + dict[key])
file_replace('./build/js/' +targetFiles[0],dict)