Skip to content
Merged
Show file tree
Hide file tree
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
86 changes: 82 additions & 4 deletions experimental/rules_python_external/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,94 @@ use this attribute to specify its BUILD target. This allows pip_repository to in
pip using the same interpreter as your toolchain. If set, takes precedence over
python_interpreter.
"""),
"quiet": attr.bool(default = True),
"requirements": attr.label(allow_single_file = True, mandatory = True),
"quiet": attr.bool(
default = True,
doc = "If True, suppress printing stdout and stderr output to the terminal.",
),
"requirements": attr.label(
allow_single_file = True,
mandatory = True,
doc = "A 'requirements.txt' pip requirements file.",
),
# 600 is documented as default here: https://docs.bazel.build/versions/master/skylark/lib/repository_ctx.html#execute
"timeout": attr.int(default = 600),
"wheel_env": attr.string_dict(),
"timeout": attr.int(
default = 600,
doc = "Timeout (in seconds) on the rule's execution duration.",
),
},
implementation = _pip_repository_impl,
doc = """A rule for importing `requirements.txt` dependencies into Bazel.

This rule imports a `requirements.txt` file and generates a new
`requirements.bzl` file. This is used via the `WORKSPACE` pattern:

```python
pip_repository(
name = "foo",
requirements = ":requirements.txt",
)
```

You can then reference imported dependencies from your `BUILD` file with:

```python
load("@foo//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("requests"),
requirement("numpy"),
],
)
```

Or alternatively:
```python
load("@foo//:requirements.bzl", "all_requirements")
py_binary(
name = "baz",
...
deps = [
":foo",
] + all_requirements,
)
```
""",
)

def pip_install(requirements, name = DEFAULT_REPOSITORY_NAME, **kwargs):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really thin macro. Not doing much at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah in my PR to promote to public API, I use this as a spot to add the fetch of external repos like pypi__pip

"""Imports a `requirements.txt` file and generates a new `requirements.bzl` file.

This is used via the `WORKSPACE` pattern:

```python
pip_install(
requirements = ":requirements.txt",
)
```

You can then reference imported dependencies from your `BUILD` file with:

```python
load("@pip//:requirements.bzl", "requirement")
py_library(
name = "bar",
...
deps = [
"//my/other:dep",
requirement("requests"),
requirement("numpy"),
],
)
```

Args:
requirements: A 'requirements.txt' pip requirements file.
name: A unique name for the created external repository (default 'pip').
**kwargs: Keyword arguments passed directly to the `pip_repository` repository rule.
"""
pip_repository(
name = name,
requirements = requirements,
Expand Down
13 changes: 8 additions & 5 deletions experimental/rules_python_external/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ py_library(
all_requirements = [name for (name, _, _) in _RULE_DEPS]

def requirement(pkg):
return "@pypi__"+ pkg + "//:lib"
return "@pypi__" + pkg + "//:lib"

def rules_python_external_dependencies():
"""
Fetch dependencies these rules depend on. Workspaces that use the rules_python_external should call this.
"""
for (name, url, sha256) in _RULE_DEPS:
maybe(
http_archive,
name,
url=url,
sha256=sha256,
type="zip",
build_file_content=_GENERIC_WHEEL,
url = url,
sha256 = sha256,
type = "zip",
build_file_content = _GENERIC_WHEEL,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From running buildifier on file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a task to make that automated (I like to do it as a git pre-commit hook so you don't need to think about it)

)