Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: build/android/pylib/utils/md5sum.py

Issue 1314913009: [Android] Move some pylib modules into devil/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/pylib/utils/host_utils.py ('k') | build/android/pylib/utils/md5sum_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import collections 5 # pylint: disable=unused-wildcard-import
6 import logging 6 # pylint: disable=wildcard-import
7 import os
8 import posixpath
9 import re
10 import tempfile
11 import types
12 7
13 from pylib import cmd_helper 8 from devil.android.md5sum import *
14 from pylib import constants
15 from pylib.utils import device_temp_file
16 from pylib.device import device_errors
17
18 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum/'
19 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + 'md5sum_bin'
20
21 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+')
22
23
24 def CalculateHostMd5Sums(paths):
25 """Calculates the MD5 sum value for all items in |paths|.
26
27 Directories are traversed recursively and the MD5 sum of each file found is
28 reported in the result.
29
30 Args:
31 paths: A list of host paths to md5sum.
32 Returns:
33 A dict mapping file paths to their respective md5sum checksums.
34 """
35 if isinstance(paths, basestring):
36 paths = [paths]
37
38 md5sum_bin_host_path = os.path.join(
39 constants.GetOutDirectory(), 'md5sum_bin_host')
40 if not os.path.exists(md5sum_bin_host_path):
41 raise IOError('File not built: %s' % md5sum_bin_host_path)
42 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths])
43
44 return _ParseMd5SumOutput(out.splitlines())
45
46
47 def CalculateDeviceMd5Sums(paths, device):
48 """Calculates the MD5 sum value for all items in |paths|.
49
50 Directories are traversed recursively and the MD5 sum of each file found is
51 reported in the result.
52
53 Args:
54 paths: A list of device paths to md5sum.
55 Returns:
56 A dict mapping file paths to their respective md5sum checksums.
57 """
58 if not paths:
59 return {}
60
61 if isinstance(paths, basestring):
62 paths = [paths]
63 # Allow generators
64 paths = list(paths)
65
66 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist')
67 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin')
68
69 if not os.path.exists(md5sum_dist_path):
70 raise IOError('File not built: %s' % md5sum_dist_path)
71 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path)
72
73 # For better performance, make the script as small as possible to try and
74 # avoid needing to write to an intermediary file (which RunShellCommand will
75 # do if necessary).
76 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH
77 # Check if the binary is missing or has changed (using its file size as an
78 # indicator), and trigger a (re-)push via the exit code.
79 md5sum_script += '! [[ $(ls -l $a) = *%d* ]]&&exit 2;' % md5sum_file_size
80 # Make sure it can find libbase.so
81 md5sum_script += 'export LD_LIBRARY_PATH=%s;' % MD5SUM_DEVICE_LIB_PATH
82 if len(paths) > 1:
83 prefix = posixpath.commonprefix(paths)
84 if len(prefix) > 4:
85 md5sum_script += 'p="%s";' % prefix
86 paths = ['$p"%s"' % p[len(prefix):] for p in paths]
87
88 md5sum_script += ';'.join('$a %s' % p for p in paths)
89 # Don't fail the script if the last md5sum fails (due to file not found)
90 # Note: ":" is equivalent to "true".
91 md5sum_script += ';:'
92 try:
93 out = device.RunShellCommand(md5sum_script, check_return=True)
94 except device_errors.AdbShellCommandFailedError as e:
95 # Push the binary only if it is found to not exist
96 # (faster than checking up-front).
97 if e.status == 2:
98 # If files were previously pushed as root (adbd running as root), trying
99 # to re-push as non-root causes the push command to report success, but
100 # actually fail. So, wipe the directory first.
101 device.RunShellCommand(['rm', '-rf', MD5SUM_DEVICE_LIB_PATH],
102 as_root=True, check_return=True)
103 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH)
104 out = device.RunShellCommand(md5sum_script, check_return=True)
105 else:
106 raise
107
108 return _ParseMd5SumOutput(out)
109
110
111 def _ParseMd5SumOutput(out):
112 hash_and_path = (l.split(None, 1) for l in out
113 if l and _STARTS_WITH_CHECKSUM_RE.match(l))
114 return dict((p, h) for h, p in hash_and_path)
115
OLDNEW
« no previous file with comments | « build/android/pylib/utils/host_utils.py ('k') | build/android/pylib/utils/md5sum_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698