diff options
author | Leo Neat <leosneat@gmail.com> | 2020-01-31 10:19:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-31 10:19:12 -0800 |
commit | f2588ea5a8687dffe6f86eba837d7348876d5aaf (patch) | |
tree | 851d87e30eac934984d7bf741776b2c568d521f4 | |
parent | 7751ab5a87907ec971beace9d4d139aaab6287cf (diff) | |
download | oss-fuzz-f2588ea5a8687dffe6f86eba837d7348876d5aaf.tar.gz |
Moving execute to utils.py (#3313)
-rw-r--r-- | infra/build_specified_commit.py | 30 | ||||
-rw-r--r-- | infra/repo_manager.py | 34 | ||||
-rw-r--r-- | infra/utils.py | 70 |
3 files changed, 66 insertions, 68 deletions
diff --git a/infra/build_specified_commit.py b/infra/build_specified_commit.py index 85f844ad2..823fd488d 100644 --- a/infra/build_specified_commit.py +++ b/infra/build_specified_commit.py @@ -21,7 +21,6 @@ import os import collections import logging import re -import subprocess import helper import utils @@ -89,35 +88,8 @@ def detect_main_repo(project_name, repo_name=None, commit=None): command_to_run.extend(['--repo_name', repo_name]) else: command_to_run.extend(['--example_commit', commit]) - out, _ = execute(command_to_run) + out, _ = utils.execute(command_to_run) match = re.search(r'\bDetected repo: ([^ ]+) ([^ ]+)', out.rstrip()) if match and match.group(1) and match.group(2): return match.group(1), match.group(2) return None, None - - -def execute(command, location=None, check_result=False): - """ Runs a shell command in the specified directory location. - - 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. - - Returns: - The stdout of the command, the error code. - - Raises: - RuntimeError: running a command resulted in an error. - """ - - 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 diff --git a/infra/repo_manager.py b/infra/repo_manager.py index 6c010a478..bb3fe2371 100644 --- a/infra/repo_manager.py +++ b/infra/repo_manager.py @@ -24,7 +24,7 @@ a python API and manage the current state of the git repo. import os import shutil -import build_specified_commit +import utils class RepoManagerError(Exception): @@ -67,8 +67,8 @@ class RepoManager: if not os.path.exists(self.base_dir): os.makedirs(self.base_dir) self.remove_repo() - out, err = build_specified_commit.execute( - ['git', 'clone', self.repo_url, self.repo_name], location=self.base_dir) + out, err = utils.execute(['git', 'clone', self.repo_url, self.repo_name], + location=self.base_dir) if not self._is_git_repo(): raise RepoManagerError('%s is not a git repo' % self.repo_url) @@ -99,8 +99,8 @@ class RepoManager: if not commit.rstrip(): raise RepoManagerError('An empty string is not a valid commit SHA') - _, err_code = build_specified_commit.execute( - ['git', 'cat-file', '-e', commit], self.repo_dir) + _, err_code = utils.execute(['git', 'cat-file', '-e', commit], + self.repo_dir) return not err_code def get_current_commit(self): @@ -109,9 +109,9 @@ class RepoManager: Returns: The current active commit SHA """ - out, _ = build_specified_commit.execute(['git', 'rev-parse', 'HEAD'], - self.repo_dir, - check_result=True) + out, _ = utils.execute(['git', 'rev-parse', 'HEAD'], + self.repo_dir, + check_result=True) return out.strip('\n') def get_commit_list(self, old_commit, new_commit): @@ -134,7 +134,7 @@ class RepoManager: raise RepoManagerError('The new commit %s does not exist' % new_commit) if old_commit == new_commit: return [old_commit] - out, err_code = build_specified_commit.execute( + out, err_code = utils.execute( ['git', 'rev-list', old_commit + '..' + new_commit], self.repo_dir) commits = out.split('\n') commits = [commit for commit in commits if commit] @@ -161,15 +161,13 @@ class RepoManager: git_path = os.path.join(self.repo_dir, '.git', 'shallow') if os.path.exists(git_path): - build_specified_commit.execute(['git', 'fetch', '--unshallow'], - self.repo_dir, - check_result=True) - build_specified_commit.execute(['git', 'checkout', '-f', commit], - self.repo_dir, - check_result=True) - build_specified_commit.execute(['git', 'clean', '-fxd'], - self.repo_dir, - check_result=True) + utils.execute(['git', 'fetch', '--unshallow'], + self.repo_dir, + check_result=True) + utils.execute(['git', 'checkout', '-f', commit], + self.repo_dir, + check_result=True) + utils.execute(['git', 'clean', '-fxd'], self.repo_dir, check_result=True) if self.get_current_commit() != commit: raise RepoManagerError('Error checking out commit %s' % commit) diff --git a/infra/utils.py b/infra/utils.py index c2820c38b..8f0e4e287 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 |