-
Notifications
You must be signed in to change notification settings - Fork 25
/
createBundle.py
executable file
·414 lines (364 loc) · 12.7 KB
/
createBundle.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#! /usr/bin/python3
# The Admin4 Project
# (c) 2013-2022 Andreas Pflug
#
# Licensed under the Apache License,
# see LICENSE.TXT for conditions of usage
# Ignoring GIT differences if True
INSTALLER_DEBUG=False
filePatterns=['.png', '.ico', '.xrc', '.py', '.html']
ignoredirs=['xrced', 'build', 'dist', '_update', 'release', 'docker']
ignoredfiles=['createBundle.py']
moreFiles=["LICENSE.TXT", 'CHANGELOG']
requiredMods=['wx.lib.ogl', 'xml', 'chardet']
appEntry='admin4.py'
packages=['wx']
includes=['ast']
addModules=[]
excludes=['lib2to3', 'hotshot', 'distutils', 'ctypes', 'unittest']
buildDir=".build"
releaseDir="release/"
versionTag=None
gitTag=None
requiredAdmVersion="3.0.0" # this is written to __version.py
checkGit=True
checkGitCommits=False
recentlyChanged=[]
createSha=True
dockerRepo="adminfour/admin4"
dockerSuffix=""
if __name__ == '__main__':
import sys, os, platform, time
sys.frozen=True
import shutil, zipfile
import hashlib
import version
import git
import subprocess
appName=version.appName
standardInstallDir="/usr/share/%s" % appName
platform=platform.system()
try: os.mkdir(releaseDir)
except: pass
if len(sys.argv) > 1 and sys.argv[1] in ['srcUpdate', 'py2exe', 'py2app', 'docker']:
installer=sys.argv[1]
distDir=releaseDir + "admin4"
else:
if platform == "Windows":
installer='py2exe'
distDir=releaseDir + "Admin4-%s-Win"
elif platform == "Darwin":
installer='py2app'
distDir=releaseDir + "Admin4-%s-Mac"
elif platform == "Linux":
installer='zip'
distDir=releaseDir + "Admin4-%s-Linux"
else:
print ("Platform %s not supported" % platform)
sys.exit(1)
sys.argv.insert(1, installer)
if installer == "srcUpdate":
checkGitCommits=True
distDir=releaseDir + 'Admin4-%s-Src'
while '--addModule' in sys.argv:
i=sys.argv.index('--addModule')
del sys.argv[i]
addModules.append(sys.argv[i])
del sys.argv[i]
if '--distDir' in sys.argv:
i=sys.argv.index('--distDir')
del sys.argv[i]
distDir=sys.argv[i]
del sys.argv[i]
if '--skipGit' in sys.argv:
i=sys.argv.index('--skipGit')
del sys.argv[i]
checkGit=False
def appendChanged(lst, name):
if not recentlyChanged or name in recentlyChanged:
lst.append(name)
def cleanWxDir(wxdir):
remainder=0
for fn in os.listdir(wxdir):
path=os.path.join(wxdir, fn)
if os.path.isdir(path):
r=cleanWxDir(path)
if path.endswith(requiredDirs):
remainder += 1
else:
if not r:
shutil.rmtree(path)
remainder += r
elif path.endswith( ('.pyc', 'pyo')):
os.unlink(path)
return remainder
def searchFiles(searchdir, stripdirlen, addFiles=[]):
lst=[]
filenames=addFiles
for fn in os.listdir(searchdir):
if fn.startswith('.'):
continue
path=os.path.join(searchdir, fn)
if os.path.isdir(path):
lst.extend(searchFiles(path, stripdirlen))
else:
ext=path[path.rfind('.'):].lower()
if ext in filePatterns:
appendChanged(filenames, path)
if filenames:
lst.append( (searchdir[stripdirlen:], filenames) )
return lst
def readVersion():
version=None
try:
with open('__version.py') as f:
verSrc=f.read()
exec (verSrc)
except:
pass
return version
def writeVersion():
# https://pythonhosted.org/GitPython/0.3.2/index.html
global versionTag, gitTag
if checkGit:
try:
if git.__version__ < "0.3":
print ("\nWARNING: GIT too old, must be >0.3\n\n")
return False
except:
print ("\nWARNING: No GIT installed\n\n")
return False
repo=git.Repo(os.path.dirname(os.path.abspath(sys.argv[0])))
tags={}
for t in repo.tags:
tags[str(t.commit)] = t
lastOriginCommit=repo.commit('origin/master')
lastCommit=repo.commit('master')
def findTag(c):
if str(c) in tags:
return tags[str(c)]
if c.parents:
for c in c.parents:
tag=findTag(c)
if tag:
return tag
return None
tag=findTag(lastCommit)
if tag:
gitTag=versionTag=tag.name
if checkGitCommits:
for commit in repo.iter_commits("%s..HEAD" % versionTag):
for change in commit.diff(tag.commit):
path=change.a_blob.path
if path not in recentlyChanged:
recentlyChanged.append(path)
with open("__version.py", "w") as f:
f.write("# Automatically created from GIT by createBundle.\n# Do not edit manually!\n\n")
f.write("version='%s'\n" % tag.name)
f.write("requiredAdmVersion='%s'\n" % requiredAdmVersion)
f.write("tagDate='%s'\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(tag.commit.committed_date)))
f.write("revDate='%s'\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(lastOriginCommit.committed_date)))
f.write("modDate='%s'\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(lastCommit.committed_date)))
if not INSTALLER_DEBUG and (repo.is_dirty() or str(lastCommit) != str(lastOriginCommit)):
dockerSuffix="-Upd"
f.write("revLocalChange=True\n")
else:
f.write("revLocalChange=False\n")
if not INSTALLER_DEBUG and str(lastOriginCommit) != str(tag.commit):
f.write("revOriginChange=True\n")
else:
f.write("revOriginChange=False\n")
if not INSTALLER_DEBUG and repo.is_dirty():
versionTag="tmp"
print("PRERELEASE Version:", gitTag)
f.write("revDirty=True\n")
else:
print("ERELEASE Version:", gitTag)
f.write("revDirty=False\n")
f.write("standardInstallDir='%s'\n" % standardInstallDir)
if installer in ['docker']:
f.write("frozen=True\n")
return not INSTALLER_DEBUG and repo.is_dirty()
else:
print ("No tags found")
return True
versionTag=readVersion()
if versionTag != None:
print ("\nWARNING: using existing __version.py file.")
return False
else:
print ("\nWARNING: No __version file!")
sys.exit()
# Start of code
if writeVersion():
print ("\nWARNING: Repository has uncommitted data\n\n")
sys.skipSetupInit=True
data_files=[]
admResources=[]
def checkAddItem(fn, stripdirlen=0):
if os.path.isdir(fn):
addFiles=[]
if os.path.exists("%s/_requires.py" % fn):
mod=__import__("%s._requires" % fn)
try:
requires=getattr(mod, "_requires")
rq=requires.GetPrerequisites(True)
if rq:
if isinstance(rq, str):
rq=rq.split(' ')
requiredMods.extend(rq)
packages.extend(rq)
if hasattr(requires, 'moreFiles'):
# requires.morefiles must be a list of filenames located in the module dir; no subdir allowed
for mf in requires.moreFiles:
appendChanged(addFiles, os.path.join(fn, mf))
except: pass
data_files.extend(searchFiles(fn, stripdirlen, addFiles))
else:
if fn.startswith('ctl_') and fn.endswith('.py'):
appendChanged(admResources, fn)
else:
ext=fn[fn.rfind('.'):].lower()
if ext in filePatterns:
appendChanged(admResources, fn)
for fn in os.listdir("."):
if fn.startswith('.') or fn in ignoredirs or fn in ignoredfiles or os.path.islink(fn):
continue
checkAddItem(fn)
for fn in addModules:
fn=os.path.abspath(fn)
checkAddItem(fn, len(os.path.dirname(fn))+1)
for file in moreFiles:
appendChanged(admResources, file)
data_files.append( (".", admResources) )
data_files.reverse()
requiredDirs = tuple(d.replace('.', '/') for d in requiredMods)
packages.extend(requiredMods)
packages=sorted(set(packages))
if not installer in ['docker']:
if distDir.find('%s') >=0:
distDir = distDir % versionTag
elif versionTag:
distDir += "-%s" % versionTag
print ("Requirements detected:", ", ".join(packages))
if installer == 'srcUpdate' or platform == 'Linux':
if recentlyChanged:
distDir = distDir[:-4] + "+%s-Upd" % time.strftime("%y%m%d", time.localtime(time.time()))
print ("Collecting update into %s" % distDir)
try:
shutil.rmtree(distDir)
os.mkdir(distDir)
except:
pass
try:
os.mkdir(distDir)
except:
pass
for d in data_files:
if d[0] == '.':
destDir = distDir
else:
destDir=os.path.join(distDir, d[0])
os.mkdir(destDir)
for file in d[1]:
shutil.copy2(file, destDir)
if recentlyChanged:
shutil.copy2('__version.py', distDir)
else:
print ("Creating package in %s" %distDir)
# os.environ['DISTUTILS_DEBUG'] = 'true'
import distutils.core
info=dict( data_files=data_files,
name=appName,
author=version.author,
license=version.copyright,
version=str(version.version),
options={'py2exe': {'packages': packages,
'includes': includes,
'excludes': excludes,
'dist_dir': distDir
},
'py2app': {'argv_emulation': False,
'packages': packages,
'includes': includes,
'iconfile': '%s.icns' % appName,
'dist_dir': distDir,
'plist': { 'CFBundleIdentifier': 'org.%s' % appName.lower() }
},
'build': {'build_base': buildDir},
}
)
if platform == "Windows":
__import__(installer)
distutils.core.setup(windows=[{'script': appEntry, 'icon_resources': [(1, '%s.ico' % appName)] }], **info)
elif platform == "Darwin":
py2app=__import__(installer)
if tuple(map(int, py2app.__version__.split('.'))) < (0, 27):
raise Exception("py2app too old: must be 0.8 or newer")
# if you're getting "Cannot include subpackages using the 'packages' option" py2app is too old
distutils.core.setup(app=[appEntry], **info)
libdir='%s/%s.app/Contents/Resources/lib/python%d.%d/wx' % (distDir, appName, sys.version_info.major, sys.version_info.minor)
if not '-A' in sys.argv:
cleanWxDir(libdir)
# not necessary any more shutil.rmtree('%s/%s.app/Contents/Resources/mpl-data' % (distDir, appName))
# elif platform == "Linux":
if installer == 'py2app':
print ("\nWriting dmg.")
distribPackage=distDir+".dmg"
with subprocess.Popen([
"hdiutil", "create",
"-format", "UDBZ",
"-volname", appName,
"-noanyowners", "-nospotlight",
"-srcfolder", distDir,
distribPackage
]) as proc:
proc.communicate()
if proc.returncode:
sys.exit(8)
elif installer == "docker":
createSha=False
dockerTag="%s:%s%s" % (dockerRepo, gitTag, dockerSuffix)
print("\nCreating docker container", dockerTag)
# sys.exit(8)
with subprocess.Popen([
"docker", "build",
"--file", "docker/Dockerfile",
"--tag", dockerTag,
"release"
]) as proc:
proc.communicate()
if proc.returncode:
sys.exit(8)
with subprocess.Popen([
"docker", "tag",
dockerTag, "%s:latest" % dockerRepo
]) as proc:
proc.communicate()
if proc.returncode:
sys.exit(8)
else:
def zipwrite(path, stripLen):
fzip.write(path, path[stripLen:])
if os.path.isdir(path):
for f in os.listdir(path):
if f in ['.', '..']:
continue
zipwrite(os.path.join(path, f), stripLen)
print ("\nWriting zip.")
distribPackage=distDir+".zip"
fzip=zipfile.ZipFile(distribPackage, 'w', zipfile.ZIP_DEFLATED)
zipwrite(distDir, len(os.path.dirname(distDir))+1)
fzip.close()
if createSha:
with open(distribPackage, 'rb') as f:
data=f.read(102400)
m=hashlib.sha1()
while data != b"":
m.update(data)
data=f.read(102400)
digest=m.hexdigest()
with open(distDir+".sha1", 'w') as f:
f.write(digest)
print ("SHA1 Hash for %s: %s" % (distribPackage, digest))
print ("\ndone.")