Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1040)

Side by Side Diff: build/android/devil/android/md5sum.py

Issue 1399273002: [Android] Add a configurable environment for devil/. (RELAND) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: push the forwarder all of the time instead of most of the time Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 os 5 import os
6 import posixpath 6 import posixpath
7 import re 7 import re
8 8
9 from devil import devil_env
9 from devil.android import device_errors 10 from devil.android import device_errors
10 from devil.utils import cmd_helper 11 from devil.utils import cmd_helper
11 from pylib import constants
12 12
13 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum' 13 MD5SUM_DEVICE_LIB_PATH = '/data/local/tmp/md5sum'
14 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin' 14 MD5SUM_DEVICE_BIN_PATH = MD5SUM_DEVICE_LIB_PATH + '/md5sum_bin'
15 15
16 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+') 16 _STARTS_WITH_CHECKSUM_RE = re.compile(r'^\s*[0-9a-fA-F]{32}\s+')
17 17
18 18
19 def CalculateHostMd5Sums(paths): 19 def CalculateHostMd5Sums(paths):
20 """Calculates the MD5 sum value for all items in |paths|. 20 """Calculates the MD5 sum value for all items in |paths|.
21 21
22 Directories are traversed recursively and the MD5 sum of each file found is 22 Directories are traversed recursively and the MD5 sum of each file found is
23 reported in the result. 23 reported in the result.
24 24
25 Args: 25 Args:
26 paths: A list of host paths to md5sum. 26 paths: A list of host paths to md5sum.
27 Returns: 27 Returns:
28 A dict mapping file paths to their respective md5sum checksums. 28 A dict mapping file paths to their respective md5sum checksums.
29 """ 29 """
30 if isinstance(paths, basestring): 30 if isinstance(paths, basestring):
31 paths = [paths] 31 paths = [paths]
32 32
33 md5sum_bin_host_path = os.path.join( 33 md5sum_bin_host_path = devil_env.config.FetchPath('md5sum_host')
34 constants.GetOutDirectory(), 'md5sum_bin_host')
35 if not os.path.exists(md5sum_bin_host_path): 34 if not os.path.exists(md5sum_bin_host_path):
36 raise IOError('File not built: %s' % md5sum_bin_host_path) 35 raise IOError('File not built: %s' % md5sum_bin_host_path)
37 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths]) 36 out = cmd_helper.GetCmdOutput([md5sum_bin_host_path] + [p for p in paths])
38 37
39 return _ParseMd5SumOutput(out.splitlines()) 38 return _ParseMd5SumOutput(out.splitlines())
40 39
41 40
42 def CalculateDeviceMd5Sums(paths, device): 41 def CalculateDeviceMd5Sums(paths, device):
43 """Calculates the MD5 sum value for all items in |paths|. 42 """Calculates the MD5 sum value for all items in |paths|.
44 43
45 Directories are traversed recursively and the MD5 sum of each file found is 44 Directories are traversed recursively and the MD5 sum of each file found is
46 reported in the result. 45 reported in the result.
47 46
48 Args: 47 Args:
49 paths: A list of device paths to md5sum. 48 paths: A list of device paths to md5sum.
50 Returns: 49 Returns:
51 A dict mapping file paths to their respective md5sum checksums. 50 A dict mapping file paths to their respective md5sum checksums.
52 """ 51 """
53 if not paths: 52 if not paths:
54 return {} 53 return {}
55 54
56 if isinstance(paths, basestring): 55 if isinstance(paths, basestring):
57 paths = [paths] 56 paths = [paths]
58 # Allow generators 57 # Allow generators
59 paths = list(paths) 58 paths = list(paths)
60 59
61 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') 60 md5sum_dist_path = devil_env.config.FetchPath('md5sum_device', device=device)
62 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin') 61
62 if os.path.isdir(md5sum_dist_path):
63 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin')
64 else:
65 md5sum_dist_bin_path = md5sum_dist_path
63 66
64 if not os.path.exists(md5sum_dist_path): 67 if not os.path.exists(md5sum_dist_path):
65 raise IOError('File not built: %s' % md5sum_dist_path) 68 raise IOError('File not built: %s' % md5sum_dist_path)
66 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path) 69 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path)
67 70
68 # For better performance, make the script as small as possible to try and 71 # For better performance, make the script as small as possible to try and
69 # avoid needing to write to an intermediary file (which RunShellCommand will 72 # avoid needing to write to an intermediary file (which RunShellCommand will
70 # do if necessary). 73 # do if necessary).
71 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH 74 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH
72 # Check if the binary is missing or has changed (using its file size as an 75 # Check if the binary is missing or has changed (using its file size as an
(...skipping 15 matching lines...) Expand all
88 out = device.RunShellCommand(md5sum_script, check_return=True) 91 out = device.RunShellCommand(md5sum_script, check_return=True)
89 except device_errors.AdbShellCommandFailedError as e: 92 except device_errors.AdbShellCommandFailedError as e:
90 # Push the binary only if it is found to not exist 93 # Push the binary only if it is found to not exist
91 # (faster than checking up-front). 94 # (faster than checking up-front).
92 if e.status == 2: 95 if e.status == 2:
93 # If files were previously pushed as root (adbd running as root), trying 96 # If files were previously pushed as root (adbd running as root), trying
94 # to re-push as non-root causes the push command to report success, but 97 # to re-push as non-root causes the push command to report success, but
95 # actually fail. So, wipe the directory first. 98 # actually fail. So, wipe the directory first.
96 device.RunShellCommand(['rm', '-rf', MD5SUM_DEVICE_LIB_PATH], 99 device.RunShellCommand(['rm', '-rf', MD5SUM_DEVICE_LIB_PATH],
97 as_root=True, check_return=True) 100 as_root=True, check_return=True)
98 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH) 101 if os.path.isdir(md5sum_dist_path):
102 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH)
103 else:
104 mkdir_cmd = 'a=%s;[[ -e $a ]] || mkdir $a' % MD5SUM_DEVICE_LIB_PATH
105 device.RunShellCommand(mkdir_cmd, check_return=True)
106 device.adb.Push(md5sum_dist_bin_path, MD5SUM_DEVICE_BIN_PATH)
107
99 out = device.RunShellCommand(md5sum_script, check_return=True) 108 out = device.RunShellCommand(md5sum_script, check_return=True)
100 else: 109 else:
101 raise 110 raise
102 111
103 return _ParseMd5SumOutput(out) 112 return _ParseMd5SumOutput(out)
104 113
105 114
106 def _ParseMd5SumOutput(out): 115 def _ParseMd5SumOutput(out):
107 hash_and_path = (l.split(None, 1) for l in out 116 hash_and_path = (l.split(None, 1) for l in out
108 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) 117 if l and _STARTS_WITH_CHECKSUM_RE.match(l))
109 return dict((p, h) for h, p in hash_and_path) 118 return dict((p, h) for h, p in hash_and_path)
110 119
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698