OLD | NEW |
(Empty) | |
| 1 |
| 2 import collections |
| 3 import logging |
| 4 import os |
| 5 import tempfile |
| 6 import types |
| 7 |
| 8 from pylib import cmd_helper |
| 9 from pylib import constants |
| 10 from pylib.utils import device_temp_file |
| 11 |
| 12 HashAndPath = collections.namedtuple('HashAndPath', ['hash', 'path']) |
| 13 |
| 14 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum/' |
| 15 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + 'md5sum_bin' |
| 16 |
| 17 # TODO(jbudorick): Consider having something to fall back on if test isn't avail
able. |
| 18 MD5SUM_DEVICE_SCRIPT_FORMAT = ( |
| 19 'test -f {path} -o -d {path} ' |
| 20 '&& LD_LIBRARY_PATH={md5sum_lib} {md5sum_bin} {path}') |
| 21 |
| 22 |
| 23 def CalculateHostMd5Sums(paths): |
| 24 logging.info('md5sum.CalculateHostMd5Sums(%s)' % str(paths)) |
| 25 |
| 26 if not isinstance(paths, list): |
| 27 paths = [paths] |
| 28 out = cmd_helper.GetCmdOutput( |
| 29 [os.path.join(constants.GetOutDirectory(), 'md5sum_bin_host')] + paths) |
| 30 return [HashAndPath(*l.split(None, 1)) for l in out.splitlines()] |
| 31 |
| 32 |
| 33 def CalculateDeviceMd5Sums(paths, device): |
| 34 """Calculates the MD5 sum value for all items in |paths|. |
| 35 """ |
| 36 if isinstance(paths, types.GeneratorType): |
| 37 paths = list(paths) |
| 38 elif not isinstance(paths, list): |
| 39 paths = [paths] |
| 40 |
| 41 if not device.FileExists(MD5SUM_DEVICE_BIN_PATH): |
| 42 device.adb.Push( |
| 43 os.path.join(constants.GetOutDirectory(), 'md5sum_dist'), |
| 44 MD5SUM_DEVICE_LIB_PATH) |
| 45 |
| 46 return _CalculateDeviceMd5SumsPrecheckExistence(paths, device) |
| 47 |
| 48 |
| 49 def _CalculateDeviceMd5SumsIndividually(paths, device): |
| 50 out = [] |
| 51 with tempfile.NamedTemporaryFile() as md5sum_script_file: |
| 52 with device_temp_file.DeviceTempFile(device) as md5sum_device_script_file: |
| 53 md5sum_script = ( |
| 54 MD5SUM_DEVICE_SCRIPT_FORMAT.format( |
| 55 path=p, md5sum_lib=MD5SUM_DEVICE_LIB_PATH, |
| 56 md5sum_bin=MD5SUM_DEVICE_BIN_PATH) |
| 57 for p in paths) |
| 58 md5sum_script_file.write('; '.join(md5sum_script)) |
| 59 md5sum_script_file.flush() |
| 60 device.adb.Push(md5sum_script_file.name, md5sum_device_script_file.name) |
| 61 out = device.RunShellCommand(['sh', md5sum_device_script_file.name]) |
| 62 |
| 63 return [HashAndPath(*l.split(None, 1)) for l in out] |
| 64 |
| 65 |
| 66 def _CalculateDeviceMd5SumsPrecheckExistence(paths, device): |
| 67 e = 'test -f {path} -o -d {path} && echo {path}' |
| 68 |
| 69 with tempfile.NamedTemporaryFile() as exists_script_file: |
| 70 exists_script = (e.format(path=p) for p in paths) |
| 71 exists_script_file.write('; '.join(exists_script)) |
| 72 exists_script_file.flush() |
| 73 with device_temp_file.DeviceTempFile(device) as exists_device_script_file: |
| 74 device.adb.Push(exists_script_file.name, exists_device_script_file.name) |
| 75 paths = device.RunShellCommand(['sh', exists_device_script_file.name]) |
| 76 |
| 77 if not paths: |
| 78 return [] |
| 79 |
| 80 out = [] |
| 81 with tempfile.NamedTemporaryFile() as md5sum_script_file: |
| 82 md5sum_script = ('LD_LIBRARY_PATH=%s %s %s' % (MD5SUM_DEVICE_LIB_PATH, MD5SU
M_DEVICE_BIN_PATH, ' '.join(paths))) |
| 83 md5sum_script_file.write(md5sum_script) |
| 84 md5sum_script_file.flush() |
| 85 with device_temp_file.DeviceTempFile(device) as md5sum_device_script_file: |
| 86 device.adb.Push(md5sum_script_file.name, md5sum_device_script_file.name) |
| 87 out = device.RunShellCommand(['sh', md5sum_device_script_file.name]) |
| 88 |
| 89 return [HashAndPath(*l.split(None, 1)) for l in out] |
OLD | NEW |