| Index: build/android/pylib/utils/md5sum.py
|
| diff --git a/build/android/pylib/utils/md5sum.py b/build/android/pylib/utils/md5sum.py
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9ab739873c338563aaf0c97669c16ab356afd136
|
| --- /dev/null
|
| +++ b/build/android/pylib/utils/md5sum.py
|
| @@ -0,0 +1,89 @@
|
| +
|
| +import collections
|
| +import logging
|
| +import os
|
| +import tempfile
|
| +import types
|
| +
|
| +from pylib import cmd_helper
|
| +from pylib import constants
|
| +from pylib.utils import device_temp_file
|
| +
|
| +HashAndPath = collections.namedtuple('HashAndPath', ['hash', 'path'])
|
| +
|
| +MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum/'
|
| +MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + 'md5sum_bin'
|
| +
|
| +# TODO(jbudorick): Consider having something to fall back on if test isn't available.
|
| +MD5SUM_DEVICE_SCRIPT_FORMAT = (
|
| + 'test -f {path} -o -d {path} '
|
| + '&& LD_LIBRARY_PATH={md5sum_lib} {md5sum_bin} {path}')
|
| +
|
| +
|
| +def CalculateHostMd5Sums(paths):
|
| + logging.info('md5sum.CalculateHostMd5Sums(%s)' % str(paths))
|
| +
|
| + if not isinstance(paths, list):
|
| + paths = [paths]
|
| + out = cmd_helper.GetCmdOutput(
|
| + [os.path.join(constants.GetOutDirectory(), 'md5sum_bin_host')] + paths)
|
| + return [HashAndPath(*l.split(None, 1)) for l in out.splitlines()]
|
| +
|
| +
|
| +def CalculateDeviceMd5Sums(paths, device):
|
| + """Calculates the MD5 sum value for all items in |paths|.
|
| + """
|
| + if isinstance(paths, types.GeneratorType):
|
| + paths = list(paths)
|
| + elif not isinstance(paths, list):
|
| + paths = [paths]
|
| +
|
| + if not device.FileExists(MD5SUM_DEVICE_BIN_PATH):
|
| + device.adb.Push(
|
| + os.path.join(constants.GetOutDirectory(), 'md5sum_dist'),
|
| + MD5SUM_DEVICE_LIB_PATH)
|
| +
|
| + return _CalculateDeviceMd5SumsPrecheckExistence(paths, device)
|
| +
|
| +
|
| +def _CalculateDeviceMd5SumsIndividually(paths, device):
|
| + out = []
|
| + with tempfile.NamedTemporaryFile() as md5sum_script_file:
|
| + with device_temp_file.DeviceTempFile(device) as md5sum_device_script_file:
|
| + md5sum_script = (
|
| + MD5SUM_DEVICE_SCRIPT_FORMAT.format(
|
| + path=p, md5sum_lib=MD5SUM_DEVICE_LIB_PATH,
|
| + md5sum_bin=MD5SUM_DEVICE_BIN_PATH)
|
| + for p in paths)
|
| + md5sum_script_file.write('; '.join(md5sum_script))
|
| + md5sum_script_file.flush()
|
| + device.adb.Push(md5sum_script_file.name, md5sum_device_script_file.name)
|
| + out = device.RunShellCommand(['sh', md5sum_device_script_file.name])
|
| +
|
| + return [HashAndPath(*l.split(None, 1)) for l in out]
|
| +
|
| +
|
| +def _CalculateDeviceMd5SumsPrecheckExistence(paths, device):
|
| + e = 'test -f {path} -o -d {path} && echo {path}'
|
| +
|
| + with tempfile.NamedTemporaryFile() as exists_script_file:
|
| + exists_script = (e.format(path=p) for p in paths)
|
| + exists_script_file.write('; '.join(exists_script))
|
| + exists_script_file.flush()
|
| + with device_temp_file.DeviceTempFile(device) as exists_device_script_file:
|
| + device.adb.Push(exists_script_file.name, exists_device_script_file.name)
|
| + paths = device.RunShellCommand(['sh', exists_device_script_file.name])
|
| +
|
| + if not paths:
|
| + return []
|
| +
|
| + out = []
|
| + with tempfile.NamedTemporaryFile() as md5sum_script_file:
|
| + md5sum_script = ('LD_LIBRARY_PATH=%s %s %s' % (MD5SUM_DEVICE_LIB_PATH, MD5SUM_DEVICE_BIN_PATH, ' '.join(paths)))
|
| + md5sum_script_file.write(md5sum_script)
|
| + md5sum_script_file.flush()
|
| + with device_temp_file.DeviceTempFile(device) as md5sum_device_script_file:
|
| + device.adb.Push(md5sum_script_file.name, md5sum_device_script_file.name)
|
| + out = device.RunShellCommand(['sh', md5sum_device_script_file.name])
|
| +
|
| + return [HashAndPath(*l.split(None, 1)) for l in out]
|
|
|