@@ -7,6 +7,7 @@ import subprocess
7
7
import plistlib
8
8
import tempfile
9
9
import shutil
10
+ import stat
10
11
11
12
12
13
quickpkg_version = '0.3'
@@ -179,22 +180,29 @@ def cleanup_and_exit(returncode):
179
180
detachpaths (dmgvolumepaths )
180
181
if tmp_path is not None :
181
182
shutil .rmtree (tmp_path )
183
+ if tmp_scripts_path is not None :
184
+ shutil .rmtree (tmp_scripts_path )
182
185
exit (returncode )
183
186
184
187
185
188
if __name__ == "__main__" :
186
189
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""" )
192
193
193
194
194
195
# takes a path as input
195
196
parser .add_argument ('item_path' , help = "path to the installer item" )
196
197
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" )
198
206
199
207
parser .add_argument ("-v" , "--verbosity" , action = "count" , default = 0 , help = "controls amount of logging output (max -vvv)" )
200
208
parser .add_argument ('--version' , help = 'prints the version' , action = 'version' , version = quickpkg_version )
@@ -228,6 +236,7 @@ if __name__ == "__main__":
228
236
dmgvolumepaths = []
229
237
tmp_path = None
230
238
dmg_was_mounted = False
239
+ tmp_scripts_path = None
231
240
232
241
# if item is a dmg, mount it and find useful contents
233
242
if item_extension == 'dmg' :
@@ -285,14 +294,55 @@ if __name__ == "__main__":
285
294
"--install-location" , "/Applications" ,
286
295
pkg_name ]
287
296
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 )
289
339
pkgcmd .extend (["--scripts" , args .scripts ])
290
340
291
341
result = cmdexec (pkgcmd )
292
342
293
343
logger (result ["stdout" ], 1 )
294
344
if result ["return_code" ] != 0 :
295
- print "Error Code: " + result ["return_code" ]
345
+ print "Error Code: %d " % result ["return_code" ]
296
346
print result ["stderr" ]
297
347
else :
298
348
print pkg_name
0 commit comments