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