-
Notifications
You must be signed in to change notification settings - Fork 25
/
pbpack_tool.py
executable file
·76 lines (64 loc) · 2.72 KB
/
pbpack_tool.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
73
74
75
76
#!/usr/bin/env python
import argparse, os
import sys
import inspect
if 'PEBBLE_SDK_PATH' not in os.environ:
print 'Please set pebble sdk path environment variable firstly!'
print 'export PEBBLE_SDK_PATH=/path/to/PebbleSDK/'
sys.exit()
sys.path.append(os.path.join(os.environ['PEBBLE_SDK_PATH'],
'Pebble/common/tools'))
import pbpack
from pbpack import ResourcePack
def fix_ResourcePack_bug(system):
spec = inspect.getargspec(ResourcePack.__init__)
if 'is_system' in spec.args:
origin_init = ResourcePack.__init__
def hacked_init(self, is_system=system):
origin_init(self, system)
ResourcePack.__init__ = hacked_init
pbpack.self = ResourcePack()
def makedirs(directory):
try:
os.makedirs(directory)
except:
pass
def cmd_unpack(args):
with open(args.pack_file, 'rb') as pack_file:
pack = ResourcePack.deserialize(pack_file)
makedirs(args.output_directory)
for i in range(len(pack.contents)):
with open(os.path.join(args.output_directory, '%03d' % i), 'wb') as content_file:
content_file.write(pack.contents[i])
def cmd_pack(args):
pack = ResourcePack()
for f in args.pack_file_list:
pack.add_resource(open(f, 'rb').read())
with open(args.pack_file, 'wb') as pack_file:
pack.serialize(pack_file)
def parse_args():
parser = argparse.ArgumentParser(description="Pack and Unpack"
"pbpack file")
parser.add_argument("--system", action="store_true",
help="whether the pbpack file is system or not")
subparsers = parser.add_subparsers(help="commands", dest='which')
unpack_parser = subparsers.add_parser('unpack',
help="unpack the pbpack file")
unpack_parser.add_argument('pack_file', metavar="PACK_FILE",
help="File to unpack")
unpack_parser.add_argument('output_directory', metavar="OUTPUT_DIRECTORY",
help="Directory to write the contents to")
unpack_parser.set_defaults(func=cmd_unpack)
pack_parser = subparsers.add_parser('pack',
help="pack the pbpack file")
pack_parser.add_argument('pack_file', metavar='PACK_FILE',
help="file to write the pbpack to")
pack_parser.add_argument('pack_file_list', metavar='PACK_FILE_LIST',
nargs="*", help="a list of <pack_file_path>s")
pack_parser.set_defaults(func=cmd_pack)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
fix_ResourcePack_bug(args.system)
args.func(args)