Skip to content

Commit

Permalink
Do not bark when scripts dir exists already
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Aug 11, 2023
1 parent 7eea318 commit 6fb2278
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
75 changes: 75 additions & 0 deletions relenv/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,81 @@ def wrapper(self, options, args):
return wrapper

mod.InstallCommand.run = wrap(mod.InstallCommand.run)

def wrap(func):
@functools.wraps(func)
def wrapper(self, target_dir, target_temp_dir, upgrade):
from pip._internal.locations import get_scheme
from pip._internal.utils.misc import ensure_dir

ensure_dir(target_dir)

# Checking both purelib and platlib directories for installed
# packages to be moved to target directory
lib_dir_list = []

# Checking both purelib and platlib directories for installed
# packages to be moved to target directory
scheme = get_scheme("", home=target_temp_dir.path)
purelib_dir = scheme.purelib
platlib_dir = scheme.platlib
data_dir = scheme.data

if os.path.exists(purelib_dir):
lib_dir_list.append(purelib_dir)
if os.path.exists(platlib_dir) and platlib_dir != purelib_dir:
lib_dir_list.append(platlib_dir)
if os.path.exists(data_dir):
lib_dir_list.append(data_dir)

for lib_dir in lib_dir_list:
for item in os.listdir(lib_dir):
if lib_dir == data_dir:
ddir = os.path.join(data_dir, item)
if any(s.startswith(ddir) for s in lib_dir_list[:-1]):
continue
target_item_dir = os.path.join(target_dir, item)

# Special handling for scripts dir when target is used.
if TARGET.TARGET is True and item == os.path.basename(
scheme.scripts
):
if not os.path.exists(target_item_dir):
os.mkdir(target_item_dir)
for script in os.listdir(os.path.join(lib_dir, item)):
shutil.move(
os.path.join(lib_dir, item, script),
os.path.join(target_item_dir, script),
)
elif os.path.exists(target_item_dir):
if not upgrade:
mod.logger.warning(
"Target directory %s already exists. Specify "
"--upgrade to force replacement.",
target_item_dir,
)
continue
if os.path.islink(target_item_dir):
mod.logger.warning(
"Target directory %s already exists and is "
"a link. pip will not automatically replace "
"links, please remove if replacement is "
"desired.",
target_item_dir,
)
continue
if os.path.isdir(target_item_dir):
shutil.rmtree(target_item_dir)
else:
os.remove(target_item_dir)

shutil.move(os.path.join(lib_dir, item), target_item_dir)
else:
shutil.move(os.path.join(lib_dir, item), target_item_dir)

return wrapper

mod.InstallCommand._handle_target_dir = wrap(mod.InstallCommand._handle_target_dir)
return mod


Expand Down
18 changes: 18 additions & 0 deletions tests/test_verify_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,21 @@ def test_install_with_target_ignore_installed(pipexec, pyexec, build):
out = proc.stdout.decode()
assert "installed cffi" in out
assert "already satisfied: cffi" not in out

@pytest.mark.skip_unless_on_linux
def test_install_with_target_scripts(pipexec, build, minor_version):
env = os.environ.copy()
env["RELENV_DEBUG"] = "yes"
extras = build / "extras"
subprocess.run(
[str(pipexec), "install", "rend", f"--target={extras}"],
check=True,
env=env,
)
assert (extras / "bin" / "rend").exists()
subprocess.run(
[str(pipexec), "install", "cowsay", f"--target={extras}"],
check=True,
env=env,
)
assert (extras / "bin" / "cowsay").exists()

0 comments on commit 6fb2278

Please sign in to comment.