Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ewianda committed Jul 1, 2024
1 parent d7b7d0b commit 06e6098
Show file tree
Hide file tree
Showing 19 changed files with 908 additions and 417 deletions.
28 changes: 28 additions & 0 deletions docs/ext_lock_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ lock_import.import_pdm(<a href="#lock_import.import_pdm-all_development_groups">
<a href="#lock_import.import_pdm-require_static_urls">require_static_urls</a>, <a href="#lock_import.import_pdm-target_environments">target_environments</a>)
lock_import.import_poetry(<a href="#lock_import.import_poetry-default_alias_single_version">default_alias_single_version</a>, <a href="#lock_import.import_poetry-disallow_builds">disallow_builds</a>, <a href="#lock_import.import_poetry-local_wheels">local_wheels</a>, <a href="#lock_import.import_poetry-lock_file">lock_file</a>,
<a href="#lock_import.import_poetry-project_file">project_file</a>, <a href="#lock_import.import_poetry-repo">repo</a>, <a href="#lock_import.import_poetry-target_environments">target_environments</a>)
lock_import.import_uv(<a href="#lock_import.import_uv-all_development_groups">all_development_groups</a>, <a href="#lock_import.import_uv-all_optional_groups">all_optional_groups</a>, <a href="#lock_import.import_uv-default">default</a>,
<a href="#lock_import.import_uv-default_alias_single_version">default_alias_single_version</a>, <a href="#lock_import.import_uv-development_groups">development_groups</a>, <a href="#lock_import.import_uv-disallow_builds">disallow_builds</a>, <a href="#lock_import.import_uv-local_wheels">local_wheels</a>,
<a href="#lock_import.import_uv-lock_file">lock_file</a>, <a href="#lock_import.import_uv-optional_groups">optional_groups</a>, <a href="#lock_import.import_uv-project_file">project_file</a>, <a href="#lock_import.import_uv-repo">repo</a>, <a href="#lock_import.import_uv-require_static_urls">require_static_urls</a>,
<a href="#lock_import.import_uv-target_environments">target_environments</a>)
lock_import.package(<a href="#lock_import.package-name">name</a>, <a href="#lock_import.package-always_build">always_build</a>, <a href="#lock_import.package-build_dependencies">build_dependencies</a>, <a href="#lock_import.package-build_target">build_target</a>, <a href="#lock_import.package-ignore_dependencies">ignore_dependencies</a>,
<a href="#lock_import.package-install_exclude_globs">install_exclude_globs</a>, <a href="#lock_import.package-repo">repo</a>)
</pre>
Expand Down Expand Up @@ -63,6 +67,30 @@ Import a Poetry lock file.
| <a id="lock_import.import_poetry-repo"></a>repo | The repository name | String | required | |
| <a id="lock_import.import_poetry-target_environments"></a>target_environments | A list of target environment descriptors. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `["@pycross_environments//:environments"]` |

<a id="lock_import.import_uv"></a>

### import_uv

Import a uv lock file.

**Attributes**

| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="lock_import.import_uv-all_development_groups"></a>all_development_groups | Install all dev dependencies. | Boolean | optional | `False` |
| <a id="lock_import.import_uv-all_optional_groups"></a>all_optional_groups | Install all optional dependencies. | Boolean | optional | `False` |
| <a id="lock_import.import_uv-default"></a>default | Whether to install dependencies from the default group. | Boolean | optional | `True` |
| <a id="lock_import.import_uv-default_alias_single_version"></a>default_alias_single_version | Generate aliases for all packages that have a single version in the lock file. | Boolean | optional | `False` |
| <a id="lock_import.import_uv-development_groups"></a>development_groups | List of development dependency groups to install. | List of strings | optional | `[]` |
| <a id="lock_import.import_uv-disallow_builds"></a>disallow_builds | If True, only pre-built wheels are allowed. | Boolean | optional | `False` |
| <a id="lock_import.import_uv-local_wheels"></a>local_wheels | A list of local .whl files to consider when processing lock files. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `[]` |
| <a id="lock_import.import_uv-lock_file"></a>lock_file | The pdm.lock file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="lock_import.import_uv-optional_groups"></a>optional_groups | List of optional dependency groups to install. | List of strings | optional | `[]` |
| <a id="lock_import.import_uv-project_file"></a>project_file | The pyproject.toml file. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="lock_import.import_uv-repo"></a>repo | The repository name | String | required | |
| <a id="lock_import.import_uv-require_static_urls"></a>require_static_urls | Require that the lock file is created with --static-urls. | Boolean | optional | `True` |
| <a id="lock_import.import_uv-target_environments"></a>target_environments | A list of target environment descriptors. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | `["@pycross_environments//:environments"]` |

