|
15 | 15 | virtualenv_name = os.path.basename(virtualenv_home)
|
16 | 16 | runfiles_dir = os.path.realpath(os.environ["RUNFILES_DIR"])
|
17 | 17 | builddir = os.path.realpath(os.environ["BUILD_WORKING_DIRECTORY"])
|
18 |
| - |
19 |
| -# Chop off the runfiles tree prefix |
20 |
| -virtualenv_path = virtualenv_home.lstrip(runfiles_dir).lstrip("/") |
21 |
| -# Chop off the repo name to get a repo-relative path |
22 |
| -virtualenv_path = virtualenv_path[virtualenv_path.find("/"):] |
| 18 | +target_package, target_name = os.environ["BAZEL_TARGET"].split("//", 1)[1].split(":") |
23 | 19 |
|
24 | 20 | PARSER = argparse.ArgumentParser(
|
25 | 21 | prog="link",
|
26 | 22 | usage=__doc__,
|
| 23 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
27 | 24 | )
|
28 | 25 |
|
29 | 26 | PARSER.add_argument(
|
30 |
| - "--venv-name", |
31 |
| - dest="venv_name", |
32 |
| - default=virtualenv_name, |
33 |
| - help="Name to link the virtualenv under.", |
| 27 | + "--dest", |
| 28 | + dest="dest", |
| 29 | + default=builddir, |
| 30 | + help="Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY.", |
34 | 31 | )
|
35 | 32 |
|
36 | 33 | PARSER.add_argument(
|
37 |
| - "--dest", |
38 |
| - dest="dest", |
39 |
| - default=os.path.join(builddir, os.path.dirname(virtualenv_path)), |
40 |
| - help="Dir to link the virtualenv into", |
| 34 | + "--name", |
| 35 | + dest="name", |
| 36 | + default=".{}+{}".format(target_package.replace("/", "+"), virtualenv_name.lstrip(".")), |
| 37 | + help="Name to link the virtualenv as.", |
41 | 38 | )
|
42 | 39 |
|
| 40 | + |
43 | 41 | if __name__ == "__main__":
|
| 42 | + PARSER.print_help(sys.stdout) |
44 | 43 | opts = PARSER.parse_args()
|
45 |
| - dest = Path(os.path.join(opts.dest, opts.venv_name)) |
46 |
| - print("""\ |
47 |
| -Linking: {venv_home} -> {venv_path} |
| 44 | + dest = Path(os.path.join(opts.dest, opts.name)) |
| 45 | + print(""" |
48 | 46 |
|
49 |
| -To activate the virtualenv run: |
50 |
| - source {venv_path}/bin/activate |
| 47 | +Linking: {venv_home} -> {venv_path} |
51 | 48 | """.format(
|
52 | 49 | venv_home = virtualenv_home,
|
53 | 50 | venv_path = dest,
|
54 | 51 | ))
|
55 | 52 |
|
56 | 53 | if dest.exists() and dest.is_symlink() and dest.readlink() == Path(virtualenv_home):
|
57 | 54 | print("Link is up to date!")
|
58 |
| - exit(0) |
59 | 55 |
|
60 | 56 | else:
|
61 | 57 | try:
|
|
67 | 63 | # From -> to
|
68 | 64 | dest.symlink_to(virtualenv_home, target_is_directory=True)
|
69 | 65 | print("Link created!")
|
70 |
| - exit(0) |
| 66 | + |
| 67 | + print(""" |
| 68 | +To configure the virtualenv in your IDE, configure an interpreter with the homedir |
| 69 | + {venv_path} |
| 70 | +
|
| 71 | + Please note that you may encounter issues if your editor doesn't evaluate |
| 72 | + the `activate` script. If you do please file an issue at |
| 73 | + https://github.com/aspect-build/rules_py/issues/new?template=BUG-REPORT.yaml |
| 74 | +
|
| 75 | +To activate the virtualenv in your shell run |
| 76 | + source {venv_path}/bin/activate |
| 77 | +
|
| 78 | +virtualenvwrapper users may further want to |
| 79 | + $ ln -s {venv_path} $WORKON_HOME/{venv_name} |
| 80 | +""".format( |
| 81 | + venv_home = virtualenv_home, |
| 82 | + venv_name = opts.name, |
| 83 | + venv_path = dest, |
| 84 | +)) |
0 commit comments