def test_DataPath_different_working_dir(): """Test that DataPath is not affected by current working dir.""" p = bazelutil.DataPath('phd/labm8/data/test/hello_world') with fs.chdir('/tmp'): assert bazelutil.DataPath('phd/labm8/data/test/hello_world') == p with tempfile.TemporaryDirectory() as d: with fs.chdir(d): assert bazelutil.DataPath('phd/labm8/data/test/hello_world') == p
def test_IsBazelSandbox_different_working_directories(): """Test IsBazelSandboxed() returns the same result from different dirs.""" sandbox = bazelutil.IsBazelSandbox() # We can't test the expected value of this since we don't know it. assert sandbox or True with tempfile.TemporaryDirectory() as d: with fs.chdir(d): assert bazelutil.IsBazelSandbox() == sandbox with fs.chdir('/tmp'): assert bazelutil.IsBazelSandbox() == sandbox
def GetAllFilesRelativePaths( root_dir: pathlib.Path, follow_symlinks: bool = False) -> typing.List[str]: """Get relative paths to all files in the root directory. Follows symlinks. Args: root_dir: The directory to find files in. follow_symlinks: If true, follow symlinks. Returns: A list of paths relative to the root directory. Raises: EmptyCorpusException: If the content files directory is empty. """ with fs.chdir(root_dir): cmd = ['find'] if follow_symlinks: cmd.append('-L') cmd += ['.', '-type', 'f'] try: find_output = subprocess.check_output(cmd).decode('utf-8').strip() except UnicodeDecodeError: # Unicode error could happen with special characters in paths. return [] if find_output: # Strip the leading './' from paths. return [x[2:] for x in find_output.split('\n')] else: return []
def test_chdir_cwd(): """Test that chdir() correctly changes the working directory.""" with tempfile.TemporaryDirectory() as d: with fs.chdir(d): # Bazel sandboxing only changes to directories within the sandbox, so # there may be an unwanted prefix like /private that we can ignore. assert os.getcwd().endswith(d)
def test_chdir_file_argument(): """Test that NotADirectoryError is raised if requested path is a file.""" with tempfile.NamedTemporaryFile(prefix='labm8_') as f: with pytest.raises(NotADirectoryError) as e_info: with fs.chdir(f.name): pass # Bazel sandboxing only changes to directories within the sandbox, so there # may be an unwanted prefix like /private that we can ignore. assert f"Not a directory: '" in str(e_info) assert f.name in str(e_info)
def DotCfgsFromBytecode(bytecode: str) -> typing.Iterator[str]: """Create a control flow graph from an LLVM bytecode file. Args: bytecode: The LLVM bytecode to create CFG dots from. Returns: An iterator of dotfile strings. Raises: OptException: In case the opt pass fails. UnicodeDecodeError: If generated dotfile can't be read. """ with tempfile.TemporaryDirectory(prefix='phd_') as d: output_dir = pathlib.Path(d) # Change into the output directory, because the -dot-cfg pass writes files # to the current working directory. with fs.chdir(output_dir): # We run with universal_newlines=False because the stdout of opt is the # binary bitcode, which we completely ignore (we're only interested in # stderr). This means we must encode stdin and decode stderr ourselves. process = opt.Exec(['-dot-cfg'], stdin=bytecode.encode('utf-8'), universal_newlines=False) stderr = process.stderr.decode('utf-8') # Propagate failures from opt as OptExceptions. if process.returncode: raise opt.OptException( f"opt failed with return code {process.returncode}:\n{stderr}" ) for file in output_dir.iterdir(): # Opt pass prints the name of the dot files it generates, e.g.: # # $ opt -dot-cfg < foo.ll # WARNING: You're attempting to print out a bitcode file. # This is inadvisable as it may cause display problems. If # you REALLY want to taste LLVM bitcode first-hand, you # can force output with the `-f' option. # # Writing 'cfg.DoSomething.dot'... # Writing 'cfg.main.dot'... if f"Writing '{file.name}'..." not in stderr: raise OSError( f"Could not find reference to file '{file.name}' in " f"opt stderr:\n{process.stderr}") with open(file) as f: yield f.read()
def BazelQuery(self, args: typing.List[str], timeout_seconds: int = 360, **subprocess_kwargs): """Run bazel query with the specified args in the workspace. Args: args: The list of arguments to pass to bazel query. timeout_seconds: The number of seconds before failing. subprocess_kwargs: Additional arguments to pass to Popen(). """ with fs.chdir(self.workspace_root): return subprocess.Popen( ['timeout', '-s9', str(timeout_seconds), 'bazel', 'query'] + args, **subprocess_kwargs)
def Bazel(self, command: str, args: typing.List[str], timeout_seconds: int = 360, **subprocess_kwargs): cmd = [ 'timeout', '-s9', str(timeout_seconds), 'bazel', command, '--noshow_progress', ] + args app.Log(2, '$ %s', ' '.join(cmd)) with fs.chdir(self.workspace_root): return subprocess.Popen(cmd, **subprocess_kwargs)
def MoveFilesToDestination(self, workspace: bazelutil.Workspace, file_move_mapping: typing.Dict[str, str]) -> None: with fs.chdir(workspace.workspace_root): for src_relpath, dst_relpath in file_move_mapping.items(): print(dst_relpath) src_path = self.workspace_root / src_relpath dst_path = workspace.workspace_root / dst_relpath if not src_path.is_file(): raise OSError(f"File `{src_relpath}` not found") dst_path.parent.mkdir(exist_ok=True, parents=True) # We can't simply `git mv` because in incremental exports, this move # may have already been applied. Instead, we manually copy the file # from the source workspace, and delete the corresponding file in the # destination workspace. shutil.copy(src_path, dst_path) dst_src_path = workspace.workspace_root / src_relpath if dst_src_path.is_file(): subprocess.check_call(['git', 'rm', src_relpath])
def GetImportRelpaths(self, contentfile_root: pathlib.Path) -> typing.List[str]: """Get relative paths to all files in the content files directory. Args: contentfile_root: The root of the content files directory. Returns: A list of paths relative to the content files root. Raises: EmptyCorpusException: If the content files directory is empty. """ with fs.chdir(contentfile_root): find_output = subprocess.check_output(['find', '.', '-type', 'f' ]).decode('utf-8').strip() if not find_output: raise errors.EmptyCorpusException( f"Empty content files directory: '{contentfile_root}'") return find_output.split('\n')
def test_chdir_not_a_directory(): """Test that FileNotFoundError is raised if requested path does not exist.""" with pytest.raises(FileNotFoundError) as e_info: with fs.chdir('/not/a/real/path'): pass assert "No such file or directory: '/not/a/real/path'" in str(e_info)
def test_chdir_yield_value(): """Test that chdir() yields the requested directory as a pathlib.Path.""" with tempfile.TemporaryDirectory() as d: with fs.chdir(d) as d2: assert pathlib.Path(d) == d2
] + args app.Log(2, "$ %s", " ".join(cmd)) ======= return self.Bazel('query', args, timeout_seconds=timeout_seconds, **subprocess_kwargs) def Bazel(self, command: str, args: typing.List[str], timeout_seconds: int = 360, **subprocess_kwargs): <<<<<<< HEAD:labm8/py/bazelutil.py >>>>>>> 6d5f13a15... Resolve dependencies for each target in turn.:labm8/bazelutil.py with fs.chdir(self.workspace_root): <<<<<<< HEAD:labm8/py/bazelutil.py return subprocess.Popen(cmd, **subprocess_kwargs) def MaybeTargetToPath( self, fully_qualified_target: str, ======= return subprocess.Popen([ 'timeout', '-s9', str(timeout_seconds), 'bazel', command, '--noshow_progress', ] + args, **subprocess_kwargs) =======