-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzip_unitypackage.py
executable file
·72 lines (66 loc) · 2.45 KB
/
unzip_unitypackage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import os
import sys
import re
import shutil
import subprocess
def extract_unitypackage(fn):
print('untar file...')
bn = os.path.splitext(os.path.basename(fn))[0]
dir_untar = '/tmp/{}'.format(bn)
if not os.path.isdir(dir_untar):
os.mkdir(dir_untar)
sp = subprocess.Popen(['tar', 'zxvf', fn, '-C', dir_untar])
result = sp.wait()
print('Finish with result={}'.format(result))
files = os.listdir(dir_untar)
'''
In each uuid folder, there are files 'asset', 'pathname'
'''
dir_output = os.path.join(dir_untar, 'output')
if not os.path.isdir(dir_output):
os.mkdir(dir_output)
for name_subfolder in files:
# print('entry: ', name_subfolder)
if not re.match(r'^[a-f0-9]{32}$', name_subfolder):
print('Path name {} does not match'.format(name_subfolder))
continue
dir_subfolder = os.path.join(dir_untar, name_subfolder)
if not os.path.isdir(dir_subfolder):
print('{} not a folder'.format(dir_subfolder))
continue
fn_asset = os.path.join(dir_subfolder, 'asset')
if not os.path.exists(fn_asset):
print('File {} does not exist'.format(fn_asset))
continue
# print('asset file: ', fn_asset)
fn_pathname = os.path.join(dir_subfolder, 'pathname')
if not os.path.isfile(fn_pathname):
print('File {} does not exist'.format(fn_asset))
continue
with open(fn_pathname) as fp:
pathname = fp.read()
if pathname.endswith('\x0a\x30\x30'):
pathname = pathname[0:-3]
fn_output = os.path.join(dir_output, pathname)
# print('destination: {}'.format(fn_output))
dir_output_tmp = os.path.dirname(fn_output)
if not os.path.isdir(dir_output_tmp):
# print('creating dir: ', dir_output_tmp)
os.makedirs(dir_output_tmp, exist_ok=True)
print('Copying from {} to {}...'.format(fn_asset, fn_output))
shutil.copy(fn_asset, fn_output)
def main():
if len(sys.argv) < 2:
print('Usage {} unitypackage_filename'.format(__file__))
return
fn = sys.argv[1]
if not os.path.isfile(fn):
print('Input file {} does not exist'.format(fn))
return
if os.path.splitext(fn)[-1] != '.unitypackage':
print('Input filename is not .unitypackage')
return
extract_unitypackage(fn)
if __name__ == '__main__':
main()