1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import posixpath
7import re
8
9from devil import devil_env
10from devil.android import device_errors
11from devil.utils import cmd_helper
12
13MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum'
14MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin'
15
16_STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+')
17
18
19def CalculateHostMd5Sums(paths):
20  """Calculates the MD5 sum value for all items in |paths|.
21
22  Directories are traversed recursively and the MD5 sum of each file found is
23  reported in the result.
24
25  Args:
26    paths: A list of host paths to md5sum.
27  Returns:
28    A dict mapping file paths to their respective md5sum checksums.
29  """
30  if isinstance(paths, basestring):
31    paths = [paths]
32
33  md5sum_bin_host_path = devil_env.config.FetchPath('md5sum_host')
34  if not os.path.exists(md5sum_bin_host_path):
35    raise IOError('File not built: %s' % md5sum_bin_host_path)
36  out = cmd_helper.GetCmdOutput(
37    [md5sum_bin_host_path] + [os.path.realpath(p) for p in paths])
38
39  return _ParseMd5SumOutput(out.splitlines())
40
41
42def CalculateDeviceMd5Sums(paths, device):
43  """Calculates the MD5 sum value for all items in |paths|.
44
45  Directories are traversed recursively and the MD5 sum of each file found is
46  reported in the result.
47
48  Args:
49    paths: A list of device paths to md5sum.
50  Returns:
51    A dict mapping file paths to their respective md5sum checksums.
52  """
53  if not paths:
54    return {}
55
56  if isinstance(paths, basestring):
57    paths = [paths]
58  # Allow generators
59  paths = list(paths)
60
61  md5sum_dist_path = devil_env.config.FetchPath('md5sum_device', device=device)
62
63  if os.path.isdir(md5sum_dist_path):
64    md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin')
65  else:
66    md5sum_dist_bin_path = md5sum_dist_path
67
68  if not os.path.exists(md5sum_dist_path):
69    raise IOError('File not built: %s' % md5sum_dist_path)
70  md5sum_file_size = os.path.getsize(md5sum_dist_bin_path)
71
72  # For better performance, make the script as small as possible to try and
73  # avoid needing to write to an intermediary file (which RunShellCommand will
74  # do if necessary).
75  md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH
76  # Check if the binary is missing or has changed (using its file size as an
77  # indicator), and trigger a (re-)push via the exit code.
78  md5sum_script += '! [[ $(ls -l $a) = *%d* ]]&&exit 2;' % md5sum_file_size
79  # Make sure it can find libbase.so
80  md5sum_script += 'export LD_LIBRARY_PATH=%s;' % MD5SUM_DEVICE_LIB_PATH
81  if len(paths) > 1:
82    prefix = posixpath.commonprefix(paths)
83    if len(prefix) > 4:
84      md5sum_script += 'p="%s";' % prefix
85      paths = ['$p"%s"' % p[len(prefix):] for p in paths]
86
87  md5sum_script += ';'.join('$a %s' % p for p in paths)
88  # Don't fail the script if the last md5sum fails (due to file not found)
89  # Note: ":" is equivalent to "true".
90  md5sum_script += ';:'
91  try:
92    out = device.RunShellCommand(md5sum_script, check_return=True)
93  except device_errors.AdbShellCommandFailedError as e:
94    # Push the binary only if it is found to not exist
95    # (faster than checking up-front).
96    if e.status == 2:
97      # If files were previously pushed as root (adbd running as root), trying
98      # to re-push as non-root causes the push command to report success, but
99      # actually fail. So, wipe the directory first.
100      device.RunShellCommand(['rm', '-rf', MD5SUM_DEVICE_LIB_PATH],
101                             as_root=True, check_return=True)
102      if os.path.isdir(md5sum_dist_path):
103        device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH)
104      else:
105        mkdir_cmd = 'a=%s;[[ -e $a ]] || mkdir $a' % MD5SUM_DEVICE_LIB_PATH
106        device.RunShellCommand(mkdir_cmd, check_return=True)
107        device.adb.Push(md5sum_dist_bin_path, MD5SUM_DEVICE_BIN_PATH)
108
109      out = device.RunShellCommand(md5sum_script, check_return=True)
110    else:
111      raise
112
113  return _ParseMd5SumOutput(out)
114
115
116def _ParseMd5SumOutput(out):
117  hash_and_path = (l.split(None, 1) for l in out
118                   if l and _STARTS_WITH_CHECKSUM_RE.match(l))
119  return dict((p, h) for h, p in hash_and_path)
120
121