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.
12 from pylib
import cmd_helper
13 from pylib
import constants
14 from pylib
.utils
import device_temp_file
16 MD5SUM_DEVICE_LIB_PATH
= '/data/local/tmp/md5sum/'
17 MD5SUM_DEVICE_BIN_PATH
= MD5SUM_DEVICE_LIB_PATH
+ 'md5sum_bin'
19 MD5SUM_DEVICE_SCRIPT_FORMAT
= (
20 'test -f {path} -o -d {path} '
21 '&& LD_LIBRARY_PATH={md5sum_lib} {md5sum_bin} {path}')
23 _STARTS_WITH_CHECKSUM_RE
= re
.compile(r
'^\s*[0-9a-fA-f]{32}\s+')
26 def CalculateHostMd5Sums(paths
):
27 """Calculates the MD5 sum value for all items in |paths|.
30 paths: A list of host paths to md5sum.
32 A dict mapping paths to their respective md5sum checksums.
34 if isinstance(paths
, basestring
):
37 md5sum_bin_host_path
= os
.path
.join(
38 constants
.GetOutDirectory(), 'md5sum_bin_host')
39 if not os
.path
.exists(md5sum_bin_host_path
):
40 raise IOError('File not built: %s' % md5sum_bin_host_path
)
41 out
= cmd_helper
.GetCmdOutput([md5sum_bin_host_path
] + [p
for p
in paths
])
43 return _ParseMd5SumOutput(out
.splitlines())
46 def CalculateDeviceMd5Sums(paths
, device
):
47 """Calculates the MD5 sum value for all items in |paths|.
50 paths: A list of device paths to md5sum.
52 A dict mapping paths to their respective md5sum checksums.
54 if isinstance(paths
, basestring
):
57 if not device
.FileExists(MD5SUM_DEVICE_BIN_PATH
):
58 md5sum_dist_path
= os
.path
.join(constants
.GetOutDirectory(), 'md5sum_dist')
59 if not os
.path
.exists(md5sum_dist_path
):
60 raise IOError('File not built: %s' % md5sum_dist_path
)
61 device
.adb
.Push(md5sum_dist_path
, MD5SUM_DEVICE_LIB_PATH
)
65 with tempfile
.NamedTemporaryFile() as md5sum_script_file
:
66 with device_temp_file
.DeviceTempFile(
67 device
.adb
) as md5sum_device_script_file
:
69 MD5SUM_DEVICE_SCRIPT_FORMAT
.format(
70 path
=p
, md5sum_lib
=MD5SUM_DEVICE_LIB_PATH
,
71 md5sum_bin
=MD5SUM_DEVICE_BIN_PATH
)
73 md5sum_script_file
.write('; '.join(md5sum_script
))
74 md5sum_script_file
.flush()
75 device
.adb
.Push(md5sum_script_file
.name
, md5sum_device_script_file
.name
)
76 out
= device
.RunShellCommand(['sh', md5sum_device_script_file
.name
])
78 return _ParseMd5SumOutput(out
)
81 def _ParseMd5SumOutput(out
):
82 hash_and_path
= (l
.split(None, 1) for l
in out
83 if l
and _STARTS_WITH_CHECKSUM_RE
.match(l
))
84 return dict((p
, h
) for h
, p
in hash_and_path
)