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

Side by Side Diff: appengine/findit/util_scripts/script_util.py

Issue 2456603003: [Predator] Add local cache for get command output. (Closed)
Patch Set: . Created 4 years, 1 month 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 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
15 _COMMAND_OUPUT_CACHE_DIR = os.path.join(os.path.dirname(__file__),
16 '.command_output_cache')
wrengr 2016/11/11 18:26:26 Wait, weren't we putting temp files in ~/.predator
Sharu Jiang 2016/11/12 01:01:50 Oops.. should have put this into the ~/*
17
12 18
13 def SetUpSystemPaths(): # pragma: no cover 19 def SetUpSystemPaths(): # pragma: no cover
14 """Sets system paths so as to import modules in findit, third_party and 20 """Sets system paths so as to import modules in findit, third_party and
15 appengine.""" 21 appengine."""
16 findit_root_dir = os.path.join(os.path.dirname(__file__), os.path.pardir) 22 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') 23 third_party_dir = os.path.join(findit_root_dir, 'third_party')
18 appengine_sdk_dir = os.path.join(findit_root_dir, os.path.pardir, 24 appengine_sdk_dir = os.path.join(findit_root_dir, os.path.pardir,
19 os.path.pardir, os.path.pardir, 25 os.path.pardir, os.path.pardir,
20 'google_appengine') 26 'google_appengine')
21 27
22 # Add App Engine SDK dir to sys.path. 28 # Add App Engine SDK dir to sys.path.
23 sys.path.insert(1, appengine_sdk_dir) 29 sys.path.insert(1, appengine_sdk_dir)
24 sys.path.insert(1, third_party_dir) 30 sys.path.insert(1, third_party_dir)
25 import dev_appserver 31 import dev_appserver
26 dev_appserver.fix_sys_path() 32 dev_appserver.fix_sys_path()
27 33
28 # Add Findit root dir to sys.path so that modules in Findit is available. 34 # Add Findit root dir to sys.path so that modules in Findit is available.
29 sys.path.insert(1, findit_root_dir) 35 sys.path.insert(1, findit_root_dir)
30 36
31 37
32 # TODO(katesonia): Add local cache for this function. 38 @Cached(namespace='Command-output',
39 cacher=LocalCacher(cache_dir=_COMMAND_OUPUT_CACHE_DIR))
33 def GetCommandOutput(command): 40 def GetCommandOutput(command):
34 """Gets the output stream of executable command. 41 """Gets the output stream of executable command.
35 42
36 Args: 43 Args:
37 command (str): Command to execute to get output. 44 command (str): Command to execute to get output.
38 45
39 Return: 46 Return:
40 Output steam of the command. 47 Output steam of the command.
41 """ 48 """
42 p = subprocess.Popen( 49 p = subprocess.Popen(
43 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 50 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
44 stdoutdata, stderrdata = p.communicate() 51 stdoutdata, stderrdata = p.communicate()
45 52
46 if p.returncode != 0: 53 if p.returncode != 0:
47 logging.error('Error running command %s: %s', command, stderrdata) 54 logging.error('Error running command %s: %s', command, stderrdata)
48 return None 55 return None
49 56
50 return stdoutdata 57 return stdoutdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698