@@ -61,6 +61,28 @@ def getFirstPlist(textString):
6161
6262
6363def attachdmg (dmgpath ):
64+ global dmg_was_mounted
65+ info_cmd = ["hdiutil" , "info" , "-plist" ]
66+ info_result = cmdexec (info_cmd )
67+ if info_result ["return_code" ] == 0 :
68+ # parse the plist output
69+ (theplist , alltext ) = getFirstPlist (info_result ["stdout" ])
70+ info_dict = plistlib .readPlistFromString (theplist )
71+ volpaths = []
72+ if "images" in info_dict .keys ():
73+ for y in info_dict ["images" ]:
74+ if "image-path" in y .keys ():
75+ if os .path .samefile (y ["image-path" ], dmgpath ):
76+ for x in y .get ("system-entities" ):
77+ if "mount-point" in x .keys ():
78+ volpaths .append (x ["mount-point" ])
79+ dmg_was_mounted = True
80+ return volpaths
81+ else :
82+ print "error getting hdiutil info"
83+ print "(%d, %s)" % (result ["returncode" ], result ["stderr" ])
84+ cleanup_and_exit (1 )
85+
6486 attachcmd = ["/usr/bin/hdiutil" ,
6587 "attach" ,
6688 dmgpath ,
@@ -83,7 +105,7 @@ def attachdmg(dmgpath):
83105 else :
84106 print "error mounting disk image"
85107 print "(%d, %s)" % (result ["returncode" ], result ["stderr" ])
86- exit (1 )
108+ cleanup_and_exit (1 )
87109
88110
89111def detachpaths (volpaths ):
@@ -104,7 +126,7 @@ def finditemswithextension(dirpath, item_extension):
104126 foundapps .append (os .path .join (dirpath , x ))
105127 else :
106128 print "path %s does not exist" % dirpath
107- exit (1 )
129+ cleanup_and_exit (1 )
108130 return foundapps
109131
110132
@@ -113,7 +135,7 @@ def appNameAndVersion(app_path):
113135 if not os .path .exists (info_path ):
114136 print "Application at path %s does not have Info.plist" % app_path
115137 # TODO: cleanup volumes here
116- exit (1 )
138+ cleanup_and_exit (1 )
117139 info_plist = plistlib .readPlist (info_path )
118140 app_name = info_plist .get ("CFBundleName" , None )
119141 app_identifier = info_plist .get ("CFBundleIdentifier" , None )
@@ -123,6 +145,18 @@ def appNameAndVersion(app_path):
123145 return (app_name , app_identifier , app_version )
124146
125147
148+ def cleanup_and_exit (returncode ):
149+ global dmgvolumepaths
150+ global dmg_was_mounted
151+ global tmp_path
152+
153+ if not dmg_was_mounted :
154+ detachpaths (dmgvolumepaths )
155+ if tmp_path is not None :
156+ shutil .rmtree (tmp_path )
157+ exit (returncode )
158+
159+
126160if __name__ == "__main__" :
127161
128162 # for convenience link to argparse tutorial:
@@ -145,6 +179,10 @@ if __name__ == "__main__":
145179 # remove trailing '/' from path
146180 item_path = string .rstrip (args .item_path , '/' )
147181
182+ if item_path .startswith ('~' ):
183+ item_path = os .path .expanduser (item_path )
184+ item_path = os .path .abspath (item_path )
185+
148186 # get file extension
149187 (item_basename , item_extension ) = os .path .splitext (item_path )
150188 item_extension = string .lstrip (item_extension , '.' )
@@ -164,6 +202,7 @@ if __name__ == "__main__":
164202
165203 dmgvolumepaths = []
166204 tmp_path = None
205+ dmg_was_mounted = False
167206
168207 # if item is a dmg, mount it and find useful contents
169208 if item_extension == 'dmg' :
@@ -174,13 +213,11 @@ if __name__ == "__main__":
174213 foundapps .extend (moreapps )
175214 if len (foundapps ) == 0 :
176215 print "Could not find an application!"
177- detachpaths (dmgvolumepaths )
178- exit (1 )
216+ cleanup_and_exit (1 )
179217 elif len (foundapps ) > 1 :
180218 print "Found too many Applications! Can't decide!"
181219 print foundapps
182- detachpaths (dmgvolumepaths )
183- exit (1 )
220+ cleanup_and_exit (1 )
184221
185222 app_path = foundapps [0 ]
186223
@@ -192,17 +229,15 @@ if __name__ == "__main__":
192229 if result ["return_code" ] != 0 :
193230 print "An error occured while unzipping:"
194231 print "%d, %s" % (result ["return_code" ], result ["stderr" ])
195- exit (1 )
232+ cleanup_and_exit (1 )
196233 foundapps = finditemswithextension (tmp_path , 'app' )
197234 if len (foundapps ) == 0 :
198235 print "Could not find an application!"
199- shutil .rmtree (tmp_path )
200- exit (1 )
236+ cleanup_and_exit (1 )
201237 elif len (foundapps ) > 1 :
202238 print "Found too many Applications! Can't decide!"
203239 print foundapps
204- shutil .rmtree (tmp_path )
205- exit (1 )
240+ cleanup_and_exit (1 )
206241
207242 app_path = foundapps [0 ]
208243
@@ -228,11 +263,9 @@ if __name__ == "__main__":
228263
229264 logger (result ["stdout" ], 1 )
230265 if result ["return_code" ] != 0 :
231- logger ( "Error Code: " + result ["return_code" ], 1 )
232- logger ( result ["stderr" ], 1 )
266+ print "Error Code: " + result ["return_code" ]
267+ print result ["stderr" ]
233268 else :
234269 print pkg_name
235270
236- detachpaths (dmgvolumepaths )
237- if tmp_path is not None :
238- shutil .rmtree (tmp_path )
271+ cleanup_and_exit (0 )
0 commit comments