Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pth: Add a add_pth option to py_pyenv. #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,20 @@ def install_site_file(site_packages_path: pathlib.Path, file: EnvFile) -> None:
site_path.symlink_to(file.path.resolve())


def install_files(env_path: pathlib.Path, files: List[EnvFile]) -> None:
def install_files(env_path: pathlib.Path, files: List[EnvFile], add_pth: bool) -> None:
site_packages_path = find_site_packages(env_path)
pth = site_packages_path / "venv.pth"
pths = set()
for file in files:
if is_data_file(file):
install_data_file(env_path, file)
else:
install_site_file(site_packages_path, file)
if add_pth:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're creating a pth file, do we still need to call install_site_file? Isn't that redundant?

pths.add(file.env_path.parts[0])
else:
install_site_file(site_packages_path, file)
if add_pth:
pth.write_text("\n".join(pths), encoding="utf-8")


# A copy of importlib_metadata:entry_points that takes a list of search paths.
Expand Down Expand Up @@ -266,7 +273,7 @@ def main():
builder = venv.EnvBuilder(clear=True, symlinks=True, with_pip=True)
builder.create(str(env_path))

install_files(env_path, files)
install_files(env_path, files, build_env_input.get("add_pth", False))
generate_console_scripts(env_path)

extra_commands = build_env_input.get("commands")
Expand Down
5 changes: 4 additions & 1 deletion venv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _py_venv_deps_impl(ctx):
"workspace": ctx.workspace_name,
"imports": imports,
"files": files,
"add_pth": ctx.attr.add_pth,
"commands": ctx.attr.commands,
"always_link": ctx.attr.always_link,
}
Expand All @@ -58,12 +59,13 @@ _py_venv_deps = rule(
"data": attr.label_list(),
"commands": attr.string_list(),
"always_link": attr.bool(),
"add_pth": attr.bool(),
"output": attr.output(),
},
toolchains = [PYTHON_TOOLCHAIN_TYPE],
)

def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, **kwargs):
def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, add_pth = False, **kwargs):
deps = deps or []
data = data or []
extra_pip_commands = extra_pip_commands or []
Expand All @@ -78,6 +80,7 @@ def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_li
commands = extra_pip_commands,
always_link = always_link,
output = out_name,
add_pth = add_pth,
**kwargs,
)

Expand Down