Skip to content

Commit

Permalink
Use pathlib in launch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 30, 2022
1 parent 4ebfe1b commit 30c62a0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pip_run/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def load(*args):
cmd = (sys.executable, '-m', 'pip', 'install', '-t', sp(target)) + args
env = dict(os.environ, PIP_QUIET="1")
Install.parse(args) and empty(target) and subprocess.check_call(cmd, env=env)
yield str(target)
yield target


@contextlib.contextmanager
Expand Down
19 changes: 8 additions & 11 deletions pip_run/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def _read(cls, target):
As .pth files aren't honored except in site dirs,
read the paths indicated by them.
"""
target_path = pathlib.Path(target)
pth_files = target_path.glob('*.pth')
pth_files = target.glob('*.pth')
file_items = map(cls._read_file, pth_files)
return itertools.chain.from_iterable(file_items)

Expand All @@ -47,8 +46,7 @@ def inject_sitecustomize(target):
site.addsitedir({target!r})
"""
).lstrip()
sc_fn = pathlib.Path(target) / 'sitecustomize.py'
sc_fn.write_text(hook)
target.joinpath('sitecustomize.py').write_text(hook)


def _pythonpath():
Expand All @@ -61,13 +59,12 @@ def _build_env(target):
"""
key = _pythonpath()
env = dict(os.environ)
suffix = env.get(key)
prefix = (target,)
items = itertools.chain(
prefix, PathReader._read(target), (suffix,) if suffix else ()
)
previous = env.get(key)
suffix = (previous,) * bool(previous)
prefix = (os.fspath(target),)
items = itertools.chain(prefix, PathReader._read(target), suffix)
joined = os.pathsep.join(items)
env[key] = joined
env[key] = os.fspath(joined)
return env


Expand All @@ -76,7 +73,7 @@ def _setup_env(target):
return _build_env(target)


def with_path(target, params):
def with_path(target: pathlib.Path, params):
"""
Launch Python with target on the path and params
"""
Expand Down
33 changes: 17 additions & 16 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,49 @@
import subprocess
import textwrap
import os
import pathlib

import pytest

from pip_run import launch


def test_with_path(tmpdir, capfd):
def test_with_path(tmp_path, capfd):
params = ['-c', 'import sys; sys.stdout.write("\\n".join(sys.path))']
res = launch.with_path(str(tmpdir), params)
res = launch.with_path(tmp_path, params)
assert res == 0
out, err = capfd.readouterr()
assert tmpdir in out.split(os.linesep)
assert str(tmp_path) in out.split(os.linesep)


def test_with_path_result_code(tmpdir):
def test_with_path_result_code(tmp_path):
"""
result code should be non-zero on error
"""
params = ['-c', "raise ValueError()"]
res = launch.with_path(str(tmpdir), params)
res = launch.with_path(tmp_path, params)
assert res > 0


@pytest.mark.xfail(reason="cleanup can't occur with execv; #4")
def test_with_path_overlay(tmpdir, capfd):
def test_with_path_overlay(tmp_path, capfd):
params = ['-c', 'import sys; sys.stdout.write("\\n".join(sys.path))']
# launch subprocess so as not to overlay the test process
script = (
textwrap.dedent(
"""
import pip_run.launch
pip_run.launch.with_path_overlay({tmpdir!r}, {params!r})
pip_run.launch.with_path_overlay({temp_dir!r}, {params!r})
print("cleanup")
"""
)
.strip()
.replace('\n', '; ')
.format(tmpdir=str(tmpdir), params=params)
.format(temp_dir=str(tmp_path), params=params)
)
subprocess.Popen([sys.executable, '-c', script]).wait()
out, err = capfd.readouterr()
assert str(tmpdir) in out.split(os.linesep)
assert str(tmp_path) in out.split(os.linesep)
assert "cleanup" in out


Expand All @@ -54,30 +55,30 @@ def clean_pythonpath(monkeypatch):

def test_build_env(clean_pythonpath):
os.environ['PYTHONPATH'] = 'something'
env = launch._build_env('else')
env = launch._build_env(pathlib.Path('else'))
expected = os.pathsep.join(('else', 'something'))
assert env['PYTHONPATH'] == expected

os.environ['PYTHONPATH'] = ''
env = launch._build_env('something')
env = launch._build_env(pathlib.Path('something'))
assert env['PYTHONPATH'] == 'something'

initial = os.pathsep.join(['something', 'else'])
os.environ['PYTHONPATH'] = initial
env = launch._build_env('a')
env = launch._build_env(pathlib.Path('a'))
expected = os.pathsep.join(['a', 'something', 'else'])
assert env['PYTHONPATH'] == expected


# protect against ResourceWarning (#56)
@pytest.mark.filterwarnings("error")
def test_build_env_includes_pth_files(tmpdir, clean_pythonpath):
def test_build_env_includes_pth_files(tmp_path, clean_pythonpath):
"""
If during _build_env, there are .pth files in the target directory,
they should be processed to include any paths indicated there.
See #6 for rationale.
"""
(tmpdir / 'foo.pth').write_text('pkg-1.0', encoding='utf-8')
env = launch._build_env(str(tmpdir))
expected = os.pathsep.join([str(tmpdir), str(tmpdir / 'pkg-1.0')])
(tmp_path / 'foo.pth').write_text('pkg-1.0', encoding='utf-8')
env = launch._build_env(tmp_path)
expected = os.pathsep.join([str(tmp_path), str(tmp_path / 'pkg-1.0')])
assert env['PYTHONPATH'] == expected

0 comments on commit 30c62a0

Please sign in to comment.