aboutsummaryrefslogtreecommitdiff
path: root/wa/commands/revent.py
blob: 6eb885a65951388bded2fc615e18a3d9ae0cb32c (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#    Copyright 2014-2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import sys
from time import sleep

from wa import Command
from wa.framework import pluginloader
from wa.framework.resource import ResourceResolver
from wa.framework.target.manager import TargetManager
from wa.utils.revent import ReventRecorder


class RecordCommand(Command):

    name = 'record'
    description = '''
    Performs a revent recording

    This command helps making revent recordings. It will automatically
    deploy revent and has options to automatically open apps and record
    specified stages of a workload.

    Revent allows you to record raw inputs such as screen swipes or button presses.
    This can be useful for recording inputs for workloads such as games that don't
    have XML UI layouts that can be used with UIAutomator. As a drawback from this,
    revent recordings are specific to the device type they were recorded on.

    WA uses two parts to the names of revent recordings in the format,
    {device_name}.{suffix}.revent.

     - device_name can either be specified manually with the ``-d`` argument or
       it can be automatically determined. On Android device it will be obtained
       from ``build.prop``, on Linux devices it is obtained from ``/proc/device-tree/model``.
     - suffix is used by WA to determine which part of the app execution the
       recording is for, currently these are either ``setup``, ``run``, ``extract_results``
       or ``teardown``. All stages except ``run`` are optional and these should
       be specified with the ``-s``, ``-e`` or ``-t`` arguments respectively,
       or optionally ``-a`` to indicate all stages should be recorded.
    '''

    def __init__(self, **kwargs):
        super(RecordCommand, self).__init__(**kwargs)
        self.tm = None
        self.target = None
        self.revent_recorder = None

    def initialize(self, context):
        self.parser.add_argument('-d', '--device', metavar='DEVICE',
                                 help='''
                                 Specify the device on which to run. This will
                                 take precedence over the device (if any)
                                 specified in configuration.
                                 ''')
        self.parser.add_argument('-o', '--output', help='Specify the output file', metavar='FILE')
        self.parser.add_argument('-s', '--setup', help='Record a recording for setup stage',
                                 action='store_true')
        self.parser.add_argument('-r', '--run', help='Record a recording for run stage',
                                 action='store_true')
        self.parser.add_argument('-e', '--extract_results', help='Record a recording for extract_results stage',
                                 action='store_true')
        self.parser.add_argument('-t', '--teardown', help='Record a recording for teardown stage',
                                 action='store_true')
        self.parser.add_argument('-a', '--all', help='Record recordings for available stages',
                                 action='store_true')

        # Need validation
        self.parser.add_argument('-C', '--clear', help='Clear app cache before launching it',
                                 action='store_true')
        group = self.parser.add_mutually_exclusive_group(required=False)
        group.add_argument('-p', '--package', help='Android package to launch before recording')
        group.add_argument('-w', '--workload', help='Name of a revent workload (mostly games)')

    def validate_args(self, args):
        if args.clear and not (args.package or args.workload):
            self.logger.error("Package/Workload must be specified if you want to clear cache")
            sys.exit()
        if args.workload and args.output:
            self.logger.error("Output file cannot be specified with Workload")
            sys.exit()
        if not args.workload and (args.setup or args.extract_results or
                                  args.teardown or args.all):
            self.logger.error("Cannot specify a recording stage without a Workload")
            sys.exit()

    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()

    def record(self, revent_file, name, output_path):
        msg = 'Press Enter when you are ready to record {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.start_record(revent_file)
        msg = 'Press Enter when you have finished recording {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.stop_record()

        if not os.path.isdir(output_path):
            os.makedirs(output_path)

        revent_file_name = self.target.path.basename(revent_file)
        host_path = os.path.join(output_path, revent_file_name)
        if os.path.exists(host_path):
            msg = 'Revent file \'{}\' already exists, overwrite? [y/n]'
            self.logger.info(msg.format(revent_file_name))
            if raw_input('') == 'y':
                os.remove(host_path)
            else:
                msg = 'Did not pull and overwrite \'{}\''
                self.logger.warning(msg.format(revent_file_name))
                return
        msg = 'Pulling \'{}\' from device'
        self.logger.info(msg.format(self.target.path.basename(revent_file)))
        self.target.pull(revent_file, output_path, as_root=self.target.is_rooted)

    def manual_record(self, args):
        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def package_record(self, args):
        if self.target.os != 'android' and self.target.os != 'chromeos':
            raise ConfigError('Target does not appear to be running Android')
        if self.target.os == 'chromeos' and not self.target.supports_android:
            raise ConfigError('Target does not appear to support Android')
        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))
        self.logger.info('Starting {}'.format(args.package))
        cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
        self.target.execute(cmd.format(args.package))

        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def workload_record(self, args):
        context = LightContext(self.tm)
        setup_revent = '{}.setup.revent'.format(self.target.model)
        run_revent = '{}.run.revent'.format(self.target.model)
        extract_results_revent = '{}.extract_results.revent'.format(self.target.model)
        teardown_file_revent = '{}.teardown.revent'.format(self.target.model)
        setup_file = self.target.get_workpath(setup_revent)
        run_file = self.target.get_workpath(run_revent)
        extract_results_file = self.target.get_workpath(extract_results_revent)
        teardown_file = self.target.get_workpath(teardown_file_revent)

        self.logger.info('Deploying {}'.format(args.workload))
        workload = pluginloader.get_workload(args.workload, self.target)
        # Setup apk if android workload
        if hasattr(workload, 'apk'):
            workload.apk.initialize(context)
            workload.apk.setup(context)
            sleep(workload.loading_time)

        output_path = os.path.join(workload.dependencies_directory,
                                   'revent_files')
        if args.setup or args.all:
            self.record(setup_file, 'SETUP', output_path)
        if args.run or args.all:
            self.record(run_file, 'RUN', output_path)
        if args.extract_results or args.all:
            self.record(extract_results_file, 'EXTRACT_RESULTS', output_path)
        if args.teardown or args.all:
            self.record(teardown_file, 'TEARDOWN', output_path)
        self.logger.info('Tearing down {}'.format(args.workload))
        workload.teardown(context)
        self.logger.info('Recording(s) are available at: \'{}\''.format(output_path))

    def _split_revent_location(self, output):
        output_path = None
        file_name = None
        if output:
            output_path, file_name, = os.path.split(output)

        if not file_name:
            file_name = '{}.revent'.format(self.target.model)
        if not output_path:
            output_path = os.getcwdu()

        return output_path, file_name