<a id="lock_import.package"></a>

### package
Expand Down
26 changes: 26 additions & 0 deletions docs/workspace_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ lock_repo_model_poetry(<a href="#lock_repo_model_poetry-project_file">project_fi
| <a id="lock_repo_model_poetry-lock_file"></a>lock_file | <p align="center"> - </p> | none |


<a id="lock_repo_model_uv"></a>

## lock_repo_model_uv

<pre>
lock_repo_model_uv(<a href="#lock_repo_model_uv-project_file">project_file</a>, <a href="#lock_repo_model_uv-lock_file">lock_file</a>, <a href="#lock_repo_model_uv-default">default</a>, <a href="#lock_repo_model_uv-optional_groups">optional_groups</a>, <a href="#lock_repo_model_uv-all_optional_groups">all_optional_groups</a>,
<a href="#lock_repo_model_uv-development_groups">development_groups</a>, <a href="#lock_repo_model_uv-all_development_groups">all_development_groups</a>, <a href="#lock_repo_model_uv-require_static_urls">require_static_urls</a>)
</pre>



**PARAMETERS**


| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="lock_repo_model_uv-project_file"></a>project_file | <p align="center"> - </p> | none |
| <a id="lock_repo_model_uv-lock_file"></a>lock_file | <p align="center"> - </p> | none |
| <a id="lock_repo_model_uv-default"></a>default | <p align="center"> - </p> | `True` |
| <a id="lock_repo_model_uv-optional_groups"></a>optional_groups | <p align="center"> - </p> | `[]` |
| <a id="lock_repo_model_uv-all_optional_groups"></a>all_optional_groups | <p align="center"> - </p> | `False` |
| <a id="lock_repo_model_uv-development_groups"></a>development_groups | <p align="center"> - </p> | `[]` |
| <a id="lock_repo_model_uv-all_development_groups"></a>all_development_groups | <p align="center"> - </p> | `False` |
| <a id="lock_repo_model_uv-require_static_urls"></a>require_static_urls | <p align="center"> - </p> | `True` |


<a id="pycross_lock_repo"></a>

## pycross_lock_repo
Expand Down
6 changes: 4 additions & 2 deletions e2e/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ lock_import.import_pdm(
target_environments = ["@smoke_environments//:environments"],
)

# lock_repo with PDM and some package overrides
lock_import.import_pdm(
# lock_repo with UV and some package overrides
lock_import.import_uv(
default_alias_single_version = True,
local_wheels = [
"//:cowsay-6.1-py3-none-any.whl",
Expand All @@ -101,6 +101,8 @@ lock_import.import_pdm(
repo = "uv",
target_environments = ["@smoke_environments//:environments"],
)

# lock_repo with PDM and some package overrides
lock_import.package(
name = "regex",
always_build = True,
Expand Down
88 changes: 88 additions & 0 deletions e2e/bzlmod/uv/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_pycross//pycross:defs.bzl", "pycross_wheel_build")
load("@rules_python//python:defs.bzl", "py_test")
load("@uv//:requirements.bzl", "all_requirements", "all_whl_requirements", "requirement")

package(default_visibility = ["//visibility:public"])

pycross_wheel_build(
name = "zstandard_build",
config_settings = {
"--build-option": [
"--no-cffi-backend",
"--system-zstd",
],
},
copts = ["-Wl,-s"],
native_deps = [
"//third_party/zstd",
],
post_build_hooks = [
"@rules_pycross//pycross/hooks:repair_wheel",
],
sdist = "@uv//zstandard:sdist",
tags = ["manual"],
deps = [
"@uv//:setuptools",
"@uv//:wheel",
],
)

write_file(
name = "ipython_py",
out = "ipython.py",
content = [
"import os",
"import tempfile",
"from IPython import start_ipython",
"with tempfile.TemporaryDirectory() as d:",
" os.environ['IPYTHONDIR'] = str(d)",
" start_ipython()",
],
)

# Tests

py_test(
name = "test_library_usage_via_ipython",
srcs = [
"ipython.py",
"//:test_zstandard.py",
],
args = ["$(location //:test_zstandard.py)"],
main = "ipython.py",
deps = [
"@uv//:ipython",
"@uv//:zstandard",
],
)

py_test(
name = "test_zstandard",
srcs = ["//:test_zstandard.py"],
Expand All @@ -13,3 +68,36 @@ py_test(
srcs = ["//:test_regex.py"],
deps = ["@uv//:regex"],
)

py_test(
name = "test_cowsay",
srcs = ["//:test_cowsay.py"],
main = "test_cowsay.py",
deps = ["@uv//:cowsay"],
)

# Test using the `requirement` function
py_test(
name = "test_regex_using_requirement",
srcs = ["//:test_regex.py"],
main = "test_regex.py",
deps = [requirement("regex")],
)

# Test using `all_requirements`
py_test(
name = "test_regex_using_all_requirements",
srcs = ["//:test_regex.py"],
main = "test_regex.py",
deps = all_requirements,
)

# Test using `all_whl_requirements`
py_test(
name = "test_all_whl_requirements",
srcs = ["//:test_all_whl_requirements.py"],
env = {
"ALL_WHL_REQUIREMENTS": ",".join(all_whl_requirements),
},
main = "test_all_whl_requirements.py",
)
1 change: 1 addition & 0 deletions pycross/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ bzl_library(
"//pycross/private:pdm_lock_model",
"//pycross/private:poetry_lock_model",
"//pycross/private:toolchain_helpers",
"//pycross/private:uv_lock_model",
],
)

Expand Down
14 changes: 13 additions & 1 deletion pycross/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ bzl_library(
],
)

bzl_library(
name = "lock_model",
srcs = ["lock_model.bzl"],
deps = [":internal_repo"],
)

bzl_library(
name = "pdm_lock_model",
srcs = ["pdm_lock_model.bzl"],
deps = [":internal_repo"],
deps = [":lock_model"],
)

bzl_library(
name = "uv_lock_model",
srcs = ["uv_lock_model.bzl"],
deps = [":lock_model"],
)

bzl_library(
Expand Down
1 change: 1 addition & 0 deletions pycross/private/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ bzl_library(
"//pycross/private:poetry_lock_model",
"//pycross/private:pypi_file",
"//pycross/private:resolved_lock_repo",
"//pycross/private:uv_lock_model",
"@bazel_features//:features",
] + REPO_HTTP_DEPS,
)
Expand Down
12 changes: 10 additions & 2 deletions pycross/private/bzlmod/lock_import.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ load("//pycross/private:lock_attrs.bzl", "package_annotation")
load("//pycross/private:pdm_lock_model.bzl", "lock_repo_model_pdm")
load("//pycross/private:poetry_lock_model.bzl", "lock_repo_model_poetry")
load("//pycross/private:resolved_lock_repo.bzl", "resolved_lock_repo")
load("//pycross/private:uv_lock_model.bzl", "lock_repo_model_uv")
load(":lock_hub_repo.bzl", "lock_hub_repo")
load(":tag_attrs.bzl", "COMMON_ATTRS", "COMMON_IMPORT_ATTRS", "PACKAGE_ATTRS", "PDM_IMPORT_ATTRS", "POETRY_IMPORT_ATTRS")
load(":tag_attrs.bzl", "COMMON_ATTRS", "COMMON_IMPORT_ATTRS", "PACKAGE_ATTRS", "PDM_IMPORT_ATTRS", "POETRY_IMPORT_ATTRS", "UV_IMPORT_ATTRS")

def _generate_resolved_lock_repo(lock_info, serialized_lock_model):
repo_name = lock_info.repo_name
Expand Down Expand Up @@ -100,7 +101,7 @@ def _lock_import_impl(module_ctx):

# A first pass initialize lock structures and make sure none of the repo names are duplicated.
for module in module_ctx.modules:
for tag in module.tags.import_pdm + module.tags.import_poetry:
for tag in module.tags.import_pdm + module.tags.import_poetry + module.tags.import_uv:
_check_unique_lock_repo(lock_owners, module, tag)
lock_repos[tag.repo] = _lock_struct(module_ctx, tag)

Expand All @@ -110,6 +111,8 @@ def _lock_import_impl(module_ctx):
lock_model_structs[tag.repo] = lock_repo_model_pdm(**{attr: getattr(tag, attr) for attr in PDM_IMPORT_ATTRS})
for tag in module.tags.import_poetry:
lock_model_structs[tag.repo] = lock_repo_model_poetry(**{attr: getattr(tag, attr) for attr in POETRY_IMPORT_ATTRS})
for tag in module.tags.import_uv:
lock_model_structs[tag.repo] = lock_repo_model_uv(**{attr: getattr(tag, attr) for attr in UV_IMPORT_ATTRS})

# Add package attributes
for module in module_ctx.modules:
Expand Down Expand Up @@ -142,6 +145,10 @@ _import_poetry_tag = tag_class(
doc = "Import a Poetry lock file.",
attrs = POETRY_IMPORT_ATTRS | COMMON_IMPORT_ATTRS | COMMON_ATTRS,
)
_import_uv_tag = tag_class(
doc = "Import a uv lock file.",
attrs = UV_IMPORT_ATTRS | COMMON_IMPORT_ATTRS | COMMON_ATTRS,
)
_package_tag = tag_class(
doc = "Specify package-specific settings.",
attrs = PACKAGE_ATTRS | COMMON_ATTRS,
Expand All @@ -152,6 +159,7 @@ lock_import = module_extension(
tag_classes = dict(
import_pdm = _import_pdm_tag,
import_poetry = _import_poetry_tag,
import_uv = _import_uv_tag,
package = _package_tag,
),
)
2 changes: 2 additions & 0 deletions pycross/private/bzlmod/tag_attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load(
_PDM_IMPORT_ATTRS = "PDM_IMPORT_ATTRS",
_POETRY_IMPORT_ATTRS = "POETRY_IMPORT_ATTRS",
_REGISTER_TOOLCHAINS_ATTRS = "REGISTER_TOOLCHAINS_ATTRS",
_UV_IMPORT_ATTRS = "UV_IMPORT_ATTRS",
)

# Attrs common to all tags
Expand Down Expand Up @@ -63,5 +64,6 @@ PACKAGE_ATTRS = dict(
CREATE_ENVIRONMENTS_ATTRS = _CREATE_ENVIRONMENTS_ATTRS
CREATE_REPOS_ATTRS = _CREATE_REPOS_ATTRS
PDM_IMPORT_ATTRS = _PDM_IMPORT_ATTRS
UV_IMPORT_ATTRS = _UV_IMPORT_ATTRS
POETRY_IMPORT_ATTRS = _POETRY_IMPORT_ATTRS
REGISTER_TOOLCHAINS_ATTRS = _REGISTER_TOOLCHAINS_ATTRS
5 changes: 4 additions & 1 deletion pycross/private/lock_attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ RENDER_ATTRS = dict(
),
) | CREATE_REPOS_ATTRS

PDM_IMPORT_ATTRS = dict(
_IMPORT_ATTRS = dict(
lock_file = attr.label(
doc = "The pdm.lock file.",
allow_single_file = True,
Expand Down Expand Up @@ -250,3 +250,6 @@ def package_annotation(
ignore_dependencies = ignore_dependencies,
install_exclude_globs = install_exclude_globs,
))

PDM_IMPORT_ATTRS = _IMPORT_ATTRS
UV_IMPORT_ATTRS = _IMPORT_ATTRS
Loading

0 comments on commit 06e6098

Please sign in to comment.