-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWillirPy2_7Utils.py
203 lines (161 loc) · 7.17 KB
/
WillirPy2_7Utils.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
#!/usr/bin/env python2.7
import os, zipfile, argparse, abc;
from WillirPyUtils import hasApkSign;
class ArgsReadableDir(argparse.Action):
'''
Checks whether argparse parameter is readable directory.
For action parameter of argparse.ArgumentParser.add_argument method.
'''
def __call__(self, parser, namespace, values, option_string=None):
prospective_dir=values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError("ArgsReadableDir:{0} is not a valid path"
.format(prospective_dir));
if os.access(prospective_dir, os.R_OK):
setattr(namespace, self.dest, prospective_dir)
else:
raise argparse.ArgumentTypeError("ArgsReadableDir:{0} is not a readable dir"
.format(prospective_dir));
class ArgsAndroidSystemDir(ArgsReadableDir):
'''
Checks whether argparse parameter is android system root readable directory.
For 'action' parameter of 'argparse.ArgumentParser.add_argument' method.
'''
def __call__(self, parser, namespace, values, option_string=None):
super(ArgsAndroidSystemDir, self).__call__(parser, namespace, values, option_string);
dirPath = values;
dirEntries = os.listdir(dirPath);
needSystemEntries = set(['app', 'framework', 'bin', 'build.prop', 'lib']);
if needSystemEntries - set(dirEntries):
raise argparse.ArgumentError(None, "'" + dirPath + "' is not a root of android system.\n" +\
"It shall contains " + str(list(needSystemEntries)));
class ArgsAndroidSourceDir(ArgsReadableDir):
'''
Checks whether argparse parameter is android source root readable directory.
For 'action' parameter of 'argparse.ArgumentParser.add_argument' method.
'''
def __call__(self, parser, namespace, values, option_string=None):
super(ArgsAndroidSourceDir, self).__call__(parser, namespace, values, option_string);
dirPath = values;
dirEntries = os.listdir(dirPath);
needSystemEntries = set(['bionic', 'build', 'device', 'external', 'frameworks', '.repo']);
if needSystemEntries - set(dirEntries):
raise argparse.ArgumentError(None, "'" + dirPath + "' is not a root of android source.\n" +\
"It shall contains " + str(list(needSystemEntries)));
class __ArgParseAbstractRecurseFileList(argparse.Action):
__metaclass__ = abc.ABCMeta;
def isRecursive(self):
if hasattr(self.__args, 'recursive'):
return self.__args.recursive;
else:
return False;
@abc.abstractmethod
def handleFile(self, filePath):
'''
Handle file or dir,
1. If it is file than verify it (raise excpetion if something wrong)
2. If is is dir, than recursivly parse all file in dir
@filePath path to file or dir.
@throw argparse.ArgumentError if filePath is file, and it doesn't match expectations.
@returns list of appropriate files.
'''
pass;
def __call__(self, parser, args, values, option_string=None):
res = [];
self.__args = args;
if isinstance(values, basestring):
values = [values];
for fPath in values:
if os.path.isdir(fPath):
if self.isRecursive():
res.extend(self.handleFile(fPath));
else:
raise argparse.ArgumentError(self, "File " + fPath + " is dir;");
else:
res.extend(self.handleFile(fPath));
setattr(args, self.dest, res);
def assertJar(jar, onlyWithSign, argInst=None):
if not os.path.exists(jar):
raise argparse.ArgumentError(argInst, "File " + jar + " is absent;");
if not os.path.isfile(jar):
raise argparse.ArgumentError(argInst, "File " + jar + " is not regular file;");
if not zipfile.is_zipfile(jar):
raise argparse.ArgumentError(argInst, "File " + jar + " is not zip file;");
if onlyWithSign and not hasApkSign(jar):
raise argparse.ArgumentError(argInst, "File " + jar + " don't contain cert.");
def checkJar(jar, onlyWithSign):
try:
assertJar(jar, onlyWithSign, argInst=None);
return True;
except argparse.ArgumentError:
return False;
def assertOdex(odex, argInst=None):
if not os.path.exists(odex):
raise argparse.ArgumentError(argInst, "File " + odex + " is absent;");
if not os.path.isfile(odex):
raise argparse.ArgumentError(argInst, "File " + odex + " is not regular file;");
class __ArgParseApkList(__ArgParseAbstractRecurseFileList):
mWithSign = False;
def handleFile(self, filePath):
if not os.path.isdir(filePath):
assertJar(filePath, self.mWithSign, argInst=self);
return [filePath];
res = [];
for root, _, files in os.walk(filePath):
apkPaths = [os.path.join(root, f) for f in files if f[-4:] in ['.apk']];
apkPaths = [f for f in apkPaths if checkJar(f, self.mWithSign)];
res.extend(apkPaths);
return res;
def getOdexApkPath(path, arg):
'''
Returns tuple(dexPath, apkPath)
@param path: Path to apk file, or odex file, or path with name without extension
@param arg: Instance of argparse.Action or None (used for exception).
@throw argparse.ArgumentError if path can't be interpreted as odexed apk.
'''
if path[-4:] in ['.apk', '.jar']:
path = path[:-4];
elif path[-5:] == '.odex':
path = path[:-5];
dexPath = path + '.odex';
if os.path.exists(path + '.jar'):
apkPath = path + '.jar';
else:
apkPath = path + '.apk';
assertOdex(dexPath, argInst=None);
assertJar(apkPath, onlyWithSign=False, argInst=None);
return (dexPath, apkPath);
class ArgParseOdexList(__ArgParseAbstractRecurseFileList):
def handleFile(self, filePath):
if not os.path.isdir(filePath):
return [getOdexApkPath(filePath, self)];
res = [];
for root, _, files in os.walk(filePath):
dexPaths = [os.path.join(root, f) for f in files if f[-5:] == '.odex'];
for dexPath in dexPaths:
res.append(getOdexApkPath(dexPath, self));
return res;
def getArgParseApkList(withSign = False):
class Res(__ArgParseApkList):
mWithSign = withSign;
return Res;
def getAndAssertApkList(apkList, recursive=False, withSign = False):
ParserClass = getArgParseApkList(withSign);
parserObj = ParserClass(None, 'apkList');
args = argparse.Namespace();
setattr(args, 'recursive', recursive);
parserObj(None, args, apkList);
return args.apkList;
def getAndAssertOdexList(odexList, recursive=False):
parserObj = ArgParseOdexList(None, 'odexList');
args = argparse.Namespace();
setattr(args, 'recursive', recursive);
parserObj(None, args, odexList);
return args.odexList;
def argarsePathFileRoType(filePath):
try:
with open(filePath):
pass;
except IOError:
raise argparse.ArgumentError(None, "can't open '" + filePath + "' for reading.");
return filePath;