def _path_without_us():
    # Choose a path which *probably* doesn't include us
    env = dict(os.environ)
    exe = find_executable('pre-commit', _environ=env)
    while exe:
        parts = env['PATH'].split(os.pathsep)
        after = [x for x in parts if x.lower() != os.path.dirname(exe).lower()]
        if parts == after:
            raise AssertionError(exe, parts)
        env['PATH'] = os.pathsep.join(after)
        exe = find_executable('pre-commit', _environ=env)
    return env['PATH']
Ejemplo n.º 2
0
def test_find_executable_path_ext(in_tmpdir):
    """Windows exports PATHEXT as a list of extensions to automatically add
    to executables when doing PATH searching.
    """
    exe_path = os.path.abspath(write_executable(
        '/usr/bin/env sh', filename='run.myext',
    ))
    env_path = {'PATH': os.path.dirname(exe_path)}
    env_path_ext = dict(env_path, PATHEXT=os.pathsep.join(('.exe', '.myext')))
    assert parse_shebang.find_executable('run') is None
    assert parse_shebang.find_executable('run', _environ=env_path) is None
    ret = parse_shebang.find_executable('run.myext', _environ=env_path)
    assert ret == exe_path
    ret = parse_shebang.find_executable('run', _environ=env_path_ext)
    assert ret == exe_path
Ejemplo n.º 3
0
def _get_default_version():  # pragma: no cover (platform dependent)
    def _norm(path):
        _, exe = os.path.split(path.lower())
        exe, _, _ = exe.partition('.exe')
        if find_executable(exe) and exe not in {'python', 'pythonw'}:
            return exe

    # First attempt from `sys.executable` (or the realpath)
    # On linux, I see these common sys.executables:
    #
    # system `python`: /usr/bin/python -> python2.7
    # system `python2`: /usr/bin/python2 -> python2.7
    # virtualenv v: v/bin/python (will not return from this loop)
    # virtualenv v -ppython2: v/bin/python -> python2
    # virtualenv v -ppython2.7: v/bin/python -> python2.7
    # virtualenv v -ppypy: v/bin/python -> v/bin/pypy
    for path in {sys.executable, os.path.realpath(sys.executable)}:
        exe = _norm(path)
        if exe:
            return exe

    # Next try the `pythonX.X` executable
    exe = 'python{}.{}'.format(*sys.version_info)
    if find_executable(exe):
        return exe

    if _find_by_py_launcher(exe):
        return exe

    # Give a best-effort try for windows
    if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
        return exe

    # We tried!
    return 'default'
Ejemplo n.º 4
0
def _get_default_version():  # pragma: no cover (platform dependent)
    def _norm(path):
        _, exe = os.path.split(path.lower())
        exe, _, _ = exe.partition('.exe')
        if find_executable(exe) and exe not in {'python', 'pythonw'}:
            return exe

    # First attempt from `sys.executable` (or the realpath)
    # On linux, I see these common sys.executables:
    #
    # system `python`: /usr/bin/python -> python2.7
    # system `python2`: /usr/bin/python2 -> python2.7
    # virtualenv v: v/bin/python (will not return from this loop)
    # virtualenv v -ppython2: v/bin/python -> python2
    # virtualenv v -ppython2.7: v/bin/python -> python2.7
    # virtualenv v -ppypy: v/bin/python -> v/bin/pypy
    for path in {sys.executable, os.path.realpath(sys.executable)}:
        exe = _norm(path)
        if exe:
            return exe

    # Next try the `pythonX.X` executable
    exe = 'python{}.{}'.format(*sys.version_info)
    if find_executable(exe):
        return exe

    # Give a best-effort try for windows
    if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
        return exe

    # We tried!
    return 'default'
Ejemplo n.º 5
0
def norm_version(version: str) -> str:
    if version == C.DEFAULT:
        return os.path.realpath(sys.executable)

    # first see if our current executable is appropriate
    if _sys_executable_matches(version):
        return sys.executable

    if os.name == 'nt':  # pragma: no cover (windows)
        version_exec = _find_by_py_launcher(version)
        if version_exec:
            return version_exec

        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

        # If it is in the form pythonx.x search in the default
        # place on windows
        if version.startswith('python'):
            default_folder_name = version.replace('.', '')
            return fr'C:\{default_folder_name}\python.exe'

    # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 6
