def __error(self, error, cmds): """Set pid to -1 and status to 127 before closing files.""" self.close_files() logger.error(error) def not_found(path): """Raise OSError. :param path: path of the executable :type path: str """ logger.error("%s not found", path) e3.log.debug('PATH=%s', os.environ['PATH']) raise OSError(errno.ENOENT, 'No such file or directory, %s not found' % path) # Try to send an helpful message if one of the executable has not # been found. if isinstance(cmds[0], basestring): if which(cmds[0], default=None) is None: not_found(cmds[0]) else: for cmd in cmds: if which(cmd[0], default=None) is None: not_found(cmd[0])
def add_interpreter_command(cmd_line: CmdLine) -> CmdLine: """Add the interpreter defined in the #! line to cmd_line. If the #! line cannot be parsed, just return the cmd_line unchanged On windows, /usr/bin/env will be ignored to avoid a dependency on cygwin and /bin/bash & /bin/sh are replaced by $SHELL if defined. :param cmd_line: command line """ if not parse_shebang: # nothing to do return cmd_line prog = which(cmd_line[0], default=None) if prog is None: # Not found. Do not modify the command line return cmd_line with open(prog) as f: try: header = f.read()[0:2] except UnicodeDecodeError: # unknown header - cannot decode the first two bytes return cmd_line if header != "#!": # Unknown header return cmd_line # Header found, get the interpreter command in the first line f.seek(0) line = f.readline() interpreter_cmds = [ word.strip() for word in line[line.find("!") + 1:].split() ] # Pass the program path to the interpreter if len(cmd_line) > 1: cmd_line = [prog] + list(cmd_line[1:]) else: cmd_line = [prog] if sys.platform == "win32": # unix: no cover if interpreter_cmds[0] == "/usr/bin/env": # On windows be sure that PATH is taken into account by # using which. In some cases involving python # interpreter, the python interpreter used to run this # module has been used rather than the first one on the # path. interpreter_cmds[1] = which( interpreter_cmds[1], default=interpreter_cmds[1]) return interpreter_cmds[1:] + cmd_line elif (interpreter_cmds[0] in ("/bin/bash", "/bin/sh") and "SHELL" in os.environ): return [os.environ["SHELL"]] + cmd_line return interpreter_cmds + cmd_line
def version(tool, nlines=1): """ Return version information as reported by the execution of TOOL --version, expected on the first NLINES of output. If TOOL is not available from PATH, return a version text indicating unavailability. If TOOL is 'gcc', append the target for which it was configured to the base version info. """ # If TOOL is not on PATH, return a version text indicating unavailability. # This situation is legitimate here for gnatemu when running through a # probe, and if we happen to actually need the tool later on, we'll see # test failures anyway. if not which(tool): return 'N/A' # --version often dumps more than the version number on a line. A # copyright notice is typically found there as well. Our heuristic # here is to strip everything past the first comma. def version_on_line(text): cprpos = text.find(',') return text[0:cprpos] if cprpos != -1 else text tool_version_output = Run([tool, '--version']).out.split('\n') version_info = '\n'.join( [version_on_line(line) for line in tool_version_output[0:nlines]]) if tool == 'gcc': gcc_target = Run([tool, '-dumpmachine']).out.strip() version_info += ' [%s]' % gcc_target return version_info
def test_rsync_mode(): """Check that rsync mode is faster than default mode.""" mkdir("work") mkdir("work2") GitRepository.create("git") for _ in range(1000): name = str(uuid.uuid1(clock_seq=int(1000 * time.time()))) touch(os.path.join("git", name + ".py")) touch(os.path.join("git", name + ".pyc")) touch(os.path.join("git", name + ".o")) touch(os.path.join("git", name + ".ali")) with open("git/.gitignore", "w") as fd: fd.write("*.pyc\n") fd.write("*.o\n") fd.write("*.ali\n") m = CheckoutManager(name="myrepo", working_dir="work") start = time.time() m.update(vcs="external", url=os.path.abspath("git")) total_sync_tree = time.time() - start os.environ["E3_ENABLE_FEATURE"] = "use-rsync" m = CheckoutManager(name="myrepo", working_dir="work2") start = time.time() m.update(vcs="external", url=os.path.abspath("git")) total_rsync = time.time() - start if which("rsync"): assert total_rsync * 4 < total_sync_tree
def add_interpreter_command(cmd_line): """Add the interpreter defined in the #! line to cmd_line. If the #! line cannot be parsed, just return the cmd_line unchanged If the interpreter command line contains /usr/bin/env python it will be replaced by the value of python_executable On windows, /usr/bin/env will be ignored to avoid a dependency on cygwin :param cmd_line: command line :type cmd_line: list[str] """ if not parse_shebang: # nothing to do return cmd_line prog = which(cmd_line[0], default=None) if prog is None: # Not found. Do not modify the command line return cmd_line with open(prog) as f: try: header = f.read()[0:2] except UnicodeDecodeError: # py3-only # unknown header - cannot decode the first two bytes return cmd_line if header != "#!": # Unknown header return cmd_line # Header found, get the interpreter command in the first line f.seek(0) line = f.readline() interpreter_cmds = [l.strip() for l in line[line.find('!') + 1:].split()] # Pass the program path to the interpreter if len(cmd_line) > 1: cmd_line = [prog] + list(cmd_line[1:]) else: cmd_line = [prog] # If the interpreter is '/usr/bin/env python', use # python_executable instead to keep the same python executable if interpreter_cmds[0:2] == ['/usr/bin/env', 'python']: if len(interpreter_cmds) > 2: return [python_executable] + \ interpreter_cmds[2:] + cmd_line else: return [python_executable] + cmd_line elif sys.platform == 'win32': # unix: no cover if interpreter_cmds[0] == '/usr/bin/env': return interpreter_cmds[1:] + cmd_line elif interpreter_cmds[0] in ('/bin/bash', '/bin/sh') and \ 'SHELL' in os.environ: return [os.environ['SHELL']] + cmd_line return interpreter_cmds + cmd_line
def __init__(self, command_line: List[str], save_output: bool = False, save_input: bool = False): """Constructor. :param command_line: list of strings representing the command line to be spawned. :type command_line: list[str] :param save_output: Save all output generated during the session for later retrieval (see method get_session_logs). :type save_output: bool :param save_input: Save all input generated during the session for later retrieval (see method get_session_logs). :type save_input: bool """ self.save_output = save_output self.save_input = save_input # Convert the command line to a list of string is needed command_line = [str(arg) for arg in command_line] if len(command_line) < 1: raise ExpectError("__init__", "expect a non empty list as argument") command_line[0] = which(command_line[0]) # Store the command line used logger.debug("spawn %s" % " ".join(command_line)) self.command_line = command_line # Spawn the process. self._child: Optional[PexpectProcess] = spawn(command_line[0], command_line[1:], timeout=None) self.pid = self._child.pid # Initialize our buffer self.buffer = "" # If we have to save input or output keep another buffer that # is never flushed. self.saved_buffer = "" # Keep the state of the process self.process_is_dead = False # This is where we store that last successful expect result self.last_match: Optional[Tuple[int, str, Match[str]]] = None # This is where the command returned status will be stored # when the command has exited. For the moment, it is not # available. self.status: Optional[int] = None
def require_vcs(prog, request): """Require svn or git to run the test. When in "CI" mode, a missing svn or git generates an error. In other modes the test is just skipped. :param prog: either "svn" or "git" """ if not which(prog): if request.config.getoption('ci'): pytest.fail('{} not available'.format(prog)) else: pytest.skip('{} not available'.format(prog))
def add_interpreter_command(cmd_line): """Add the interpreter defined in the #! line to cmd_line. If the #! line cannot be parsed, just return the cmd_line unchanged If the interpreter command line contains /usr/bin/env python it will be replaced by the value of python_executable On windows, /usr/bin/env will be ignored to avoid a dependency on cygwin :param cmd_line: command line :type cmd_line: list[str] """ if not parse_shebang: # nothing to do return cmd_line prog = which(cmd_line[0], default=None) if prog is None: # Not found. Do not modify the command line return cmd_line with open(prog) as f: header = f.read()[0:2] if header != "#!": # Unknown header return cmd_line # Header found, get the interpreter command in the first line f.seek(0) line = f.readline() interpreter_cmds = [l.strip() for l in line[line.find('!') + 1:].split()] # Pass the program path to the interpreter if len(cmd_line) > 1: cmd_line = [prog] + list(cmd_line[1:]) else: cmd_line = [prog] # If the interpreter is '/usr/bin/env python', use # python_executable instead to keep the same python executable if interpreter_cmds[0:2] == ['/usr/bin/env', 'python']: if len(interpreter_cmds) > 2: return [python_executable] + \ interpreter_cmds[2:] + cmd_line else: return [python_executable] + cmd_line elif sys.platform == 'win32': # unix: no cover if interpreter_cmds[0] == '/usr/bin/env': return interpreter_cmds[1:] + cmd_line elif interpreter_cmds[0] in ('/bin/bash', '/bin/sh') and \ 'SHELL' in os.environ: return [os.environ['SHELL']] + cmd_line return interpreter_cmds + cmd_line
def __init__(self, entity_id, entity_type='Aircraft', uxas_bin=None): """Initialize an UxAS instance. :param entity_id: the entity id :type entity_id: str :param entity_type: the entity type :type entity_type: str :param uxas_bin: location of the uxas executable. If None try to find uxas on the path. :type uxas_bin: str | None """ if uxas_bin is None: self.uxas_bin = which('uxas') else: self.uxas_bin = uxas_bin self.cfg_path = None self.cfg = UxASConfig(entity_id, entity_type) self.process = None
def __error(self, error: Exception, cmds: list[CmdLine]) -> None: """Set pid to -1 and status to 127 before closing files.""" self.close_files() logger.error(error) def not_found(path: str) -> NoReturn: """Raise OSError. :param path: path of the executable """ logger.error("%s not found", path) e3.log.debug("PATH=%s", os.environ["PATH"]) raise OSError(errno.ENOENT, f"No such file or directory, {path} not found") # Try to send an helpful message if one of the executable has not # been found. for cmd in cmds: if which(cmd[0], default=None) is None: not_found(cmd[0])
def locate_script(name): return which(name, default=os.path.join(scripts_dir, name))
def spark_install_path(): """the location of the SPARK install""" exec_loc = which("gnatprove") return os.path.dirname(os.path.dirname(exec_loc))
def git(request): if not which("git"): if request.config.getoption("ci"): pytest.fail("git not available") else: pytest.skip("git not available")
def git(request): if not which('git'): if request.config.getoption('ci'): pytest.fail('git not available') else: pytest.skip('git not available')
def update_external( self, url: str, revision: Optional[str] ) -> tuple[ReturnValue, str, str]: """Update working dir using a local directory. :param url: path to the repository :param revision: ignored If <url>/.git is a directory then git ls-files will be called to get the list of files to ignore. """ if os.path.isdir(self.working_dir): old_commit = get_filetree_state(self.working_dir) else: old_commit = "" ignore_list: list[str] = [] if which("rsync") and "use-rsync" in os.environ.get( "E3_ENABLE_FEATURE", "" ).split(","): # Run rsync using -a but without preserving timestamps. --update switch # is also used to skip files that are older in the user directory than # in the checkout itself. This ensure rsync remain efficient event when # timestamps are not preserved (otherwise rsync has to compute checksum # of all file as quick check cannot be used when timestamp is not # preserved). rsync_cmd = [ "rsync", "--update", "-rlpgoD", f"{unixpath(url)}/", f"{unixpath(self.working_dir)}", "--delete-excluded", ] + [f"--exclude={el}" for el in VCS_IGNORE_LIST] if os.path.isdir(os.path.join(url, ".git")) and os.path.isfile( os.path.join(url, ".gitignore") ): rsync_cmd.append("--filter=:- .gitignore") p = Run(rsync_cmd, cwd=url, output=None) if p.status != 0: raise e3.error.E3Error("rsync failed") else: if os.path.isdir(os.path.join(url, ".git")): # It seems that this is a git repository. Get the list of files to # ignore try: g = GitRepository(working_tree=url) ignore_list_lines = g.git_cmd( [ "ls-files", "-o", "--ignored", "--exclude-standard", "--directory", ], output=PIPE, ).out ignore_list = [ f"/{f.strip().rstrip('/')}" for f in ignore_list_lines.splitlines() ] logger.debug("Ignore in external: %s", ignore_list) except Exception: # defensive code # don't crash on exception pass sync_tree( url, self.working_dir, preserve_timestamps=False, delete_ignore=True, ignore=list(VCS_IGNORE_LIST) + ignore_list, ) new_commit = get_filetree_state(self.working_dir) if new_commit == old_commit: return ReturnValue.unchanged, old_commit, new_commit else: return ReturnValue.success, old_commit, new_commit