-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcreate_unitypackage.py
65 lines (54 loc) · 1.99 KB
/
create_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
#!/usr/bin/env python3
import sys
import os
import io
import argparse
import os.path
import tarfile
import yaml
import glob
parser = argparse.ArgumentParser(description='Create unitypackage without Unity')
parser.add_argument('-r', '--recursive', action='store_true')
parser.add_argument('targets', nargs='*', help='Target directory or file to pack')
parser.add_argument('-o', '--output', required=True, help='Output unitypackage path')
args = parser.parse_args()
print('Targets:', args.targets)
print('Output unitypackage:', args.output)
print('Is recursive', args.recursive)
for target in args.targets:
if not os.path.exists(target):
print("Target doesn't exist: " + target)
sys.exit(1)
def filter_tarinfo(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = "root"
return tarinfo
def add_file(tar, metapath):
filepath = metapath[0:-5].replace('\\', '/')
print(filepath)
with open(metapath, 'r') as f:
try:
guid = yaml.safe_load(f)['guid']
except yaml.YAMLError as exc:
print(exc)
return
# dir
tarinfo = tarfile.TarInfo(guid)
tarinfo.type = tarfile.DIRTYPE
tar.addfile(tarinfo=tarinfo)
if os.path.isfile(filepath):
tar.add(filepath, arcname=os.path.join(guid, 'asset'), filter=filter_tarinfo)
tar.add(metapath, arcname=os.path.join(guid, 'asset.meta'), filter=filter_tarinfo)
# path: {guid}/pathname
# text: path of asset
tarinfo = tarfile.TarInfo(os.path.join(guid, 'pathname'))
tarinfo.size= len(filepath)
tar.addfile(tarinfo=tarinfo, fileobj=io.BytesIO(filepath.encode('utf8')))
with tarfile.open(args.output, 'w:gz') as tar:
for target in args.targets:
add_file(tar, target + '.meta')
if args.recursive:
for meta in glob.glob(os.path.join(target, '*.meta')):
add_file(tar, meta)
for meta in glob.glob(os.path.join(target, '**/*.meta'), recursive=True):
add_file(tar, meta)