0
def test_find_executable_path_ext(in_tmpdir):
    """Windows exports PATHEXT as a list of extensions to automatically add
    to executables when doing PATH searching.
    """
    exe_path = os.path.abspath(
        write_executable(
            '/usr/bin/env sh',
            filename='run.myext',
        ))
    env_path = {'PATH': os.path.dirname(exe_path)}
    env_path_ext = dict(env_path, PATHEXT=os.pathsep.join(('.exe', '.myext')))
    assert parse_shebang.find_executable('run') is None
    assert parse_shebang.find_executable('run', _environ=env_path) is None
    ret = parse_shebang.find_executable('run.myext', _environ=env_path)
    assert ret == exe_path
    ret = parse_shebang.find_executable('run', _environ=env_path_ext)
    assert ret == exe_path
Ejemplo n.º 7
0
def get_default_version() -> str:
    # nodeenv does not yet support `-n system` on windows
    if sys.platform == 'win32':
        return C.DEFAULT
    # if node is already installed, we can save a bunch of setup time by
    # using the installed version
    elif all(parse_shebang.find_executable(exe) for exe in ('node', 'npm')):
        return 'system'
    else:
        return C.DEFAULT
Ejemplo n.º 8
0
def xargs(
    cmd: Tuple[str, ...],
    varargs: Sequence[str],
    *,
    color: bool = False,
    target_concurrency: int = 1,
    _max_length: int = _get_platform_max_length(),
    **kwargs: Any,
) -> Tuple[int, bytes]:
    """A simplified implementation of xargs.

    color: Make a pty if on a platform that supports it
    target_concurrency: Target number of partitions to run concurrently
    """
    cmd_fn = cmd_output_p if color else cmd_output_b
    retcode = 0
    stdout = b''

    try:
        cmd = parse_shebang.normalize_cmd(cmd)
    except parse_shebang.ExecutableNotFoundError as e:
        return e.to_output()[:2]

    # on windows, batch files have a separate length limit than windows itself
    if (sys.platform == 'win32' and cmd[0].lower().endswith(
        ('.bat', '.cmd'))):  # pragma: win32 cover
        # this is implementation details but the command gets translated into
        # full/path/to/cmd.exe /c *cmd
        cmd_exe = parse_shebang.find_executable('cmd.exe')
        # 1024 is additionally subtracted to give headroom for further
        # expansion inside the batch file
        _max_length = 8192 - len(cmd_exe) - len(' /c ') - 1024

    partitions = partition(cmd, varargs, target_concurrency, _max_length)

    def run_cmd_partition(
        run_cmd: Tuple[str, ...], ) -> Tuple[int, bytes, Optional[bytes]]:
        return cmd_fn(
            *run_cmd,
            retcode=None,
            stderr=subprocess.STDOUT,
            **kwargs,
        )

    threads = min(len(partitions), target_concurrency)
    with _thread_mapper(threads) as thread_map:
        results = thread_map(run_cmd_partition, partitions)

        for proc_retcode, proc_out, _ in results:
            retcode = max(retcode, proc_retcode)
            stdout += proc_out

    return retcode, stdout
Ejemplo n.º 9
0
def norm_version(version):
    if os.name == 'nt':  # pragma: no cover (windows)
        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

        # If it is in the form pythonx.x search in the default
        # place on windows
        if version.startswith('python'):
            return r'C:\{}\python.exe'.format(version.replace('.', ''))

        # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 10
0
def norm_version(version):
    if os.name == 'nt':  # pragma: no cover (windows)
        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

        # If it is in the form pythonx.x search in the default
        # place on windows
        if version.startswith('python'):
            return r'C:\{}\python.exe'.format(version.replace('.', ''))

        # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 11
0
def exe_exists(exe: str) -> bool:
    found = parse_shebang.find_executable(exe)
    if found is None:  # exe exists
        return False

    homedir = os.path.expanduser('~')
    try:
        common: Optional[str] = os.path.commonpath((found, homedir))
    except ValueError:  # on windows, different drives raises ValueError
        common = None

    return (not SHIMS_RE.search(found) and  # it is not in a /shims/ directory
            common != homedir  # it is not in the home directory
            )
Ejemplo n.º 12
0
def get_default_version() -> str:  # pragma: no cover (platform dependent)
    # First attempt from `sys.executable` (or the realpath)
    exe = _find_by_sys_executable()
    if exe:
        return exe

    # Next try the `pythonX.X` executable
    exe = f'python{sys.version_info[0]}.{sys.version_info[1]}'
    if find_executable(exe):
        return exe

    if _find_by_py_launcher(exe):
        return exe

    # We tried!
    return C.DEFAULT
Ejemplo n.º 13
0
def norm_version(version: str) -> Optional[str]:
    if version == C.DEFAULT:  # use virtualenv's default
        return None
    elif _sys_executable_matches(version):  # virtualenv defaults to our exe
        return None

    if os.name == 'nt':  # pragma: no cover (windows)
        version_exec = _find_by_py_launcher(version)
        if version_exec:
            return version_exec

        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

    # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 14
