| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 """This module contains util functions that local scripts can use.""" | 5 """This module contains util functions that local scripts can use.""" |
| 6 | 6 |
| 7 import atexit | 7 import atexit |
| 8 import functools | 8 import functools |
| 9 import os | 9 import os |
| 10 import Queue | 10 import Queue |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 # Push task to task queue for execution. | 118 # Push task to task queue for execution. |
| 119 for task in tasks: | 119 for task in tasks: |
| 120 TASK_QUEUE.put((task['function'], task.get('args', []), | 120 TASK_QUEUE.put((task['function'], task.get('args', []), |
| 121 task.get('kwargs', {}), result_semaphore)) | 121 task.get('kwargs', {}), result_semaphore)) |
| 122 | 122 |
| 123 # Wait until all tasks to be executed. | 123 # Wait until all tasks to be executed. |
| 124 for _ in tasks: | 124 for _ in tasks: |
| 125 result_semaphore.acquire() | 125 result_semaphore.acquire() |
| 126 | 126 |
| 127 | 127 |
| 128 @Cached(namespace='Command-output', cache=LocalCache()) | 128 @Cached(LocalCache(), namespace='Command-output') |
| 129 def GetCommandOutput(command): # pragma: no cover | 129 def GetCommandOutput(command): # pragma: no cover |
| 130 """Gets the output stream of executable command. | 130 """Gets the output stream of executable command. |
| 131 | 131 |
| 132 Args: | 132 Args: |
| 133 command (str): Command to execute to get output. | 133 command (str): Command to execute to get output. |
| 134 | 134 |
| 135 Return: | 135 Return: |
| 136 Output steam of the command. | 136 Output steam of the command. |
| 137 """ | 137 """ |
| 138 p = subprocess.Popen( | 138 p = subprocess.Popen( |
| 139 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 139 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 140 stdoutdata, stderrdata = p.communicate() | 140 stdoutdata, stderrdata = p.communicate() |
| 141 | 141 |
| 142 if p.returncode != 0: | 142 if p.returncode != 0: |
| 143 raise Exception('Error running command %s: %s' % (command, stderrdata)) | 143 raise Exception('Error running command %s: %s' % (command, stderrdata)) |
| 144 | 144 |
| 145 return stdoutdata | 145 return stdoutdata |
| 146 | 146 |
| 147 | 147 |
| 148 def GetLockedMethod(cls, method_name, lock): # pragma: no cover | 148 def GetLockedMethod(cls, method_name, lock): # pragma: no cover |
| 149 """Returns a class/object method serialized with lock.""" | 149 """Returns a class/object method serialized with lock.""" |
| 150 method = getattr(cls, method_name) | 150 method = getattr(cls, method_name) |
| 151 | 151 |
| 152 def LockedMethod(cls, *args, **kwargs): # pylint: disable=W | 152 def LockedMethod(cls, *args, **kwargs): # pylint: disable=W |
| 153 with lock: | 153 with lock: |
| 154 return method(*args, **kwargs) | 154 return method(*args, **kwargs) |
| 155 | 155 |
| 156 return functools.partial(LockedMethod, cls) | 156 return functools.partial(LockedMethod, cls) |
| OLD | NEW |