OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 os | 5 import os |
6 import posixpath | 6 import posixpath |
7 import re | 7 import re |
8 | 8 |
| 9 from devil import devil_env |
9 from devil.android import device_errors | 10 from devil.android import device_errors |
10 from devil.utils import cmd_helper | 11 from devil.utils import cmd_helper |
11 from pylib import constants | |
12 | 12 |
13 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum' | 13 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum' |
14 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin' | 14 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin' |
15 | 15 |
16 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+') | 16 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+') |
17 | 17 |
18 | 18 |
19 def CalculateHostMd5Sums(paths): | 19 def CalculateHostMd5Sums(paths): |
20 """Calculates the MD5 sum value for all items in |paths|. | 20 """Calculates the MD5 sum value for all items in |paths|. |
21 | 21 |
22 Directories are traversed recursively and the MD5 sum of each file found is | 22 Directories are traversed recursively and the MD5 sum of each file found is |
23 reported in the result. | 23 reported in the result. |
24 | 24 |
25 Args: | 25 Args: |
26 paths: A list of host paths to md5sum. | 26 paths: A list of host paths to md5sum. |
27 Returns: | 27 Returns: |
28 A dict mapping file paths to their respective md5sum checksums. | 28 A dict mapping file paths to their respective md5sum checksums. |
29 """ | 29 """ |
30 if isinstance(paths, basestring): | 30 if isinstance(paths, basestring): |
31 paths = [paths] | 31 paths = [paths] |
32 | 32 |
33 md5sum_bin_host_path = os.path.join( | 33 md5sum_bin_host_path = devil_env.config.md5sum_host_path |
34 constants.GetOutDirectory(), 'md5sum_bin_host') | |
35 if not os.path.exists(md5sum_bin_host_path): | 34 if not os.path.exists(md5sum_bin_host_path): |
36 raise IOError('File not built: %s' % md5sum_bin_host_path) | 35 raise IOError('File not built: %s' % md5sum_bin_host_path) |
37 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths]) | 36 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths]) |
38 | 37 |
39 return _ParseMd5SumOutput(out.splitlines()) | 38 return _ParseMd5SumOutput(out.splitlines()) |
40 | 39 |
41 | 40 |
42 def CalculateDeviceMd5Sums(paths, device): | 41 def CalculateDeviceMd5Sums(paths, device): |
43 """Calculates the MD5 sum value for all items in |paths|. | 42 """Calculates the MD5 sum value for all items in |paths|. |
44 | 43 |
45 Directories are traversed recursively and the MD5 sum of each file found is | 44 Directories are traversed recursively and the MD5 sum of each file found is |
46 reported in the result. | 45 reported in the result. |
47 | 46 |
48 Args: | 47 Args: |
49 paths: A list of device paths to md5sum. | 48 paths: A list of device paths to md5sum. |
50 Returns: | 49 Returns: |
51 A dict mapping file paths to their respective md5sum checksums. | 50 A dict mapping file paths to their respective md5sum checksums. |
52 """ | 51 """ |
53 if not paths: | 52 if not paths: |
54 return {} | 53 return {} |
55 | 54 |
56 if isinstance(paths, basestring): | 55 if isinstance(paths, basestring): |
57 paths = [paths] | 56 paths = [paths] |
58 # Allow generators | 57 # Allow generators |
59 paths = list(paths) | 58 paths = list(paths) |
60 | 59 |
61 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') | 60 md5sum_dist_path = devil_env.config.md5sum_device_path |
62 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin') | 61 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin') |
63 | 62 |
64 if not os.path.exists(md5sum_dist_path): | 63 if not os.path.exists(md5sum_dist_path): |
65 raise IOError('File not built: %s' % md5sum_dist_path) | 64 raise IOError('File not built: %s' % md5sum_dist_path) |
66 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path) | 65 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path) |
67 | 66 |
68 # For better performance, make the script as small as possible to try and | 67 # For better performance, make the script as small as possible to try and |
69 # avoid needing to write to an intermediary file (which RunShellCommand will | 68 # avoid needing to write to an intermediary file (which RunShellCommand will |
70 # do if necessary). | 69 # do if necessary). |
71 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH | 70 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH |
(...skipping 29 matching lines...) Expand all Loading... |
101 raise | 100 raise |
102 | 101 |
103 return _ParseMd5SumOutput(out) | 102 return _ParseMd5SumOutput(out) |
104 | 103 |
105 | 104 |
106 def _ParseMd5SumOutput(out): | 105 def _ParseMd5SumOutput(out): |
107 hash_and_path = (l.split(None, 1) for l in out | 106 hash_and_path = (l.split(None, 1) for l in out |
108 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) | 107 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) |
109 return dict((p, h) for h, p in hash_and_path) | 108 return dict((p, h) for h, p in hash_and_path) |
110 | 109 |
OLD | NEW |