forked from mcxiaoke/packer-ng-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngpacker.py
executable file
·218 lines (203 loc) · 6.71 KB
/
ngpacker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: mcxiaoke
# @Date: 2015-11-26 16:52:55
from __future__ import print_function
import os
import sys
import struct
import shutil
import argparse
import time
from apkinfo import APK
__version__ = '1.0.2.20151204'
ZIP_SHORT = 2
MARKET_PATH = 'markets.txt'
OUTPUT_PATH = 'archives'
MAGIC = '!ZXK!'
def write_market(path, market, output):
'''
write market info to apk file
write_market(apk-file-path, market-name, output-path)
'''
path = os.path.abspath(path)
output = unicode(output)
if not os.path.exists(path):
print('apk file',path,'not exists')
return
if read_market(path):
print('apk file',path,'had market already')
return
if not output:
output = os.path.dirname(path)
if not os.path.exists(output):
os.makedirs(output)
name,ext = os.path.splitext(os.path.basename(path))
# name,package,vname,vcode
app = parse_apk(path)
apk_name = '%s-%s-%s-%s%s' % (app['app_package'],
market.decode('utf8'), app['version_name'], app['version_code'], ext)
# apk_name = name + "-" + market + ext
apk_file = os.path.join(output,apk_name)
shutil.copy(path,apk_file)
# print('apk file:',apk_file)
index = os.stat(apk_file).st_size
index -= ZIP_SHORT
with open(apk_file,"r+b") as f:
f.seek(index)
# write comment length
f.write(struct.pack('<H',len(market) + ZIP_SHORT + len(MAGIC)))
# write comment content
# content = [market_string + market_length + magic_string]
f.write(market)
f.write(struct.pack('<H',len(market)))
f.write(MAGIC)
return apk_file
def read_market(path):
'''
read market info from apk file
read_market(apk-file-path)
'''
index = os.stat(path).st_size
# print('path:',path,'length:',index)
index -= len(MAGIC)
f = open(path,'rb')
f.seek(index)
# read and check magic
magic = f.read(len(MAGIC))
# print('magic',magic)
if magic == MAGIC:
index -= ZIP_SHORT
f.seek(index)
# read market string length
market_length = struct.unpack('<H',f.read(ZIP_SHORT))[0]
# print('comment length:',market_length)
index -= market_length
f.seek(index)
# read market
market = f.read(market_length)
# print('found market:',market)
return market
else:
# print('magic not matched')
return None
def verify_market(path,market):
'''
verify apk market info
verify_market(apk-file-path,market-name)
'''
return read_market(path) == market
def show_market(path):
'''
show market info for apk file
show_market(apk-file-path)
'''
app = parse_apk(path)
market = read_market(path)
if market:
market = market.decode('utf8')
print(u'Name: %s\nMarket: %s\nPackage:%s\nVersionName: %s\nVersionCode: %s' % (
app['app_name'], market, app['app_package'],
app['version_name'], app['version_code']))
def parse_markets(path):
'''
parse file lines to market name list
parse_markets(market-file-path)
'''
with open(path) as f:
return filter(None,map(lambda x: x.split('#')[0].strip(), f.readlines()))
def parse_apk(path):
'''
parse apk file, get name, package, version
'''
apk = APK(path)
if not apk.is_valid_APK():
return None
return {
'app_file': path.decode('utf8'),
'app_name': apk.get_app_name(),
'app_package': apk.get_package(),
'version_name': apk.get_version_name(),
'version_code': apk.get_version_code()
}
def process(path, market = MARKET_PATH,output = OUTPUT_PATH):
'''
process apk file to create market apk archives
process(apk-file-path, market = MARKET_PATH, output = OUTPUT_PATH)
'''
print('start to write market info to apk files ...')
markets = parse_markets(market)
counter = 0
for market in markets:
apk_file = write_market(path, market, output)
verified = verify_market(apk_file, market)
if not verified:
print('apk',apk_file,'for market',market,'verify failed')
# break
else:
print('processed apk',apk_file)
counter += 1
print('all',counter,'apks saved to',os.path.abspath(output))
def run_test(path,times):
'''
run market packer performance test
'''
print('start to run market packaging testing...')
t0 = time.time()
for i in xrange(1,times):
write_market(path,'%s Test Market' % i, 'temp')
print('run',times,'using',(time.time() - t0), 'seconds')
pass
def _check(path, market = MARKET_PATH,output = OUTPUT_PATH, show=False, test = 0):
'''
check apk file exists, check apk valid, check arguments, check market file exists
'''
if not os.path.exists(path):
print('apk file',path,'not exists or not readable')
return
if not parse_apk(path):
print('apk file',path,'is not valid apk')
return
if show:
show_market(path)
return
if test > 0:
run_test(path,test)
return
if not os.path.exists(market):
print('market file',market,'not exists or not readable')
return
old_market = read_market(path)
if old_market:
print('apk file',path,'already had market:',old_market,
'please using original release apk file')
return
process(path,market,output)
def _parse_args():
'''
parse command line arguments
'''
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='PackerNg v{0} created by mcxiaoke. \nNext Generation Android Market Packaging Tool'.format(__version__),
epilog='''Project Home: https://github.com/mcxiaoke/packer-ng-plugin
''')
parser.add_argument('path', nargs='?',
help='original release apk file path (required)')
parser.add_argument('market', nargs='?',default = MARKET_PATH,
help='markets file path [default: ./markets.txt]')
parser.add_argument('output', nargs='?',default = OUTPUT_PATH,
help='archives output path [default: ./archives]')
parser.add_argument('-s', '--show', action='store_const', const=True,
help='show apk file info (pkg/market/version)')
parser.add_argument('-t', '--test', default = 0, type = int,
help='perform serval times packer-ng test')
args = parser.parse_args()
if len(sys.argv) == 1:
print('use ./%s -h/--help to show help messages' % sys.argv[0])
return None
return args
if __name__ == '__main__':
args = _parse_args()
if args:
_check(**vars(args))