| 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 logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 from lib.cache_decorator import Cached |
| 13 from local_cache import LocalCacher # pylint: disable=W |
| 14 |
| 12 | 15 |
| 13 def SetUpSystemPaths(): # pragma: no cover | 16 def SetUpSystemPaths(): # pragma: no cover |
| 14 """Sets system paths so as to import modules in findit, third_party and | 17 """Sets system paths so as to import modules in findit, third_party and |
| 15 appengine.""" | 18 appengine.""" |
| 16 findit_root_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) | 19 findit_root_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) |
| 17 third_party_dir = os.path.join(findit_root_dir, 'third_party') | 20 third_party_dir = os.path.join(findit_root_dir, 'third_party') |
| 18 appengine_sdk_dir = os.path.join(findit_root_dir, os.path.pardir, | 21 appengine_sdk_dir = os.path.join(findit_root_dir, os.path.pardir, |
| 19 os.path.pardir, os.path.pardir, | 22 os.path.pardir, os.path.pardir, |
| 20 'google_appengine') | 23 'google_appengine') |
| 21 | 24 |
| 22 # Add App Engine SDK dir to sys.path. | 25 # Add App Engine SDK dir to sys.path. |
| 23 sys.path.insert(1, appengine_sdk_dir) | 26 sys.path.insert(1, appengine_sdk_dir) |
| 24 sys.path.insert(1, third_party_dir) | 27 sys.path.insert(1, third_party_dir) |
| 25 import dev_appserver | 28 import dev_appserver |
| 26 dev_appserver.fix_sys_path() | 29 dev_appserver.fix_sys_path() |
| 27 | 30 |
| 28 # Add Findit root dir to sys.path so that modules in Findit is available. | 31 # Add Findit root dir to sys.path so that modules in Findit is available. |
| 29 sys.path.insert(1, findit_root_dir) | 32 sys.path.insert(1, findit_root_dir) |
| 30 | 33 |
| 31 | 34 |
| 32 # TODO(katesonia): Add local cache for this function. | 35 @Cached(namespace='Command-output', cacher=LocalCacher()) |
| 33 def GetCommandOutput(command): | 36 def GetCommandOutput(command): # pragma: no cover |
| 34 """Gets the output stream of executable command. | 37 """Gets the output stream of executable command. |
| 35 | 38 |
| 36 Args: | 39 Args: |
| 37 command (str): Command to execute to get output. | 40 command (str): Command to execute to get output. |
| 38 | 41 |
| 39 Return: | 42 Return: |
| 40 Output steam of the command. | 43 Output steam of the command. |
| 41 """ | 44 """ |
| 42 p = subprocess.Popen( | 45 p = subprocess.Popen( |
| 43 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) | 46 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 44 stdoutdata, stderrdata = p.communicate() | 47 stdoutdata, stderrdata = p.communicate() |
| 45 | 48 |
| 46 if p.returncode != 0: | 49 if p.returncode != 0: |
| 47 logging.error('Error running command %s: %s', command, stderrdata) | 50 logging.error('Error running command %s: %s', command, stderrdata) |
| 48 return None | 51 return None |
| 49 | 52 |
| 50 return stdoutdata | 53 return stdoutdata |
| OLD | NEW |