| OLD | NEW |
| 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 collections | 5 import collections |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import posixpath | 8 import posixpath |
| 9 import re | 9 import re |
| 10 import tempfile | 10 import tempfile |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 Args: | 53 Args: |
| 54 paths: A list of device paths to md5sum. | 54 paths: A list of device paths to md5sum. |
| 55 Returns: | 55 Returns: |
| 56 A dict mapping file paths to their respective md5sum checksums. | 56 A dict mapping file paths to their respective md5sum checksums. |
| 57 """ | 57 """ |
| 58 if isinstance(paths, basestring): | 58 if isinstance(paths, basestring): |
| 59 paths = [paths] | 59 paths = [paths] |
| 60 # Allow generators | 60 # Allow generators |
| 61 paths = list(paths) | 61 paths = list(paths) |
| 62 | 62 |
| 63 def push_md5sum(): | 63 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') |
| 64 md5sum_dist_path = os.path.join(constants.GetOutDirectory(), 'md5sum_dist') | 64 md5sum_dist_bin_path = os.path.join(md5sum_dist_path, 'md5sum_bin') |
| 65 if not os.path.exists(md5sum_dist_path): | 65 |
| 66 raise IOError('File not built: %s' % md5sum_dist_path) | 66 if not os.path.exists(md5sum_dist_path): |
| 67 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH) | 67 raise IOError('File not built: %s' % md5sum_dist_path) |
| 68 md5sum_file_size = os.path.getsize(md5sum_dist_bin_path) |
| 68 | 69 |
| 69 # For better performance, make the script as small as possible to try and | 70 # For better performance, make the script as small as possible to try and |
| 70 # avoid needing to write to an intermediary file (which RunShellCommand will | 71 # avoid needing to write to an intermediary file (which RunShellCommand will |
| 71 # do if necessary). | 72 # do if necessary). |
| 72 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH | 73 md5sum_script = 'a=%s;' % MD5SUM_DEVICE_BIN_PATH |
| 73 md5sum_script += 'test -e $a||exit 2;' | 74 # Check if the binary is missing or has changed (using its file size as an |
| 75 # indicator), and trigger a (re-)push via the exit code. |
| 76 md5sum_script += '! [[ $(ls -l $a) = *%d* ]]&&exit 2;' % md5sum_file_size |
| 77 # Make sure it can find libbase.so |
| 74 md5sum_script += 'export LD_LIBRARY_PATH=%s;' % MD5SUM_DEVICE_LIB_PATH | 78 md5sum_script += 'export LD_LIBRARY_PATH=%s;' % MD5SUM_DEVICE_LIB_PATH |
| 75 if len(paths) > 1: | 79 if len(paths) > 1: |
| 76 prefix = posixpath.commonprefix(paths) | 80 prefix = posixpath.commonprefix(paths) |
| 77 if len(prefix) > 4: | 81 if len(prefix) > 4: |
| 78 md5sum_script += 'p="%s";' % prefix | 82 md5sum_script += 'p="%s";' % prefix |
| 79 paths = ['$p"%s"' % p[len(prefix):] for p in paths] | 83 paths = ['$p"%s"' % p[len(prefix):] for p in paths] |
| 80 | 84 |
| 81 md5sum_script += ';'.join('$a %s' % p for p in paths) | 85 md5sum_script += ';'.join('$a %s' % p for p in paths) |
| 82 # Don't fail the script if the last md5sum fails (due to file not found) | 86 # Don't fail the script if the last md5sum fails (due to file not found) |
| 83 # Note: ":" is equivalent to "true". | 87 # Note: ":" is equivalent to "true". |
| 84 md5sum_script += ';:' | 88 md5sum_script += ';:' |
| 85 try: | 89 try: |
| 86 out = device.RunShellCommand(md5sum_script, check_return=True) | 90 out = device.RunShellCommand(md5sum_script, check_return=True) |
| 87 except device_errors.AdbShellCommandFailedError as e: | 91 except device_errors.AdbShellCommandFailedError as e: |
| 88 # Push the binary only if it is found to not exist | 92 # Push the binary only if it is found to not exist |
| 89 # (faster than checking up-front). | 93 # (faster than checking up-front). |
| 90 if e.status == 2: | 94 if e.status == 2: |
| 91 push_md5sum() | 95 device.adb.Push(md5sum_dist_path, MD5SUM_DEVICE_LIB_PATH) |
| 92 out = device.RunShellCommand(md5sum_script, check_return=True) | 96 out = device.RunShellCommand(md5sum_script, check_return=True) |
| 93 else: | 97 else: |
| 94 raise | 98 raise |
| 95 | 99 |
| 96 return _ParseMd5SumOutput(out) | 100 return _ParseMd5SumOutput(out) |
| 97 | 101 |
| 98 | 102 |
| 99 def _ParseMd5SumOutput(out): | 103 def _ParseMd5SumOutput(out): |
| 100 hash_and_path = (l.split(None, 1) for l in out | 104 hash_and_path = (l.split(None, 1) for l in out |
| 101 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) | 105 if l and _STARTS_WITH_CHECKSUM_RE.match(l)) |
| 102 return dict((p, h) for h, p in hash_and_path) | 106 return dict((p, h) for h, p in hash_and_path) |
| 103 | 107 |
| OLD | NEW |