1+ load ("//:repositories.bzl" , "all_requirements" )
2+
3+ DEFAULT_REPOSITORY_NAME = "pip"
4+
5+
16def _pip_repository_impl (rctx ):
27 if not rctx .which (rctx .attr .python_interpreter ):
38 fail ("python interpreter not found" )
@@ -7,17 +12,26 @@ def _pip_repository_impl(rctx):
712 result = rctx .execute (
813 [
914 rctx .which (rctx .attr .python_interpreter ),
10- rctx .path (rctx .attr ._script ),
15+ rctx .path (rctx .attr ._script ). dirname ,
1116 "--requirements" ,
1217 rctx .path (rctx .attr .requirements ),
1318 "--repo" ,
1419 "@%s" % rctx .attr .name ,
1520 ],
16-
17- environment = rctx .attr .wheel_env ,
21+ environment = {
22+ # Manually construct the PYTHONPATH since we cannot use the toolchain here
23+ "PYTHONPATH" : ":" .join (
24+ # Includes the root of this repo and all the external dependencies from repositories.bzl
25+ [str (rctx .path (rctx .attr ._script ).dirname .dirname )]
26+ + [
27+ str (rctx .path (Label ("@" + repo + "//:BUILD.bazel" )).dirname )
28+ for repo in all_requirements
29+ ]
30+ )
31+ },
1832 )
1933 if result .return_code :
20- fail ("failed to create pip repository : %s (%s)" % (result .stdout , result .stderr ))
34+ fail ("rules_python_external failed : %s (%s)" % (result .stdout , result .stderr ))
2135
2236 return
2337
@@ -28,10 +42,14 @@ pip_repository = repository_rule(
2842 "wheel_env" : attr .string_dict (),
2943 "python_interpreter" : attr .string (default = "python3" ),
3044 "_script" : attr .label (
31- executable = True ,
32- default = Label ("//tools:wheel_wrapper.py" ),
33- cfg = "host" ,
45+ executable = True , default = Label ("//src:__main__.py" ), cfg = "host" ,
3446 ),
3547 },
3648 implementation = _pip_repository_impl ,
3749)
50+
51+
52+ def pip_install (requirements , name = DEFAULT_REPOSITORY_NAME ):
53+ pip_repository (
54+ name = name , requirements = requirements ,
55+ )
0 commit comments