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 collections | 5 import collections |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import re | 8 import re |
9 import tempfile | 9 import tempfile |
10 import types | 10 import types |
11 | 11 |
12 from pylib import cmd_helper | 12 from pylib import cmd_helper |
13 from pylib import constants | 13 from pylib import constants |
14 from pylib.utils import device_temp_file | 14 from pylib.utils import device_temp_file |
15 | 15 |
16 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum/' | 16 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum/' |
17 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + 'md5sum_bin' | 17 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + 'md5sum_bin' |
18 | 18 |
19 MD5SUM_DEVICE_SCRIPT_FORMAT = ( | 19 MD5SUM_DEVICE_SCRIPT_FORMAT = ( |
20 'test -f {path} -o -d {path} ' | 20 'test -f {path} -o -d {path} ' |
21 '&& LD_LIBRARY_PATH={md5sum_lib} {md5sum_bin} {path}') | 21 '&& LD_LIBRARY_PATH={md5sum_lib} {md5sum_bin} {path}') |
22 | 22 |
23 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-f]{32}\s+') | 23 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+') |
24 | 24 |
25 | 25 |
26 def CalculateHostMd5Sums(paths): | 26 def CalculateHostMd5Sums(paths): |
27 """Calculates the MD5 sum value for all items in |paths|. | 27 """Calculates the MD5 sum value for all items in |paths|. |
28 | 28 |
| 29 Directories are traversed recursively and the MD5 sum of each file found is |
| 30 reported in the result. |
| 31 |
29 Args: | 32 Args: |
30 paths: A list of host paths to md5sum. | 33 paths: A list of host paths to md5sum. |
31 Returns: | 34 Returns: |
32 A dict mapping paths to their respective md5sum checksums. | 35 A dict mapping file paths to their respective md5sum checksums. |
33 """ | 36 """ |
34 if isinstance(paths, basestring): | 37 if isinstance(paths, basestring): |
35 paths = [paths] | 38 paths = [paths] |
36 | 39 |
37 md5sum_bin_host_path = os.path.join( | 40 md5sum_bin_host_path = os.path.join( |
38 constants.GetOutDirectory(), 'md5sum_bin_host') | 41 constants.GetOutDirectory(), 'md5sum_bin_host') |
39 if not os.path.exists(md5sum_bin_host_path): | 42 if not os.path.exists(md5sum_bin_host_path): |
40 raise IOError('File not built: %s' % md5sum_bin_host_path) | 43 raise IOError('File not built: %s' % md5sum_bin_host_path) |
41 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths]) | 44 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths]) |
42 | 45 |
43 return _ParseMd5SumOutput(out.splitlines()) | 46 return _ParseMd5SumOutput(out.splitlines()) |
44 | 47 |
45 | 48 |
46 def CalculateDeviceMd5Sums(paths, device): | 49 def CalculateDeviceMd5Sums(paths, device): |
47 """Calculates the MD5 sum value for all items in |paths|. | 50 """Calculates the MD5 sum value for all items in |paths|. |
48 | 51 |
| 52 Directories are traversed recursively and the MD5 sum of each file found is |
| 53 reported in the result. |
| 54 |
49 Args: | 55 Args: |
50 paths: A list of device paths to md5sum. | 56 paths: A list of device paths to md5sum. |
51 Returns: | 57 Returns: |
52 A dict mapping paths to their respective md5sum checksums. | 58 A dict mapping file paths to their respective md5sum checksums. |
53 """ | 59 """ |
54 if isinstance(paths, basestring): | 60 if isinstance(paths, basestring): |
55 paths = [paths] | 61 paths = [paths] |
56 | 62 |
57 if not device.FileExists(MD5SUM_DEVICE_BIN_PATH): | 63 if not device.FileExists(MD5SUM_DEVICE_BIN_PATH): |
58 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') | 64 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') |
59 if not os.path.exists(md5sum_dist_path): | 65 if not os.path.exists(md5sum_dist_path): |
60 raise IOError('File not built: %s' % md5sum_dist_path) | 66 raise IOError('File not built: %s' % md5sum_dist_path) |
61 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH) | 67 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH) |
62 | 68 |
(...skipping 13 matching lines...) Expand all Loading... |
76 out = device.RunShellCommand(['sh', md5sum_device_script_file.name]) | 82 out = device.RunShellCommand(['sh', md5sum_device_script_file.name]) |
77 | 83 |
78 return _ParseMd5SumOutput(out) | 84 return _ParseMd5SumOutput(out) |
79 | 85 |
80 | 86 |
81 def _ParseMd5SumOutput(out): | 87 def _ParseMd5SumOutput(out): |
82 hash_and_path = (l.split(None, 1) for l in out | 88 hash_and_path = (l.split(None, 1) for l in out |
83 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) | 89 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) |
84 return dict((p, h) for h, p in hash_and_path) | 90 return dict((p, h) for h, p in hash_and_path) |
85 | 91 |
OLD | NEW |