0
def exe_exists(exe: str) -> bool:
    found = parse_shebang.find_executable(exe)
    if found is None:  # exe exists
        return False

    homedir = os.path.expanduser('~')
    try:
        common: str | None = os.path.commonpath((found, homedir))
    except ValueError:  # on windows, different drives raises ValueError
        common = None

    return (
        # it is not in a /shims/ directory
        not SHIMS_RE.search(found) and (
            # the homedir is / (docker, service user, etc.)
            os.path.dirname(homedir) == homedir or
            # the exe is not contained in the home directory
            common != homedir))
Ejemplo n.º 15
0
def _get_default_version():  # pragma: no cover (platform dependent)
    # First attempt from `sys.executable` (or the realpath)
    exe = _find_by_sys_executable()
    if exe:
        return exe

    # Next try the `pythonX.X` executable
    exe = 'python{}.{}'.format(*sys.version_info)
    if find_executable(exe):
        return exe

    if _find_by_py_launcher(exe):
        return exe

    # Give a best-effort try for windows
    if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
        return exe

    # We tried!
    return C.DEFAULT
Ejemplo n.º 16
0
def norm_version(version: str) -> str:
    if version == C.DEFAULT:
        return os.path.realpath(sys.executable)

    # first see if our current executable is appropriate
    if _sys_executable_matches(version):
        return sys.executable

    if os.name == 'nt':  # pragma: no cover (windows)
        version_exec = _find_by_py_launcher(version)
        if version_exec:
            return version_exec

        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

    # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 17
0
def get_default_version() -> str:  # pragma: no cover (platform dependent)
    # First attempt from `sys.executable` (or the realpath)
    exe = _find_by_sys_executable()
    if exe:
        return exe

    # Next try the `pythonX.X` executable
    exe = f'python{sys.version_info[0]}.{sys.version_info[1]}'
    if find_executable(exe):
        return exe

    if _find_by_py_launcher(exe):
        return exe

    # Give a best-effort try for windows
    default_folder_name = exe.replace('.', '')
    if os.path.exists(fr'C:\{default_folder_name}\python.exe'):
        return exe

    # We tried!
    return C.DEFAULT
Ejemplo n.º 18
0
def norm_version(version):
    if os.name == 'nt':  # pragma: no cover (windows)
        # first see if our current executable is appropriate
        if _sys_executable_matches(version):
            return sys.executable

        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

        version_exec = _find_by_py_launcher(version)
        if version_exec:
            return version_exec

        # If it is in the form pythonx.x search in the default
        # place on windows
        if version.startswith('python'):
            return r'C:\{}\python.exe'.format(version.replace('.', ''))

    # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 19
0
def norm_version(version):
    if os.name == 'nt':  # pragma: no cover (windows)
        # first see if our current executable is appropriate
        if _sys_executable_matches(version):
            return sys.executable

        version_exec = _find_by_py_launcher(version)
        if version_exec:
            return version_exec

        # Try looking up by name
        version_exec = find_executable(version)
        if version_exec and version_exec != version:
            return version_exec

        # If it is in the form pythonx.x search in the default
        # place on windows
        if version.startswith('python'):
            return r'C:\{}\python.exe'.format(version.replace('.', ''))

    # Otherwise assume it is a path
    return os.path.expanduser(version)
Ejemplo n.º 20
0
def test_find_executable_on_path():
    assert parse_shebang.find_executable('echo') == _echo_exe()
Ejemplo n.º 21
0
def test_find_executable_full_path():
    assert parse_shebang.find_executable(sys.executable) == sys.executable
Ejemplo n.º 22
0
 def _norm(path: str) -> Optional[str]:
     _, exe = os.path.split(path.lower())
     exe, _, _ = exe.partition('.exe')
     if exe not in {'python', 'pythonw'} and find_executable(exe):
         return exe
     return None
Ejemplo n.º 23
0
 def _norm(path: str) -> Optional[str]:
     _, exe = os.path.split(path.lower())
     exe, _, _ = exe.partition(".exe")
     if exe not in {"python", "pythonw"} and find_executable(exe):
         return exe
     return None
Ejemplo n.º 24
0
 def _norm(path):
     _, exe = os.path.split(path.lower())
     exe, _, _ = exe.partition('.exe')
     if find_executable(exe) and exe not in {'python', 'pythonw'}:
         return exe
Ejemplo n.º 25
0
    pre_commit_home=None,
    env=None,
    **kwargs,
):
    if pre_commit_home is None:
        pre_commit_home = tempdir_factory.get()
    env = env if env is not None else os.environ
    kwargs.setdefault('stderr', subprocess.STDOUT)
    # Don't want to write to the home directory
    env = dict(env, PRE_COMMIT_HOME=pre_commit_home)
    ret, out, _ = cmd_output(*args, env=env, **kwargs)
    return ret, out.replace('\r\n', '\n'), None


