-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathbuild.py
More file actions
250 lines (222 loc) · 8.17 KB
/
Copy pathbuild.py
File metadata and controls
250 lines (222 loc) · 8.17 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import hashlib
import os
import shutil
import subprocess
import sys
import zipfile
import vswhere
import jproperties
import platform
import winreg
rulesToDelete = [
r'com\sun\jna\aix-ppc',
r'com\sun\jna\darwin',
r'com\sun\jna\freebsd',
r'com\sun\jna\linux',
r'com\sun\jna\openbsd',
r'com\sun\jna\sunos',
r'com\sun\jna\win32-aarch64',
r'com\sun\jna\win32-x86',
r'com\sun\jna\platform\linux',
r'com\sun\jna\platform\mac',
r'com\sun\jna\platform\unix',
r'org\sqlite\native',
r'oshi\driver\linux',
r'oshi\driver\mac',
r'oshi\driver\unix',
r'oshi\hardware\platform\linux',
r'oshi\hardware\platform\mac',
r'oshi\hardware\platform\unix',
r'oshi\jna\platform\linux',
r'oshi\jna\platform\mac',
r'oshi\jna\platform\unix',
r'oshi\software\os\linux',
r'oshi\software\os\mac',
r'oshi\software\os\unix',
r'oshi\util\platform\linux',
r'oshi\util\platform\mac',
r'oshi\util\platform\unix'
]
rulesToSave = [
r'com\sun\jna\win32-x86-64',
]
def unzipFile(zip_src, dst_dir): # 解压函数,将zip_src解压到dst_dir
r = zipfile.is_zipfile(zip_src)
if r:
fz = zipfile.ZipFile(zip_src, 'r')
for file in fz.namelist():
fz.extract(file, dst_dir)
else:
print('This is not zip......')
def delFileInZip():
print("UnZip:" + 'File-Engine.jar')
pathName = 'File-Engine'
unzipFile('File-Engine.jar', pathName)
for root, _, files in os.walk(pathName): # 遍历pathName文件夹
for f in files:
nextFile = False
fileName = os.path.join(root, f)
for deleteRule in rulesToDelete:
deletePrefix = os.path.join(pathName, deleteRule)
if fileName.startswith(deletePrefix):
for saveRule in rulesToSave:
savePrefix = os.path.join(pathName, saveRule)
if fileName.startswith(savePrefix):
nextFile = True
break
if nextFile:
break
os.remove(fileName)
os.remove('File-Engine.jar')
delDir(pathName)
shutil.make_archive(pathName, 'zip', pathName) # 压缩
shutil.rmtree(pathName) # 删除临时文件
os.rename('File-Engine.zip', 'File-Engine.jar')
print('=======Finish!======')
def delDir(path):
"""
清理空文件夹和空文件
param path: 文件路径,检查此文件路径下的子文件
"""
try:
files = os.listdir(path) # 获取路径下的子文件(夹)列表
for file in files:
if os.path.isdir(os.fspath(path+'/'+file)): # 如果是文件夹
if not os.listdir(os.fspath(path+'/'+file)): # 如果子文件为空
os.rmdir(os.fspath(path+'/'+file)) # 删除这个空文件夹
else:
delDir(os.fspath(path+'/'+file)) # 遍历子文件
if not os.listdir(os.fspath(path+'/'+file)): # 如果子文件为空
os.rmdir(os.fspath(path+'/'+file)) # 删除这个空文件夹
elif os.path.isfile(os.fspath(path+'/'+file)): # 如果是文件
if os.path.getsize(os.fspath(path+'/'+file)) == 0: # 文件大小为0
os.remove(os.fspath(path+'/'+file)) # 删除这个文件
return
except FileNotFoundError:
print("文件夹路径错误")
def getFileMd5(file_name):
"""
计算文件的md5
:param file_name:
:return:
"""
m = hashlib.md5() # 创建md5对象
with open(file_name, 'rb') as fobj:
while True:
data = fobj.read(4096)
if not data:
break
m.update(data) # 更新md5对象
return m.hexdigest() # 返回md5对象
def get_java_path():
if platform.system() == "Windows":
registry_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\JavaSoft\\Java Development Kit")
try:
i = 0
while True:
sub_key_name = winreg.EnumKey(registry_key, i)
sub_key = winreg.OpenKey(registry_key, sub_key_name)
java_home = winreg.QueryValueEx(sub_key, "JavaHome")[0]
if os.path.exists(java_home):
return java_home
i += 1
except WindowsError:
return None
return None
buildDir = 'build'
if not os.path.exists(buildDir):
os.mkdir(buildDir)
jdkPath = get_java_path()
additionalModule = []
if len(sys.argv) > 1:
jdkPath = sys.argv[1]
if len(sys.argv) > 2:
for i in range(2, len(sys.argv)):
additionalModule.append(sys.argv[i])
print("additional module added: " + sys.argv[i])
if jdkPath is None:
raise EnvironmentError('未找到JAVA_HOME')
print("JAVA_HOME is set to " + jdkPath)
# 编译jar
if os.system('set JAVA_HOME=' + jdkPath + '&& mvn clean compile package') != 0:
print('maven compile failed')
exit()
# 获取版本
configs = jproperties.Properties()
with open(r'.\target\maven-archiver\pom.properties', 'rb') as f:
configs.load(f)
fileEngineVersion = configs.get('version')
# 切换到build
os.chdir(buildDir)
if os.system(r'xcopy ..\target\File-Engine.jar . /Y') != 0:
print('xcopy File-Engine.jar failed.')
exit()
os.system(r'del /Q /F File-Engine.zip')
# 生成jre
binPath = os.path.join(jdkPath, 'bin')
jdepExe = os.path.join(binPath, 'jdeps.exe')
deps = subprocess.check_output([jdepExe, '--ignore-missing-deps', '--print-module-deps', r'..\target\File-Engine-' + fileEngineVersion.data + '.jar'])
depsStr = deps.decode().strip()
modulesFromJar = depsStr.split(',')
javaExe = os.path.join(binPath, 'java.exe')
modules = subprocess.check_output([javaExe, '--list-modules'])
modulesStr = modules.decode().strip()
tmpModuleList = modulesStr.splitlines()
moduleList = []
for each in tmpModuleList:
if not each.startswith('jdk.') and each.startswith('java.'):
moduleName = each.split('@')
moduleList.append(moduleName[0])
for eachModule in modulesFromJar:
moduleList.append(eachModule)
#将additionalModule添加到moduleList
for eachModule in additionalModule:
moduleList.append(eachModule.strip())
moduleList = set(moduleList)
print("deps: " + str(moduleList))
depsStr = ','.join(moduleList)
#判断jre文件夹是否存在
if os.path.exists('jre'):
shutil.rmtree('jre')
jlinkExe = os.path.join(binPath, 'jlink.exe')
jlinkExe = jlinkExe[0:1] + '\"' + jlinkExe[1:] + '\"'
if os.system(jlinkExe + r' --no-header-files --no-man-pages --module-path jmods --add-modules ' + depsStr + ' --output jre') != 0:
print('Generate jre failed.')
exit()
# os.system('pause')
# 精简jar
delFileInZip()
md5Str = getFileMd5('File-Engine.jar')
print("File-Engine.jar md5: " + md5Str)
current_dir = os.getcwd()
launchWrapCppFile = os.path.join(
current_dir, r'..\C++\launcherWrap\launcherWrap\launcherWrap.cpp')
# 计算File-Engine.jar md5
strs: list
with open(launchWrapCppFile, 'r', encoding='utf-8') as f:
strs = f.readlines()
for i in range(len(strs)):
if strs[i].startswith('#define FILE_ENGINE_JAR_MD5'):
strs[i] = '#define FILE_ENGINE_JAR_MD5 ' + \
"\"" + md5Str + "\"" + '\n'
break
with open(launchWrapCppFile, 'w', encoding='utf-8') as f:
f.writelines(strs)
print('Generate File-Engine.zip')
shutil.make_archive('File-Engine', 'zip', root_dir='.', base_dir='jre')
with zipfile.ZipFile('File-Engine.zip', mode="a") as f:
f.write('File-Engine.jar')
if os.system(r'xcopy File-Engine.zip "..\C++\launcherWrap\launcherWrap\" /Y') != 0:
print('xcopy File-Engine.zip to launcherWrap directory failed.')
exit()
# 编译启动器
vsPathList = vswhere.find(
latest=True, requires='Microsoft.Component.MSBuild', find=r'MSBuild\**\Bin\MSBuild.exe')
if not vsPathList:
raise RuntimeError("Cannot find visual studio installation or MSBuild.exe")
vsPath = vsPathList[0]
vsPath = vsPath[0:1] + '\"' + vsPath[1:] + "\""
os.system(vsPath + r' ..\C++\launcherWrap\launcherWrap.sln /p:Configuration=Release')
os.system(r'xcopy ..\C++\launcherWrap\x64\Release\launcherWrap.exe .\ /F /Y')
os.system(r'del /Q /F File-Engine.exe')
os.system(r'ren launcherWrap.exe File-Engine.exe')