Skip to content

Commit 19ee041

Browse files
committed
added --preinstall and --postinstall options
1 parent 35b1414 commit 19ee041

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

quickpkg

+58-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import subprocess
77
import plistlib
88
import tempfile
99
import shutil
10+
import stat
1011

1112

1213
quickpkg_version = '0.3'
@@ -179,22 +180,29 @@ def cleanup_and_exit(returncode):
179180
detachpaths(dmgvolumepaths)
180181
if tmp_path is not None:
181182
shutil.rmtree(tmp_path)
183+
if tmp_scripts_path is not None:
184+
shutil.rmtree(tmp_scripts_path)
182185
exit(returncode)
183186

184187

185188
if __name__ == "__main__":
186189

187-
parser = argparse.ArgumentParser(description="Attempts to build a pkg from the input.",
188-
epilog="""Installer item can be a dmg, zip, or app.
189-
190-
Example:
191-
quickpkg /path/to/installer_item""", formatter_class=argparse.RawTextHelpFormatter)
190+
parser = argparse.ArgumentParser(description="""Attempts to build a pkg from the input.
191+
Installer item can be a dmg, zip, or app.""",
192+
epilog="""Example: quickpkg /path/to/installer_item""")
192193

193194

194195
# takes a path as input
195196
parser.add_argument('item_path', help="path to the installer item")
196197

197-
parser.add_argument('--scripts', help="path to a folder with scripts that will be passed to 'pkgbuild'")
198+
scripts_group = parser.add_argument_group('Installation Scripts',
199+
'''These options will set the installation scripts. You pass an entire folder of scripts,
200+
just like the option of `pkgbuild` or you can give a file for the preinstall or postinstall
201+
scripts respectively. If you give both the --scripts and either one or both of --preinstall
202+
and --postinstall, quickpkg will attempt to merge, but throw an error if it cannot.''')
203+
scripts_group.add_argument('--scripts', help="path to a folder with scripts")
204+
scripts_group.add_argument('--preinstall', help="path to the preinstall script")
205+
scripts_group.add_argument('--postinstall', help="path to the postinstall script")
198206

199207
parser.add_argument("-v", "--verbosity", action="count", default=0, help="controls amount of logging output (max -vvv)")
200208
parser.add_argument('--version', help='prints the version', action='version', version=quickpkg_version)
@@ -228,6 +236,7 @@ if __name__ == "__main__":
228236
dmgvolumepaths = []
229237
tmp_path = None
230238
dmg_was_mounted = False
239+
tmp_scripts_path = None
231240

232241
# if item is a dmg, mount it and find useful contents
233242
if item_extension == 'dmg':
@@ -285,14 +294,55 @@ if __name__ == "__main__":
285294
"--install-location", "/Applications",
286295
pkg_name]
287296

288-
if args.scripts:
297+
if args.scripts and not os.path.exists(args.scripts):
298+
print "scripts folder %s does not exist!" % args.scripts
299+
cleanup_and_exit(1)
300+
301+
if args.postinstall or args.preinstall:
302+
tmp_scripts_path = tempfile.mkdtemp()
303+
if args.scripts:
304+
logger("copying %s to tmp scripts folder %s" % (args.scripts, tmp_scripts_path), 1)
305+
shutil.rmtree(tmp_scripts_path)
306+
shutil.copytree(args.scripts, tmp_scripts_path)
307+
if args.postinstall:
308+
if not os.path.exists(args.postinstall):
309+
print "postinstall file %s does not exist!" % args.postinstall
310+
cleanup_and_exit(1)
311+
postinstall_path = os.path.join(tmp_scripts_path, "postinstall")
312+
if os.path.exists(postinstall_path):
313+
print "postinstall script already exists in %s" % args.scripts
314+
cleanup_and_exit(1)
315+
logger("copying %s to %s" % (args.postinstall, postinstall_path))
316+
shutil.copy2(args.postinstall, postinstall_path)
317+
os.chmod(postinstall_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
318+
stat.S_IRGRP | stat.S_IXGRP |
319+
stat.S_IROTH | stat.S_IXOTH)
320+
if args.preinstall:
321+
if not os.path.exists(args.preinstall):
322+
print "preinstall file %s does not exist!" % args.preinstall
323+
cleanup_and_exit(1)
324+
preinstall_path = os.path.join(tmp_scripts_path, "preinstall")
325+
if os.path.exists(preinstall_path):
326+
print "preinstall script already exists in %s" % args.scripts
327+
cleanup_and_exit(1)
328+
logger("copying %s to %s" % (args.preinstall, preinstall_path))
329+
shutil.copy2(args.preinstall, preinstall_path)
330+
os.chmod(preinstall_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
331+
stat.S_IRGRP | stat.S_IXGRP |
332+
stat.S_IROTH | stat.S_IXOTH)
333+
334+
if tmp_scripts_path:
335+
logger("scripts path: %s" % tmp_scripts_path)
336+
pkgcmd.extend(["--scripts", tmp_scripts_path])
337+
elif args.scripts:
338+
logger("scripts path: %s" % args.scripts)
289339
pkgcmd.extend(["--scripts", args.scripts])
290340

291341
result = cmdexec(pkgcmd)
292342

293343
logger(result["stdout"], 1)
294344
if result["return_code"] != 0:
295-
print "Error Code: " + result["return_code"]
345+
print "Error Code: %d " % result["return_code"]
296346
print result["stderr"]
297347
else:
298348
print pkg_name

testpost.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
/usr/bin/echo "test postinstall script"
4+
5+
exit 0

testpre.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
/usr/bin/echo "test preinstall script"
4+
5+
exit 0

0 commit comments

Comments
 (0)