skipif_cant_run_coursier = pytest.mark.skipif(
    os.name == 'nt' or parse_shebang.find_executable('cs') is None,
    reason="coursier isn't installed or can't be found",
)
skipif_cant_run_docker = pytest.mark.skipif(
    os.name == 'nt' or not docker_is_running(),
    reason="Docker isn't running or can't be accessed",
)
skipif_cant_run_swift = pytest.mark.skipif(
    parse_shebang.find_executable('swift') is None,
    reason="swift isn't installed or can't be found",
)
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')


def run_opts(
    all_files=False,
Ejemplo n.º 26
0
    if pre_commit_home is None:
        pre_commit_home = tempdir_factory.get()
    env = env if env is not None else os.environ
    kwargs.setdefault('stderr', subprocess.STDOUT)
    # Don't want to write to the home directory
    env = dict(env, PRE_COMMIT_HOME=pre_commit_home)
    ret, out, _ = cmd_output(*args, env=env, **kwargs)
    return ret, out.replace('\r\n', '\n'), None


skipif_cant_run_docker = pytest.mark.skipif(
    os.name == 'nt' or not docker_is_running(),
    reason="Docker isn't running or can't be accessed",
)
skipif_cant_run_swift = pytest.mark.skipif(
    parse_shebang.find_executable('swift') is None,
    reason="swift isn't installed or can't be found",
)
xfailif_windows = pytest.mark.xfail(os.name == 'nt', reason='windows')


def run_opts(
    all_files=False,
    files=(),
    color=False,
    verbose=False,
    hook=None,
    from_ref='',
    to_ref='',
    remote_name='',
    remote_url='',
Ejemplo n.º 27
0
def test_find_executable_on_path():
    expected = distutils.spawn.find_executable('echo')
    assert parse_shebang.find_executable('echo') == expected
Ejemplo n.º 28
0
def test_find_executable_path_added(in_tmpdir):
    path = os.path.abspath(write_executable('/usr/bin/env sh'))
    assert parse_shebang.find_executable('run') is None
    with bin_on_path():
        assert parse_shebang.find_executable('run') == path
Ejemplo n.º 29
0
def test_find_executable_not_found_none():
    assert parse_shebang.find_executable('not-a-real-executable') is None
Ejemplo n.º 30
0
def test_find_executable_on_path():
    expected = distutils.spawn.find_executable('echo')
    assert parse_shebang.find_executable('echo') == expected
Ejemplo n.º 31
0
def test_find_executable_not_found_none():
    assert parse_shebang.find_executable('not-a-real-executable') is None
Ejemplo n.º 32
0
def test_find_executable_path_added(in_tmpdir):
    path = os.path.abspath(write_executable('/usr/bin/env sh'))
    assert parse_shebang.find_executable('run') is None
    with bin_on_path():
        assert parse_shebang.find_executable('run') == path
Ejemplo n.º 33
0
    env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home)
    return cmd_output(*args, env=env, **kwargs)


skipif_cant_run_docker = pytest.mark.skipif(
    docker_is_running() is False,
    reason='Docker isn\'t running or can\'t  be accessed',
)

skipif_slowtests_false = pytest.mark.skipif(
    os.environ.get('slowtests') == 'false',
    reason='slowtests=false',
)

skipif_cant_run_swift = pytest.mark.skipif(
    parse_shebang.find_executable('swift') is None,
    reason='swift isn\'t installed or can\'t be found',
)

xfailif_windows_no_ruby = pytest.mark.xfail(
    os.name == 'nt',
    reason='Ruby support not yet implemented on windows.',
)

xfailif_windows_no_node = pytest.mark.xfail(
    os.name == 'nt',
    reason='Node support not yet implemented on windows.',
)


def platform_supports_pcre():
Ejemplo n.º 34
0
def get_default_version() -> str:
    if all(parse_shebang.find_executable(exe) for exe in ("ruby", "gem")):
        return "system"
    else:
        return C.DEFAULT
Ejemplo n.º 35
0
 def _norm(path):
     _, exe = os.path.split(path.lower())
     exe, _, _ = exe.partition('.exe')
     if find_executable(exe) and exe not in {'python', 'pythonw'}:
         return exe
Ejemplo n.º 36
0
def get_default_version() -> str:
    if all(parse_shebang.find_executable(exe) for exe in ('ruby', 'gem')):
        return 'system'
    else:
        return C.DEFAULT
Ejemplo n.º 37
0
def test_find_executable_full_path():
    assert parse_shebang.find_executable(sys.executable) == sys.executable