def ls(root: typing.Union[str, pathlib.Path] = ".", abspaths=False, recursive=False): """ Return a list of files in directory. Directory listings are sorted alphabetically. If the named directory is a file, return it's path. Examples: >>> ls("foo") ["a", "b", "c"] >>> ls("foo/a") ["foo/a"] >>> ls("foo", abspaths=True) ["/home/test/foo/a", "/home/test/foo/b", "/home/test/foo/c"] >>> ls("foo", recursive=True) ["a", "b", "b/d", "b/d/e", "c"] Arguments: root (str): Path to directory. Can be relative or absolute. abspaths (bool, optional): Return absolute paths if true. recursive (bool, optional): Recursively list subdirectories if true. Returns: list of str: A list of paths. Raises: OSError: If root directory does not exist. """ def _expand_subdirs(file): if isdir(path(root, file)): return [file] + [path(file, x) for x in ls(path(root, file), recursive=True)] else: return [file] if isfile(root): # If argument is a file, return path. return [abspath(root)] if abspaths else [basename(root)] elif abspaths: # Get relative names. relpaths = ls(root, recursive=recursive, abspaths=False) # Prepend the absolute path to each relative name. base = abspath(root) return [path(base, relpath) for relpath in relpaths] elif recursive: # Recursively expand subdirectories. paths = ls(root, abspaths=abspaths, recursive=False) return labtypes.flatten([_expand_subdirs(file) for file in paths]) else: # List directory contents. return list(sorted(os.listdir(root)))
def GetProtos( export_path: pathlib.Path, outcomes: typing.List[str], max_src_len: int) -> typing.List[TrainingProto]: paths = sorted(labtypes.flatten( [list((export_path / outcome).iterdir()) for outcome in outcomes])) protos = [] for path in paths: proto = pbutil.FromFile(path, TrainingProto()) if len(proto.src) > max_src_len: continue protos.append(proto) return protos
def main(argv): """Main entry point.""" if len(argv) > 1: unknown_args = ', '.join(argv[1:]) raise app.UsageError(f'Unknown arguments "{unknown_args}"') logging.info('Initializing datastore.') config = pathlib.Path(FLAGS.datastore) ds = datastore.DataStore.FromFile(config) output_dir = pathlib.Path(FLAGS.output_directory) # Make directories to write the classifications to. We use the same shorthand # classification names as in Table 2 of the paper: # # http://chriscummins.cc/pub/2018-issta.pdf (output_dir / 'bc').mkdir(parents=True, exist_ok=True) (output_dir / 'bto').mkdir(exist_ok=True) (output_dir / 'abf').mkdir(exist_ok=True) (output_dir / 'arc').mkdir(exist_ok=True) (output_dir / 'awo').mkdir(exist_ok=True) (output_dir / 'pass').mkdir(exist_ok=True) result_dirs = [ pathlib.Path(x) for x in FLAGS.input_directories if pathlib.Path(x).is_dir() ] results_paths = labtypes.flatten([ pathlib.Path(x) for x in fs.lsfiles(x, recursive=True, abspaths=True) ] for x in result_dirs) logging.info('Importing %d results into datastore ...', len(results_paths)) with ds.Session(commit=True) as s: for path in progressbar.ProgressBar()(results_paths): # Instantiating a result from file has the side effect of adding the # result object to the datastore's session. result.Result.FromFile(s, path) with ds.Session() as s: testcases = s.query(testcase.Testcase) logging.info('Difftesting the results of %d testcases ...', testcases.count()) for t in progressbar.ProgressBar( max_value=testcases.count())(testcases): DifftestTestcase(s, t, output_dir) df = ReadClassificationsToTable(output_dir) print() print( 'Table of results. For each testbed, this shows the number of results') print('of each class, using the same shortand as in Table 2 of the paper:') print('http://chriscummins.cc/pub/2018-issta.pdf') print() print(df.to_string(index=False)) print() print('Individual classified programs are written to: ' f"'{output_dir}/<class>/<device>/'")
def main(argv): if len(argv) > 1: unknown_args = ', '.join(argv[1:]) raise app.UsageError(f"Unknown arguments {unknown_args}") logging.info('Preparing OpenCL testbed.') config = harness_pb2.CldriveHarness() config.opencl_env.extend([env.OclgrindOpenCLEnvironment().name]) config.opencl_opt.extend([FLAGS.opencl_opt]) harness = cldrive.CldriveHarness(config) assert len(harness.testbeds) >= 1 input_directories = FLAGS.input_directories logging.info('Reading testcases from: %s', ' '.join(input_directories)) output_directory = pathlib.Path(FLAGS.output_directory) logging.info('Writing results to %s', output_directory) output_directory.mkdir(parents=True, exist_ok=True) # Load testcases. testcase_dirs = [ pathlib.Path(x) for x in input_directories if pathlib.Path(x).is_dir()] if not testcase_dirs: raise app.UsageError('No --input_directories found.') testcase_paths = labtypes.flatten( [[pathlib.Path(y) for y in fs.ls(x, abspaths=True)] for x in testcase_dirs]) testcases = [ pbutil.FromFile(path, deepsmith_pb2.Testcase()) for path in testcase_paths] logging.info('Read %d testcases.', len(testcases)) if not len(testcases): raise app.UsageError("No testcases found: '%s'", ' '.join(input_directories)) # Execute testcases. req = harness_pb2.RunTestcasesRequest() req.testbed.CopyFrom(harness.testbeds[0]) req.testcases.extend(testcases) res = harness.RunTestcases(req, None) # Write results to file. for testcase, result in zip(testcases, res.results): result_id = crypto.md5_str(str(testcase)) pbutil.ToFile(result, output_directory / f'{result_id}.pbtxt') logging.info('Executed %d testcases and wrote results to %s', len(res.results), output_directory) execution_times = [ result.profiling_events[0].duration_ms for result in res.results] logging.info('Average time to evaluate testcase: %.2f ms', sum(execution_times) / len(execution_times))
def test_flatten(): assert labtypes.flatten([[1], [2, 3]]) == [1, 2, 3]
else: return [file] if isfile(root): # If argument is a file, return path. return [abspath(root)] if abspaths else [basename(root)] elif abspaths: # Get relative names. relpaths = ls(root, recursive=recursive, abspaths=False) # Prepend the absolute path to each relative name. base = abspath(root) return [path(base, relpath) for relpath in relpaths] elif recursive: # Recursively expand subdirectories. paths = ls(root, abspaths=abspaths, recursive=False) return labtypes.flatten([_expand_subdirs(file) for file in paths]) else: # List directory contents. return list(sorted(os.listdir(root))) def lsdirs(root=".", **kwargs): """ Return only subdirectories from a directory listing. Arguments: root (str): Path to directory. Can be relative or absolute. **kwargs: Any additional arguments to be passed to ls(). Returns: