Skip to content
Closed
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
54 changes: 34 additions & 20 deletions py/private/py_venv/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,43 @@
virtualenv_name = os.path.basename(virtualenv_home)
runfiles_dir = os.path.realpath(os.environ["RUNFILES_DIR"])
builddir = os.path.realpath(os.environ["BUILD_WORKING_DIRECTORY"])

# Chop off the runfiles tree prefix
virtualenv_path = virtualenv_home.lstrip(runfiles_dir).lstrip("/")
# Chop off the repo name to get a repo-relative path
virtualenv_path = virtualenv_path[virtualenv_path.find("/"):]
target_package, target_name = os.environ["BAZEL_TARGET"].split("//", 1)[1].split(":")

PARSER = argparse.ArgumentParser(
prog="link",
usage=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)

PARSER.add_argument(
"--venv-name",
dest="venv_name",
default=virtualenv_name,
help="Name to link the virtualenv under.",
"--dest",
dest="dest",
default=builddir,
help="Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY.",
)

PARSER.add_argument(
"--dest",
dest="dest",
default=os.path.join(builddir, os.path.dirname(virtualenv_path)),
help="Dir to link the virtualenv into",
"--name",
dest="name",
default=".{}+{}".format(target_package.replace("/", "+"), virtualenv_name.lstrip(".")),
help="Name to link the virtualenv as.",
)


if __name__ == "__main__":
PARSER.print_help(sys.stdout)
opts = PARSER.parse_args()
dest = Path(os.path.join(opts.dest, opts.venv_name))
print("""\
Linking: {venv_home} -> {venv_path}
dest = Path(os.path.join(opts.dest, opts.name))
print("""

To activate the virtualenv run:
source {venv_path}/bin/activate
Linking: {venv_home} -> {venv_path}
""".format(
venv_home = virtualenv_home,
venv_path = dest,
))

if dest.exists() and dest.is_symlink() and dest.readlink() == Path(virtualenv_home):
print("Link is up to date!")
exit(0)

else:
try:
Expand All @@ -67,4 +63,22 @@
# From -> to
dest.symlink_to(virtualenv_home, target_is_directory=True)
print("Link created!")
exit(0)

print("""
To configure the virtualenv in your IDE, configure an interpreter with the homedir
{venv_path}

Please note that you may encounter issues if your editor doesn't evaluate
the `activate` script. If you do please file an issue at
https://github.com/aspect-build/rules_py/issues/new?template=BUG-REPORT.yaml

To activate the virtualenv in your shell run
source {venv_path}/bin/activate

virtualenvwrapper users may further want to
$ ln -s {venv_path} $WORKON_HOME/{venv_name}
""".format(
venv_home = virtualenv_home,
venv_name = opts.name,
venv_path = dest,
))