-
Notifications
You must be signed in to change notification settings - Fork 3
/
launchDataCollection.py
115 lines (102 loc) · 4.64 KB
/
launchDataCollection.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
#Copyright (C) 2017 Luca Massarelli
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
# any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
sys.path.insert(0,"util/")
sys.path.insert(0,"core/")
import click
from EmulatorInterface import EmulatorInterface
from Logger import Logger
import os
from Controller import Controller
import re
##This class start the DataCollection.
#It search for folders that contain an apk file inside the workspacePath.
#Each apk is executed n times and data are saved into a file with a user-defined name
#inside the apk folder.
class DataCollection:
numberOfRun = 0;
numberOfEvents = 0;
workspacePath = ""
resultName = ""
playerPath = ""
apk_regex = ""
VMName = ""
logger = "";
emulator = "";
rndSeed = "";
throttle = "";
def __init__(self,workspacePath,resultName,emulator,playerPath,VMName,apk_regex,run,event,rndSeed,throttle):
self.workspacePath = workspacePath;
self.resultName = resultName;
self.playerPath = playerPath;
self.VMName = VMName;
self.apk_regex = apk_regex;
self.numberOfRun = run;
self.numberOfEvents = event;
self.rndSeed = rndSeed;
self.emulator = emulator;
self.throttle = throttle;
self.logger = Logger(3);
self.logger.log("INFO","CREATED NEW EPERIMENT: \n\
\t Workspace Path: {}\n\
\t Result Prefix: {}\n\
\t Emulator: {} \n \
\t Player Path: {}\n\
\t Virtual Machine Name: {} \n\
\t APK Regex: {} \n\
\t Number Of Run per Packet: {} \n\
\t Number of Event per Run: {} \n\\".format(workspacePath,resultName,emulator,playerPath,VMName,apk_regex,run,event));
def runExperiment(self):
self.logger.log("INFO","STARTING EXPERIMENT");
dir = os.listdir(self.workspacePath);
for item in dir:
if(os.path.isdir(self.workspacePath + item)):
print("--------------------------------------")
self.logger.log("INFO","TESTING PACKAGE: " + item);
path = self.workspacePath + item + "/";
apkDir = os.listdir(path)
for apk_item in apkDir:
regex = re.compile(self.apk_regex);
if(os.path.isfile(path + apk_item) and regex.match(apk_item) != None):
self.logger.log("INFO","TESTING PACKAGE: " + item);
for i in range(0,self.numberOfRun):
self.logger.log("INFO","RUN: " + str(i) + " OF: " + str(self.numberOfRun));
fileName = path + str(self.numberOfEvents)+self.resultName+str(i)+".csv";
fileNameErr = path + str(self.numberOfEvents)+self.resultName+str(i)+".csv.err";
if(os.path.exists(fileName) == False and os.path.exists(fileNameErr) == False):
self.logger.log("INFO","RUN: " + str(i) + " OF: " + str(self.numberOfRun));
emulatorInterface = EmulatorInterface(self.playerPath,self.VMName,self.emulator);
emulatorInterface.restoreSnapshot("Snap1");
emulatorInterface.runEmulator();
controller = Controller(self.numberOfEvents,self.throttle);
controller.run(path+apk_item,fileName,self.rndSeed);
emulatorInterface.stopEmulator();
else:
self.logger.log("INFO","FILENAME: " + fileName + " ALREADY EXIST -- SKIPPPING");
self.logger.log("INFO","FINISH EXPERIMENT");
@click.command()
@click.option("--workspacepath",default="",help="Workspace path of apk")
@click.option("--resultname",default="",help="Prefix of data files")
@click.option("--emulator",default="",help="Name of emulator: vbox|geny")
@click.option("--playerpath",default="",help="Path of genymotion player")
@click.option("--vmname",default="",help="Name of virtual machine")
@click.option("--apk_regex",default="\w{64}.apk",help="Regex for apk files (optional)")
@click.option("--run",default=0,help="Number of run for each packet")
@click.option("--event",default=0,help="Number of event for each run")
@click.option("--rndseed",default=False,help="True if you want change the simulated event every run")
@click.option("--throttle",default=1,help="Delay in microsecond between inputs events")
def hello(workspacepath,resultname,emulator,playerpath,vmname,apk_regex,run,event,rndseed,throttle):
experiment = DataCollection(workspacepath,resultname,emulator,playerpath,vmname,apk_regex,run,event,rndseed,throttle);
experiment.runExperiment();
if __name__ == "__main__":
hello();