1#!/usr/bin/env python
2# Copyright 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Takes a screenshot from an Android device."""
7
8import argparse
9import logging
10import os
11import sys
12
13if __name__ == '__main__':
14  sys.path.append(os.path.abspath(os.path.join(
15      os.path.dirname(__file__), '..', '..', '..')))
16from devil.android import device_utils
17from devil.android.tools import script_common
18
19
20def main():
21  # Parse options.
22  parser = argparse.ArgumentParser(description=__doc__)
23  parser.add_argument('-d', '--device', dest='devices', action='append',
24                      help='Serial number of Android device to use.')
25  parser.add_argument('--blacklist-file', help='Device blacklist JSON file.')
26  parser.add_argument('-f', '--file', metavar='FILE',
27                      help='Save result to file instead of generating a '
28                           'timestamped file name.')
29  parser.add_argument('-v', '--verbose', action='store_true',
30                      help='Verbose logging.')
31  parser.add_argument('host_file', nargs='?',
32                      help='File to which the screenshot will be saved.')
33
34  args = parser.parse_args()
35
36  host_file = args.host_file or args.file
37
38  if args.verbose:
39    logging.getLogger().setLevel(logging.DEBUG)
40
41  devices = script_common.GetDevices(args.devices, args.blacklist_file)
42
43  def screenshot(device):
44    f = None
45    if host_file:
46      root, ext = os.path.splitext(host_file)
47      f = '%s_%s%s' % (root, str(device), ext)
48    f = device.TakeScreenshot(f)
49    print 'Screenshot for device %s written to %s' % (
50        str(device), os.path.abspath(f))
51
52  device_utils.DeviceUtils.parallel(devices).pMap(screenshot)
53  return 0
54
55
56if __name__ == '__main__':
57  sys.exit(main())
58