diff options
author | 2020-01-31 10:19:12 -0800 | |
---|---|---|
committer | 2020-01-31 10:19:12 -0800 | |
commit | f2588ea5a8687dffe6f86eba837d7348876d5aaf (patch) | |
tree | 851d87e30eac934984d7bf741776b2c568d521f4 /infra/utils.py | |
parent | 7751ab5a87907ec971beace9d4d139aaab6287cf (diff) |
Moving execute to utils.py (#3313)
Diffstat (limited to 'infra/utils.py')
-rw-r--r-- | infra/utils.py | 70 |
1 files changed, 49 insertions, 21 deletions
diff --git a/infra/utils.py b/infra/utils.py index c2820c38..8f0e4e28 100644 --- a/infra/utils.py +++ b/infra/utils.py @@ -16,6 +16,7 @@ import os import re import stat +import subprocess import helper @@ -31,31 +32,31 @@ def chdir_to_root(): os.chdir(helper.OSSFUZZ_DIR) -def is_fuzz_target_local(file_path): - """Returns whether |file_path| is a fuzz target binary (local path). - Copied from clusterfuzz src/python/bot/fuzzers/utils.py - with slight modifications. - """ - filename, file_extension = os.path.splitext(os.path.basename(file_path)) - if not VALID_TARGET_NAME.match(filename): - # Check fuzz target has a valid name (without any special chars). - return False - - if file_extension not in ALLOWED_FUZZ_TARGET_EXTENSIONS: - # Ignore files with disallowed extensions (to prevent opening e.g. .zips). - return False +def execute(command, location=None, check_result=False): + """ Runs a shell command in the specified directory location. - if not os.path.exists(file_path) or not os.access(file_path, os.X_OK): - return False + Args: + command: The command as a list to be run. + location: The directory the command is run in. + check_result: Should an exception be thrown on failed command. - if filename.endswith('_fuzzer'): - return True + Returns: + The stdout of the command, the error code. - if os.path.exists(file_path) and not stat.S_ISREG(os.stat(file_path).st_mode): - return False + Raises: + RuntimeError: running a command resulted in an error. + """ - with open(file_path, 'rb') as file_handle: - return file_handle.read().find(FUZZ_TARGET_SEARCH_STRING.encode()) != -1 + if not location: + location = os.getcwd() + process = subprocess.Popen(command, stdout=subprocess.PIPE, cwd=location) + out, err = process.communicate() + if check_result and (process.returncode or err): + raise RuntimeError('Error: %s\n Command: %s\n Return code: %s\n Out: %s' % + (err, command, process.returncode, out)) + if out is not None: + out = out.decode('ascii').rstrip() + return out, process.returncode def get_fuzz_targets(path): @@ -92,3 +93,30 @@ def get_container_name(): return None with open('/etc/hostname') as file_handle: return file_handle.read().strip() + + +def is_fuzz_target_local(file_path): + """Returns whether |file_path| is a fuzz target binary (local path). + Copied from clusterfuzz src/python/bot/fuzzers/utils.py + with slight modifications. + """ + filename, file_extension = os.path.splitext(os.path.basename(file_path)) + if not VALID_TARGET_NAME.match(filename): + # Check fuzz target has a valid name (without any special chars). + return False + + if file_extension not in ALLOWED_FUZZ_TARGET_EXTENSIONS: + # Ignore files with disallowed extensions (to prevent opening e.g. .zips). + return False + + if not os.path.exists(file_path) or not os.access(file_path, os.X_OK): + return False + + if filename.endswith('_fuzzer'): + return True + + if os.path.exists(file_path) and not stat.S_ISREG(os.stat(file_path).st_mode): + return False + + with open(file_path, 'rb') as file_handle: + return file_handle.read().find(FUZZ_TARGET_SEARCH_STRING.encode()) != -1 |