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