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.
11 from pylib
import cmd_helper
12 from pylib
import constants
13 from pylib
.utils
import device_temp_file
15 HashAndPath
= collections
.namedtuple('HashAndPath', ['hash', 'path'])
17 MD5SUM_DEVICE_LIB_PATH
= '/data/local/tmp/md5sum/'
18 MD5SUM_DEVICE_BIN_PATH
= MD5SUM_DEVICE_LIB_PATH
+ 'md5sum_bin'
20 MD5SUM_DEVICE_SCRIPT_FORMAT
= (
21 'test -f {path} -o -d {path} '
22 '&& LD_LIBRARY_PATH={md5sum_lib} {device_pie_wrapper} {md5sum_bin} {path}')
25 def CalculateHostMd5Sums(paths
):
26 """Calculates the MD5 sum value for all items in |paths|.
29 paths: A list of host paths to md5sum.
31 A list of named tuples with 'hash' and 'path' attributes.
33 if isinstance(paths
, basestring
):
36 out
= cmd_helper
.GetCmdOutput(
37 [os
.path
.join(constants
.GetOutDirectory(), 'md5sum_bin_host')] +
39 return [HashAndPath(*l
.split(None, 1)) for l
in out
.splitlines()]
42 def CalculateDeviceMd5Sums(paths
, device
):
43 """Calculates the MD5 sum value for all items in |paths|.
46 paths: A list of device paths to md5sum.
48 A list of named tuples with 'hash' and 'path' attributes.
50 if isinstance(paths
, basestring
):
53 if not device
.FileExists(MD5SUM_DEVICE_BIN_PATH
):
55 os
.path
.join(constants
.GetOutDirectory(), 'md5sum_dist'),
56 MD5SUM_DEVICE_LIB_PATH
)
60 with tempfile
.NamedTemporaryFile() as md5sum_script_file
:
61 with device_temp_file
.DeviceTempFile(
62 device
.adb
) as md5sum_device_script_file
:
63 device_pie_wrapper
= device
.GetDevicePieWrapper()
65 MD5SUM_DEVICE_SCRIPT_FORMAT
.format(
66 path
=p
, md5sum_lib
=MD5SUM_DEVICE_LIB_PATH
,
67 device_pie_wrapper
=device_pie_wrapper
,
68 md5sum_bin
=MD5SUM_DEVICE_BIN_PATH
)
70 md5sum_script_file
.write('; '.join(md5sum_script
))
71 md5sum_script_file
.flush()
72 device
.adb
.Push(md5sum_script_file
.name
, md5sum_device_script_file
.name
)
73 out
= device
.RunShellCommand(['sh', md5sum_device_script_file
.name
])
75 return [HashAndPath(*l
.split(None, 1)) for l
in out
if l
]