1
- #!/usr/bin/python
1
+ #!/usr/bin/python3
2
2
3
3
# unpkg
4
4
# http://www.timdoug.com/unpkg/
5
5
# by timdoug[.com|@gmail.com]
6
6
#
7
7
# version notes:
8
8
#
9
+ # 4.8 -- 2022-03-20
10
+ # - Port to Python3 for macOS 12.3+
11
+ # https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes#Python
12
+ #
9
13
# 4.7 -- 2021-03-14
10
14
# - Native Apple Silicon support: rebuild w/ arm64 and x86_64 binaries
11
15
#
@@ -85,11 +89,11 @@ def get_extract_dir(pkg_path):
85
89
86
90
if os .path .exists (extract_dir ):
87
91
orig_extract_dir = extract_dir
88
- for i in xrange (1 , 1000 ):
92
+ for i in range (1 , 1000 ):
89
93
extract_dir = '%s-%s' % (orig_extract_dir , str (i ))
90
94
if not os .path .exists (extract_dir ): break
91
95
if i == 999 : # I sure hope this never happens...
92
- print 'Cannot establish appropriate extraction directory.'
96
+ print ( 'Cannot establish appropriate extraction directory.' )
93
97
sys .exit ()
94
98
95
99
return extract_dir
@@ -112,7 +116,7 @@ def extract_package(pkg_path, extract_dir):
112
116
if os .path .isdir (pkg_path ):
113
117
pax_path = find_pax (pkg_path )
114
118
if not pax_path :
115
- print 'Cannot find pax file. (not a valid package?)'
119
+ print ( 'Cannot find pax file. (not a valid package?)' )
116
120
return False
117
121
os .mkdir (extract_dir )
118
122
extract_prog = '/bin/pax -r < "%s"'
@@ -125,12 +129,12 @@ def extract_package(pkg_path, extract_dir):
125
129
else :
126
130
# no 'with' for compatibility with python 2.3 (10.4)
127
131
try :
128
- f = open (pkg_path , 'r ' )
129
- if f .read (4 ) != 'xar!' :
130
- print 'Not a valid package.'
132
+ f = open (pkg_path , 'rb ' )
133
+ if f .read (4 ) != b 'xar!' :
134
+ print ( 'Not a valid package.' )
131
135
return False
132
136
except IOError :
133
- print 'Cannot read package.'
137
+ print ( 'Cannot read package.' )
134
138
return False
135
139
else :
136
140
f .close ()
@@ -140,14 +144,14 @@ def extract_package(pkg_path, extract_dir):
140
144
141
145
payloads = []
142
146
for root , dirs , files in os .walk (temp_dir ):
143
- for file in filter ( lambda x : x == 'Payload' , files ) :
147
+ for file in [ x for x in files if x == 'Payload' ] :
144
148
payloads .append (os .path .join (root , file ))
145
149
146
150
os .mkdir (extract_dir )
147
151
extract_prog = '/usr/bin/gzcat "%s" | "' + CPIO_PATH + '" -i -m --quiet'
148
152
149
153
if len (payloads ) == 0 :
150
- print 'No payloads found.'
154
+ print ( 'No payloads found.' )
151
155
return False
152
156
elif len (payloads ) == 1 :
153
157
run_in_path (extract_prog % payloads [0 ], extract_dir )
@@ -164,11 +168,11 @@ def extract_package(pkg_path, extract_dir):
164
168
165
169
def main ():
166
170
for pkg_path in sys .argv [1 :]:
167
- print 'Extracting "%s"...' % os .path .splitext (os .path .basename (pkg_path ))[0 ]
171
+ print ( 'Extracting "%s"...' % os .path .splitext (os .path .basename (pkg_path ))[0 ])
168
172
sys .stdout .flush ()
169
173
170
174
if not os .access (pkg_path , os .R_OK ):
171
- print 'Cannot read package.'
175
+ print ( 'Cannot read package.' )
172
176
continue
173
177
174
178
extract_dir = get_extract_dir (pkg_path )
@@ -183,20 +187,20 @@ def main():
183
187
if extract_package (os .path .join (root , file ), subpkg_extract_dir ):
184
188
count += 1
185
189
if count > 0 :
186
- print 'Extracted %d internal package%s to "%s".' % \
187
- (count , ('' , 's' )[count > 1 ], extract_dir )
190
+ print ( 'Extracted %d internal package%s to "%s".' % \
191
+ (count , ('' , 's' )[count > 1 ], extract_dir ))
188
192
else :
189
193
shutil .rmtree (extract_dir )
190
- print 'No packages found within the metapackage.'
194
+ print ( 'No packages found within the metapackage.' )
191
195
elif pkg_path .endswith ('.pkg' ):
192
196
if extract_package (pkg_path , extract_dir ):
193
- print 'Extracted to "%s".' % extract_dir
197
+ print ( 'Extracted to "%s".' % extract_dir )
194
198
else :
195
- print 'Not a package.'
199
+ print ( 'Not a package.' )
196
200
197
- print '----------------------'
201
+ print ( '----------------------' )
198
202
199
- print 'Done!'
203
+ print ( 'Done!' )
200
204
201
205
if __name__ == '__main__' :
202
206
main ()
0 commit comments