This repository was archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·520 lines (419 loc) · 13.6 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·520 lines (419 loc) · 13.6 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!//usr/bin/python3
"""
build.py
======================
Builds source code in src directory
"""
import sys
import os
import subprocess
import shutil
import subprocess
import hashlib
import random
import pdb
## CONSTANTS START ##
# Arguments
RELEASE_ARG: str = "release"
DEBUG_ARG: str = "debug"
PACK_ARG: str = "pack"
BUILD_ARG: str = "build"
HELP_ARG: str = "--help"
# See if we are in debug mode
XP_BUILD: str = "xp"
if sys.platform == "linux" or sys.platform == "linux2":
PLATFORM_NAME = "linux"
elif sys.platform == "darwin":
PLATFORM_NAME = "mac"
elif sys.platform == "win32":
PLATFORM_NAME = "windows"
XP_BUILD = "{}.exe".format(XP_BUILD) # Add .exe for xp build
# Expected path for sym link
# we need this to exist for us to build
if sys.platform == "win32":
XPRO_LINK_PATH: str = "C:\\Library\\xPro"
else:
XPRO_LINK_PATH: str = "/Library/xPro"
SCRIPT_NAME: str = os.path.basename(sys.argv[0])
SCRIPT_PATH: str = os.path.realpath(os.path.dirname(sys.argv[0]))
XPRO_PATH: str = SCRIPT_PATH
BIN_NAME: str = "bin"
BIN_PATH: str = os.path.join(XPRO_PATH, BIN_NAME)
DOCS_PATH: str = os.path.join(XPRO_PATH, "docs")
DEVENV_SCRIPT: str = "devenv.py"
PACKAGES_PATH: str = os.path.join(XPRO_PATH, "packages")
ARCHIVE_TYPE: str = "zip" # 'zip', 'tar', 'gztar', 'bztar', or 'xztar'
HASH_FILE_PATH: str = os.path.join(XPRO_PATH, ".hash")
# Default version string
DEFAULT_VERSION: str = "1.0"
## CONSTANTS END ##
## FUNCTION START ##
def help():
"""
help
===================
Prints help menu
"""
print("usage: {scriptname} [ {debug} | {release} ] [ {build} ] [ {pack} ] [ {help} ]".format(
scriptname = SCRIPT_NAME,
debug = DEBUG_ARG,
help = HELP_ARG,
release = RELEASE_ARG,
pack = PACK_ARG,
build = BUILD_ARG
))
print()
print("\t{debug}\tRuns debug type".format(debug=DEBUG_ARG))
print("\t{release}\tRuns release type".format(release=RELEASE_ARG))
print("\t{build}\tBuilds the xp binary for either release or debug, depending on the specified. Default is release".format(build=BUILD_ARG))
print("\t{pack}\tPacks build and other deliverables".format(pack=PACK_ARG))
print()
print("See '{scriptname} {help}' for an overview of the system.".format(
scriptname = SCRIPT_NAME,
help = HELP_ARG
))
def setup():
"""
setup
============
Sets up the build environment
"""
print("Updating submodules...")
result = subprocess.Popen(
[ "git", "submodule", "update", "--recursive" ],
stderr = subprocess.STDOUT
).wait()
return result
def createBuildHash():
"""
createBuildHash
===============
Calculates a random hash and writes the result into a random file that the makefile can get the contents from
"""
result: int = 0
# Create the hash string
hashStr: str = hashlib.sha256(
str(
random.getrandbits(256)
).encode('utf-8')
).hexdigest()[0:40]
if hashStr == None:
print("The hash was None")
result = 1
elif len(hashStr) == 0:
print("Received an empty hash")
result = 1
if result == 0:
f = open(HASH_FILE_PATH, "w")
if f == None:
result = 1
print("There was a problem trying to open the hash file to write the build hash, file path: ", HASH_FILE_PATH)
else:
f.write(hashStr)
f.close()
if os.path.exists(HASH_FILE_PATH) is False:
result = 1
print("Could not create the hash file at:", HASH_FILE_PATH)
return result
def cleanUpBuildHash():
"""
cleanUpBuildHash
=================
Removes the build hash file
"""
result: int = 0
if os.path.exists(HASH_FILE_PATH) is False:
result = 1
print("The build hash file does not exist")
else:
os.remove(HASH_FILE_PATH)
if os.path.exists(HASH_FILE_PATH):
result = 1
print("The hash file path at {} could not be removed".format(HASH_FILE_PATH))
return result
def build():
"""
build
==================
"""
result: int = 0
currDir: str = os.curdir
buildTypeString: str = ""
buildFolder: str = ""
buildPath: str = ""
buildName: str = XP_BUILD
if result == 0:
if (DEBUG_ARG in sys.argv) and (RELEASE_ARG in sys.argv):
print("Please choose either '{}' or '{}'".format(
DEBUG_ARG, RELEASE_ARG
))
result = 1
elif DEBUG_ARG in sys.argv:
buildTypeString = "Debug"
buildName = "debug-{}".format(buildName)
elif RELEASE_ARG in sys.argv:
buildTypeString = "Release"
else:
# defaulting to release
buildTypeString = "Release"
# Make sure we have the path to build
if result == 0:
if os.path.exists(XPRO_LINK_PATH) is False:
result = 1
print("We need to have '{}' to build. Please run '{}'".format(
XPRO_LINK_PATH,
DEVENV_SCRIPT
))
# Change xpro
os.chdir(XPRO_PATH)
# Setup the dev environment
if result == 0:
result = setup()
if result == 0:
if os.path.exists(BIN_PATH) is False:
os.mkdir(BIN_PATH)
if os.path.exists(BIN_PATH) is False:
result = 1
print("Could not create bin folder")
if result == 0:
buildFolder = "{} ({})".format(buildTypeString, PLATFORM_NAME)
buildPath = os.path.join(XPRO_PATH, "src", "xpro-cli", buildFolder)
# Change to source directory
os.chdir(buildPath)
# Clean
print("Cleaning up xPro CLI")
result = subprocess.Popen(
[ "make", "clean" ],
stderr = subprocess.STDOUT
).wait()
# Create the build hash that the makefile
# will read
if result == 0:
result = createBuildHash()
# Build all
if result == 0:
print("Building xPro CLI")
result = subprocess.Popen(
[ "make", "all" ],
stderr = subprocess.STDOUT
).wait()
if result == 0:
path = os.path.join(BIN_PATH, buildName)
if os.path.exists(path):
print("Removing old build:", path)
os.remove(path)
path = shutil.copy(buildName, BIN_PATH)
if path is None:
result = 1
else:
print("Build copy:", path)
# remove the build hash file
if result == 0:
result = cleanUpBuildHash()
# Go back to dir
os.chdir(currDir)
return result
def createCopySet(destination: str):
"""
createCopySet
====================
Creates an array of copy tasks we will be doing
Parameters
------------------
destination: The path we will be copying everything to
Return
------------------
[[source0, destination], [source1, destination], ...]
"""
result: list = list()
error: int = 0
tempPath: str = ""
tempDest: str = ""
buildName: str = XP_BUILD
# Scripts
if error == 0:
tempPath = os.path.join(XPRO_PATH, "scripts")
tempDest = os.path.join(destination, "scripts")
if os.path.exists(tempPath) is False:
print("{} does not exist!".format(tempPath))
result = 1
else:
result.append([tempPath, tempDest])
# Schemas
if error == 0:
tempPath = os.path.join(XPRO_PATH, "schema")
tempDest = os.path.join(destination, "schema")
if os.path.exists(tempPath) is False:
print("{} does not exist!".format(tempPath))
result = 1
else:
result.append([tempPath, tempDest])
# Readme
if error == 0:
tempPath = os.path.join(DOCS_PATH, "README.md")
if os.path.exists(tempPath) is False:
print("{} does not exist!".format(tempPath))
result = 1
else:
result.append([tempPath, destination])
# Binary
if error == 0:
if DEBUG_ARG in sys.argv:
buildName = "debug-{}".format(buildName)
tempDest = os.path.join(destination, BIN_NAME)
if os.path.exists(tempPath) is False:
print("{} does not exist!".format(tempPath))
result = 1
else:
result.append([BIN_PATH, tempDest])
return result, error
def getVersionString():
"""
getVersionString
==============
Gets the version string from the git repo
The default version string will be 1.0
"""
result: str = ""
output: list
process = subprocess.run(
['git', 'tag', '-l', '--sort=-creatordate'],
stdout = subprocess.PIPE,
text = True
)
if process.returncode != 0:
print(
"Error getting version, {code}. Defaulting to {version}",
process.returncode,
DEFAULT_VERSION
)
result = DEFAULT_VERSION
else:
output = process.stdout
if len(output) == 0:
result = DEFAULT_VERSION
print("No values were returned, will default to:", DEFAULT_VERSION)
else:
result = output.split('\n')[0]
return result
def pack():
"""
pack
==================
"""
result: int = 0
tmpPackagePath: str = os.path.join(PACKAGES_PATH, "tmp")
copySet: list = list()
versionString: str = ""
currDir: str = os.curdir
outPath: str = ""
archiveName: str = ""
archiveDestination: str = ""
# Create the packages directory if it already wasn't built
if result == 0:
if os.path.exists(PACKAGES_PATH) is False:
os.mkdir(PACKAGES_PATH)
if os.path.exists(PACKAGES_PATH) is False:
result = 1
print("Could not create package directory")
else:
os.chdir(PACKAGES_PATH)
# Make sure the tmp directory does not already exist
if result == 0:
if os.path.exists(tmpPackagePath):
shutil.rmtree(tmpPackagePath)
if os.path.exists(tmpPackagePath):
result = 1
print("Could not remove tmp package directory")
# Create the direcory we are going to put all the release
# files into
if result == 0:
os.mkdir(tmpPackagePath)
if os.path.exists(tmpPackagePath) is False:
result = 1
print("Could not create package directory")
# Create sub directory that I want users to see when unarchiving
if result == 0:
# Gets the git version
versionString = getVersionString()
# Create the name of the archive
archiveName = "xPro-{version}".format(
version = versionString
)
archiveDestination = os.path.join(tmpPackagePath, archiveName)
os.mkdir(archiveDestination)
if os.path.exists(archiveDestination) is False:
result = 1
print("Could not create package directory")
# Create the copy tasks
if result == 0:
copySet, result = createCopySet(archiveDestination)
# Copy all the files
if result == 0:
print("Packing:")
for command in copySet:
if len(command) == 2:
print(" - {}".format(os.path.basename(command[0])))
if os.path.isdir(command[0]):
shutil.copytree(command[0], command[1])
else:
shutil.copy(command[0], command[1])
else:
result = 1
print("Unexpected amount of arguments")
break
# Create final product path
if result == 0:
outPath = os.path.join(
PACKAGES_PATH,
"{base}.{type}".format(
base = archiveName,
type = ARCHIVE_TYPE
)
)
# Removing old archive
if os.path.exists(outPath):
print("Removing old package archive:", outPath)
os.remove(outPath)
if os.path.exists(outPath):
result = 1
if result == 0:
# Create the zip archive
shutil.make_archive(
os.path.splitext(outPath)[0],
ARCHIVE_TYPE,
tmpPackagePath
)
print("Archive:", outPath)
# Make sure the tmp directory does not already exist
if result == 0:
if os.path.exists(tmpPackagePath):
shutil.rmtree(tmpPackagePath)
if os.path.exists(tmpPackagePath):
result = 1
print("Could not remove tmp package directory")
os.chdir(currDir)
return result
def main():
status: int = 0
building: bool = True
packing: bool = True
if HELP_ARG in sys.argv:
help()
else:
# Read the arguments
# If the user specified packaging but did not say we need
# build, then we will not build
if PACK_ARG in sys.argv and BUILD_ARG not in sys.argv:
building = False
# Same thing as above
if BUILD_ARG in sys.argv and PACK_ARG not in sys.argv:
packing = False
if status == 0 and building:
status = build()
if status == 0 and packing:
status = pack()
sys.exit(status)
## FUNCTION END ##
if __name__ == "__main__":
main()