class ReplayCommand(Command):

    name = 'replay'
    description = '''
    Replay a revent recording

    Revent allows you to record raw inputs such as screen swipes or button presses.
    See ``wa show record`` to see how to make an revent recording.
    '''

    def initialize(self, context):
        self.parser.add_argument('recording', help='The name of the file to replay',
                                 metavar='FILE')
        self.parser.add_argument('-d', '--device', help='The name of the device')
        self.parser.add_argument('-p', '--package', help='Package to launch before recording')
        self.parser.add_argument('-C', '--clear', help='Clear app cache before launching it',
                                 action="store_true")

    # pylint: disable=W0201
    def execute(self, state, args):
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        target_manager = TargetManager(device, device_config, None)
        self.target = target_manager.target
        revent_file = self.target.path.join(self.target.working_directory,
                                            os.path.split(args.recording)[1])

        self.logger.info("Pushing file to target")
        self.target.push(args.recording, self.target.working_directory)

        revent_recorder = ReventRecorder(target_manager.target)
        revent_recorder.deploy()

        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))

        if args.package:
            self.logger.info('Starting {}'.format(args.package))
            cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
            self.target.execute(cmd.format(args.package))

        self.logger.info("Starting replay")
        revent_recorder.replay(revent_file)
        self.logger.info("Finished replay")
        revent_recorder.remove()


# Used to satisfy the workload API
class LightContext(object):
    def __init__(self, tm):
        self.tm = tm
        self.resolver = ResourceResolver()
        self.resolver.load()