Skip to content

Commit 76f0dc3

Browse files
committed
added --[no-]clean options and now creates payload folder
1 parent f3bc9e2 commit 76f0dc3

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

quickpkg

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,12 @@ def cleanup_and_exit(returncode):
319319
global dmgvolumepaths
320320
global dmg_was_mounted
321321
global tmp_path
322-
323-
if not dmg_was_mounted:
324-
detachpaths(dmgvolumepaths)
325-
if tmp_path is not None:
326-
shutil.rmtree(tmp_path)
327-
if tmp_scripts_path is not None:
328-
shutil.rmtree(tmp_scripts_path)
322+
323+
if args.clean:
324+
if not dmg_was_mounted:
325+
detachpaths(dmgvolumepaths)
326+
if tmp_path is not None:
327+
shutil.rmtree(tmp_path)
329328
exit(returncode)
330329

331330

@@ -355,6 +354,10 @@ if __name__ == "__main__":
355354
If this is a directory, then the
356355
package will be created with the default filename {name}-{version}.pkg''')
357356

357+
parser.add_argument('--clean', dest='clean', action='store_true', help="clean up temp files (default)")
358+
parser.add_argument('--no-clean', dest='clean', action='store_false', help=" do NOT clean up temp files")
359+
parser.set_defaults(clean=True)
360+
358361
parser.add_argument("-v", "--verbosity", action="count", default=0, help="controls amount of logging output (max -vvv)")
359362
parser.add_argument('--version', help='prints the version', action='version', version=quickpkg_version)
360363

@@ -388,6 +391,9 @@ if __name__ == "__main__":
388391
tmp_path = None
389392
dmg_was_mounted = False
390393
tmp_scripts_path = None
394+
tmp_path = tempfile.mkdtemp()
395+
payload_path = os.path.join(tmp_path, "payload")
396+
os.makedirs(payload_path)
391397

392398
# if item is a dmg, mount it and find useful contents
393399
if item_extension == 'dmg':
@@ -403,19 +409,17 @@ if __name__ == "__main__":
403409
print "Found too many Applications! Can't decide!"
404410
print foundapps
405411
cleanup_and_exit(1)
406-
407-
app_path = foundapps[0]
408-
412+
409413
# if item is zip, unzip to tmp location and find useful contents
410414
if item_extension == 'zip':
411-
tmp_path = tempfile.mkdtemp()
412-
unzip_cmd = ["/usr/bin/unzip", "-d", tmp_path, item_path]
415+
unarchive_path = os.path.join(tmp_path, "unarchive")
416+
unzip_cmd = ["/usr/bin/unzip", "-d", unarchive_path, item_path]
413417
result = cmdexec(unzip_cmd)
414418
if result["return_code"] != 0:
415419
print "An error occured while unzipping:"
416420
print "%d, %s" % (result["return_code"], result["stderr"])
417421
cleanup_and_exit(1)
418-
foundapps = finditemswithextension(tmp_path, 'app')
422+
foundapps = finditemswithextension(unarchive_path, 'app')
419423
if len(foundapps) == 0:
420424
print "Could not find an application!"
421425
cleanup_and_exit(1)
@@ -424,15 +428,35 @@ if __name__ == "__main__":
424428
print foundapps
425429
cleanup_and_exit(1)
426430

427-
app_path = foundapps[0]
431+
logger("Found application: %s" % foundapps[0], 1)
428432

429-
logger("Found application: %s" % app_path, 1)
433+
# copy found app to payload folder
434+
app_name = os.path.basename(foundapps[0])
435+
app_path = os.path.join(payload_path, app_name)
436+
shutil.copytree(foundapps[0], app_path)
430437

431438
# extract version and other metadata
432439
(app_name, app_identifier, app_version) = appNameAndVersion(app_path)
433440

434441
logger("Name: %s, ID: %s, Version: %s" % (app_name, app_identifier, app_version), 1)
435442

443+
# create the component plist
444+
component_plist = os.path.join(tmp_path, app_identifier) + ".plist"
445+
analyzecmd = ["/usr/bin/pkgbuild",
446+
"--analyze",
447+
"--root", payload_path,
448+
"--identifier", app_identifier,
449+
"--version", app_version,
450+
"--install-location", "/Applications",
451+
component_plist]
452+
result = cmdexec(analyzecmd)
453+
454+
logger(result["stdout"], 1)
455+
if result["return_code"] != 0:
456+
print "Error Code: %d " % result["return_code"]
457+
print result["stderr"]
458+
cleanup_and_exit(1)
459+
436460
pkg_name = "{name}-{version}.pkg"
437461
if args.output:
438462
if os.path.isdir(args.output):
@@ -449,7 +473,8 @@ if __name__ == "__main__":
449473

450474
# run pkgutil to build result
451475
pkgcmd = ["/usr/bin/pkgbuild",
452-
"--component", app_path,
476+
"--root", payload_path,
477+
"--component-plist", component_plist,
453478
"--identifier", app_identifier,
454479
"--version", app_version,
455480
"--install-location", "/Applications",
@@ -460,7 +485,9 @@ if __name__ == "__main__":
460485
cleanup_and_exit(1)
461486

462487
if args.postinstall or args.preinstall:
463-
tmp_scripts_path = tempfile.mkdtemp()
488+
tmp_scripts_path = os.path.join(tmp_dir, "scripts")
489+
os.makedirs(tmp_scripts_path)
490+
464491
if args.scripts:
465492
logger("copying %s to tmp scripts folder %s" % (args.scripts, tmp_scripts_path), 1)
466493
shutil.rmtree(tmp_scripts_path)

todo.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
- identify shell scripts and build a payload free package
2+
- problems with this:
3+
- how to determine if the given file is a script? executable bit? parse the #! ?
4+
- how to choose id and version? (for true payload free packages, this may not matter since they don't leave a receipt anyway?)
15
- use some preference setting to determine default package name syntax
26
- signature support
3-
- identify shell scripts and build a payload free package
4-
- support for tar, gzip and bzip
5-
? other possible file formats: fonts, prefpanes, Safari extensions?
7+
- support for tar, gzip and bzip and xip
8+
- identify fonts and put them in /Library/Fonts
9+
? other possible file formats: prefpanes, Safari extensions?
610
? identify app just by name or id (could use: mdfind "kMDItemKind == 'Application' && kMDItemDisplayName == 'iTunes'")
711
? identify mobileconfigs and build a package installer if make-profile-pkg is present

0 commit comments

Comments
 (0)