Permission messages: Add a bunch of missing combinations/suppressions.
[chromium-blink-merge.git] / build / android / screenshot.py
blobb03979663633df155d0ef72578031133f81f3487
1 #!/usr/bin/env python
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Takes a screenshot or a screen video capture from an Android device."""
9 import logging
10 import optparse
11 import os
12 import sys
14 from devil.android import device_blacklist
15 from devil.android import device_errors
16 from devil.android import device_utils
17 from pylib import screenshot
19 def _PrintMessage(heading, eol='\n'):
20 sys.stdout.write('%s%s' % (heading, eol))
21 sys.stdout.flush()
24 def _CaptureScreenshot(device, host_file):
25 host_file = device.TakeScreenshot(host_file)
26 _PrintMessage('Screenshot written to %s' % os.path.abspath(host_file))
29 def _CaptureVideo(device, host_file, options):
30 size = tuple(map(int, options.size.split('x'))) if options.size else None
31 recorder = screenshot.VideoRecorder(device,
32 megabits_per_second=options.bitrate,
33 size=size,
34 rotate=options.rotate)
35 try:
36 recorder.Start()
37 _PrintMessage('Recording. Press Enter to stop...', eol='')
38 raw_input()
39 finally:
40 recorder.Stop()
41 host_file = recorder.Pull(host_file)
42 _PrintMessage('Video written to %s' % os.path.abspath(host_file))
45 def main():
46 # Parse options.
47 parser = optparse.OptionParser(description=__doc__,
48 usage='screenshot.py [options] [filename]')
49 parser.add_option('-d', '--device', metavar='ANDROID_DEVICE', help='Serial '
50 'number of Android device to use.', default=None)
51 parser.add_option('--blacklist-file', help='Device blacklist JSON file.')
52 parser.add_option('-f', '--file', help='Save result to file instead of '
53 'generating a timestamped file name.', metavar='FILE')
54 parser.add_option('-v', '--verbose', help='Verbose logging.',
55 action='store_true')
56 video_options = optparse.OptionGroup(parser, 'Video capture')
57 video_options.add_option('--video', help='Enable video capturing. Requires '
58 'Android KitKat or later', action='store_true')
59 video_options.add_option('-b', '--bitrate', help='Bitrate in megabits/s, '
60 'from 0.1 to 100 mbps, %default mbps by default.',
61 default=4, type='float')
62 video_options.add_option('-r', '--rotate', help='Rotate video by 90 degrees.',
63 default=False, action='store_true')
64 video_options.add_option('-s', '--size', metavar='WIDTHxHEIGHT',
65 help='Frame size to use instead of the device '
66 'screen size.', default=None)
67 parser.add_option_group(video_options)
69 (options, args) = parser.parse_args()
71 if len(args) > 1:
72 parser.error('Too many positional arguments.')
73 host_file = args[0] if args else options.file
75 if options.verbose:
76 logging.getLogger().setLevel(logging.DEBUG)
78 if options.blacklist_file:
79 blacklist = device_blacklist.Blacklist(options.blacklist_file)
80 else:
81 blacklist = None
83 devices = device_utils.DeviceUtils.HealthyDevices(blacklist)
84 if options.device:
85 device = next((d for d in devices if d == options.device), None)
86 if not device:
87 raise device_errors.DeviceUnreachableError(options.device)
88 else:
89 if len(devices) > 1:
90 parser.error('Multiple devices are attached. '
91 'Please specify device serial number with --device.')
92 elif len(devices) == 1:
93 device = devices[0]
94 else:
95 raise device_errors.NoDevicesError()
97 if options.video:
98 _CaptureVideo(device, host_file, options)
99 else:
100 _CaptureScreenshot(device, host_file)
101 return 0
104 if __name__ == '__main__':
105 sys.exit(main())