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" )
49
510 rctx .file ("BUILD" , "" )
611
12+ # Get the root directory of these rules
13+ rules_root = rctx .path (Label ("//:BUILD" )).dirname
14+ thirdparty_roots = [
15+ # Includes all the external dependencies from repositories.bzl
16+ rctx .path (Label ("@" + repo + "//:BUILD.bazel" )).dirname
17+ for repo in all_requirements
18+ ]
19+ pypath = ":" .join ([str (p ) for p in [rules_root ] + thirdparty_roots ])
20+
721 result = rctx .execute (
822 [
923 rctx .which (rctx .attr .python_interpreter ),
10- rctx .path (rctx .attr ._script ),
24+ rctx .path (rctx .attr ._script ). dirname ,
1125 "--requirements" ,
1226 rctx .path (rctx .attr .requirements ),
1327 "--repo" ,
1428 "@%s" % rctx .attr .name ,
1529 ],
16-
17- environment = rctx .attr .wheel_env ,
30+ environment = {
31+ # Manually construct the PYTHONPATH since we cannot use the toolchain here
32+ "PYTHONPATH" : pypath
33+ },
1834 )
1935 if result .return_code :
20- fail ("failed to create pip repository : %s (%s)" % (result .stdout , result .stderr ))
36+ fail ("rules_python_external failed : %s (%s)" % (result .stdout , result .stderr ))
2137
2238 return
2339
@@ -28,10 +44,14 @@ pip_repository = repository_rule(
2844 "wheel_env" : attr .string_dict (),
2945 "python_interpreter" : attr .string (default = "python3" ),
3046 "_script" : attr .label (
31- executable = True ,
32- default = Label ("//tools:wheel_wrapper.py" ),
33- cfg = "host" ,
47+ executable = True , default = Label ("//src:__main__.py" ), cfg = "host" ,
3448 ),
3549 },
3650 implementation = _pip_repository_impl ,
3751)
52+
53+
54+ def pip_install (requirements , name = DEFAULT_REPOSITORY_NAME ):
55+ pip_repository (
56+ name = name , requirements = requirements ,
57+ )
0 commit comments