From 3a6587002124b40a612f934cc73ca013face47a7 Mon Sep 17 00:00:00 2001 From: "Reid D. McKenzie" Date: Mon, 9 Jun 2025 17:34:54 -0600 Subject: [PATCH 1/2] tweak(venv_link): Place venvs in src dirs --- py/private/py_venv/link.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/py/private/py_venv/link.py b/py/private/py_venv/link.py index 4b2eb7c4..4bb6b0ec 100644 --- a/py/private/py_venv/link.py +++ b/py/private/py_venv/link.py @@ -15,11 +15,7 @@ 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", @@ -36,7 +32,7 @@ PARSER.add_argument( "--dest", dest="dest", - default=os.path.join(builddir, os.path.dirname(virtualenv_path)), + default=os.path.join(builddir, target_package), help="Dir to link the virtualenv into", ) From 476e953dfe77ef0650b642a7c1fe869171f7267e Mon Sep 17 00:00:00 2001 From: "Reid D. McKenzie" Date: Mon, 9 Jun 2025 17:55:51 -0600 Subject: [PATCH 2/2] More of same. --- py/private/py_venv/link.py | 48 ++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/py/private/py_venv/link.py b/py/private/py_venv/link.py index 4bb6b0ec..dfe9fd7a 100644 --- a/py/private/py_venv/link.py +++ b/py/private/py_venv/link.py @@ -20,30 +20,31 @@ 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, target_package), - 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, @@ -51,7 +52,6 @@ if dest.exists() and dest.is_symlink() and dest.readlink() == Path(virtualenv_home): print("Link is up to date!") - exit(0) else: try: @@ -63,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, +))