-
Notifications
You must be signed in to change notification settings - Fork 732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add build backend scaffolding #7662
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use crate::commands::ExitStatus; | ||
use anyhow::Result; | ||
use std::path::Path; | ||
|
||
pub(crate) fn build_sdist(_sdist_directory: &Path) -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn build_wheel( | ||
_wheel_directory: &Path, | ||
_metadata_directory: Option<&Path>, | ||
) -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn build_editable( | ||
_wheel_directory: &Path, | ||
_metadata_directory: Option<&Path>, | ||
) -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn get_requires_for_build_sdist() -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn get_requires_for_build_wheel() -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn prepare_metadata_for_build_wheel(_wheel_directory: &Path) -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn get_requires_for_build_editable() -> Result<ExitStatus> { | ||
todo!() | ||
} | ||
|
||
pub(crate) fn prepare_metadata_for_build_editable(_wheel_directory: &Path) -> Result<ExitStatus> { | ||
todo!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
import sysconfig | ||
|
||
|
||
def find_uv_bin() -> str: | ||
"""Return the uv binary path.""" | ||
|
||
uv_exe = "uv" + sysconfig.get_config_var("EXE") | ||
|
||
path = os.path.join(sysconfig.get_path("scripts"), uv_exe) | ||
if os.path.isfile(path): | ||
return path | ||
|
||
if sys.version_info >= (3, 10): | ||
user_scheme = sysconfig.get_preferred_scheme("user") | ||
elif os.name == "nt": | ||
user_scheme = "nt_user" | ||
elif sys.platform == "darwin" and sys._framework: | ||
user_scheme = "osx_framework_user" | ||
else: | ||
user_scheme = "posix_user" | ||
|
||
path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), uv_exe) | ||
if os.path.isfile(path): | ||
return path | ||
|
||
# Search in `bin` adjacent to package root (as created by `pip install --target`). | ||
pkg_root = os.path.dirname(os.path.dirname(__file__)) | ||
target_path = os.path.join(pkg_root, "bin", uv_exe) | ||
if os.path.isfile(target_path): | ||
return target_path | ||
|
||
raise FileNotFoundError(path) | ||
|
||
|
||
__all__ = [ | ||
"find_uv_bin", | ||
] | ||
if os.environ.get("UV_PREVIEW"): | ||
from ._build_backend import * | ||
from ._find_uv import find_uv_bin | ||
|
||
if os.environ.get("UV_PREVIEW"): | ||
__all__ = [ | ||
"find_uv_bin", | ||
# PEP 517 hook `build_sdist`. | ||
"build_sdist", | ||
# PEP 517 hook `build_wheel`. | ||
"build_wheel", | ||
# PEP 660 hook `build_editable`. | ||
"build_editable", | ||
# PEP 517 hook `get_requires_for_build_sdist`. | ||
"get_requires_for_build_sdist", | ||
# PEP 517 hook `get_requires_for_build_wheel`. | ||
"get_requires_for_build_wheel", | ||
# PEP 517 hook `prepare_metadata_for_build_wheel`. | ||
"prepare_metadata_for_build_wheel", | ||
# PEP 660 hook `get_requires_for_build_editable`. | ||
"get_requires_for_build_editable", | ||
# PEP 660 hook `prepare_metadata_for_build_editable`. | ||
"prepare_metadata_for_build_editable", | ||
] | ||
else: | ||
__all__ = ["find_uv_bin"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Python shims for the PEP 517 and PEP 660 build backend. | ||
|
||
Major imports in this module are required to be lazy: | ||
``` | ||
$ hyperfine \ | ||
"/usr/bin/python3 -c \"print('hi')\"" \ | ||
"/usr/bin/python3 -c \"from subprocess import check_call; print('hi')\"" | ||
Base: Time (mean ± σ): 11.0 ms ± 1.7 ms [User: 8.5 ms, System: 2.5 ms] | ||
With import: Time (mean ± σ): 15.2 ms ± 2.0 ms [User: 12.3 ms, System: 2.9 ms] | ||
Base 1.38 ± 0.28 times faster than with import | ||
``` | ||
|
||
The same thing goes for the typing module, so we use Python 3.10 type annotations that | ||
don't require importing typing but then quote them so earlier Python version ignore | ||
them while IDEs and type checker can see through the quotes. | ||
""" | ||
|
||
|
||
def warn_config_settings(config_settings: "dict | None" = None): | ||
import sys | ||
|
||
if config_settings: | ||
print("Warning: Config settings are not supported", file=sys.stderr) | ||
|
||
|
||
def call(args: "list[str]", config_settings: "dict | None" = None) -> str: | ||
"""Invoke a uv subprocess and return the filename from stdout.""" | ||
import subprocess | ||
import sys | ||
|
||
from ._find_uv import find_uv_bin | ||
|
||
warn_config_settings(config_settings) | ||
# Forward stderr, capture stdout for the filename | ||
result = subprocess.run([find_uv_bin()] + args, stdout=subprocess.PIPE) | ||
if result.returncode != 0: | ||
sys.exit(result.returncode) | ||
# If there was extra stdout, forward it (there should not be extra stdout) | ||
result = result.stdout.decode("utf-8").strip().splitlines(keepends=True) | ||
sys.stdout.writelines(result[:-1]) | ||
# Fail explicitly instead of an irrelevant stacktrace | ||
if not result: | ||
print("uv subprocess did not return a filename on stdout", file=sys.stderr) | ||
sys.exit(1) | ||
return result[-1].strip() | ||
|
||
|
||
def build_sdist(sdist_directory: str, config_settings: "dict | None" = None): | ||
"""PEP 517 hook `build_sdist`.""" | ||
args = ["build-backend", "build-sdist", sdist_directory] | ||
return call(args, config_settings) | ||
|
||
|
||
def build_wheel( | ||
wheel_directory: str, | ||
config_settings: "dict | None" = None, | ||
metadata_directory: "str | None" = None, | ||
): | ||
"""PEP 517 hook `build_wheel`.""" | ||
args = ["build-backend", "build-wheel", wheel_directory] | ||
if metadata_directory: | ||
args.extend(["--metadata-directory", metadata_directory]) | ||
return call(args, config_settings) | ||
|
||
|
||
def get_requires_for_build_sdist(config_settings: "dict | None" = None): | ||
"""PEP 517 hook `get_requires_for_build_sdist`.""" | ||
warn_config_settings(config_settings) | ||
return [] | ||
|
||
|
||
def get_requires_for_build_wheel(config_settings: "dict | None" = None): | ||
"""PEP 517 hook `get_requires_for_build_wheel`.""" | ||
warn_config_settings(config_settings) | ||
return [] | ||
|
||
|
||
def prepare_metadata_for_build_wheel( | ||
metadata_directory: str, config_settings: "dict | None" = None | ||
): | ||
"""PEP 517 hook `prepare_metadata_for_build_wheel`.""" | ||
args = ["build-backend", "prepare-metadata-for-build-wheel", metadata_directory] | ||
return call(args, config_settings) | ||
|
||
|
||
def build_editable( | ||
wheel_directory: str, | ||
config_settings: "dict | None" = None, | ||
metadata_directory: "str | None" = None, | ||
): | ||
"""PEP 660 hook `build_editable`.""" | ||
args = ["build-backend", "build-editable", wheel_directory] | ||
if metadata_directory: | ||
args.extend(["--metadata-directory", metadata_directory]) | ||
return call(args, config_settings) | ||
|
||
|
||
def get_requires_for_build_editable(config_settings: "dict | None" = None): | ||
"""PEP 660 hook `get_requires_for_build_editable`.""" | ||
warn_config_settings(config_settings) | ||
return [] | ||
|
||
|
||
def prepare_metadata_for_build_editable( | ||
metadata_directory: str, config_settings: "dict | None" = None | ||
): | ||
"""PEP 660 hook `prepare_metadata_for_build_editable`.""" | ||
args = ["build-backend", "prepare-metadata-for-build-editable", metadata_directory] | ||
return call(args, config_settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
import sysconfig | ||
|
||
|
||
def find_uv_bin() -> str: | ||
"""Return the uv binary path.""" | ||
|
||
uv_exe = "uv" + sysconfig.get_config_var("EXE") | ||
|
||
path = os.path.join(sysconfig.get_path("scripts"), uv_exe) | ||
if os.path.isfile(path): | ||
return path | ||
|
||
if sys.version_info >= (3, 10): | ||
user_scheme = sysconfig.get_preferred_scheme("user") | ||
elif os.name == "nt": | ||
user_scheme = "nt_user" | ||
elif sys.platform == "darwin" and sys._framework: | ||
user_scheme = "osx_framework_user" | ||
else: | ||
user_scheme = "posix_user" | ||
|
||
path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), uv_exe) | ||
if os.path.isfile(path): | ||
return path | ||
|
||
# Search in `bin` adjacent to package root (as created by `pip install --target`). | ||
pkg_root = os.path.dirname(os.path.dirname(__file__)) | ||
target_path = os.path.join(pkg_root, "bin", uv_exe) | ||
if os.path.isfile(target_path): | ||
return target_path | ||
|
||
raise FileNotFoundError(path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# uv_backend | ||
|
||
A simple package to be built with the uv build backend. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to include this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've had many cases where something was done due to perfomance reasons but there was no way for me to find out if those still apply.
If Python makes improvement in the future to import times (something that's highly requested, so it's not unlikely to happen), we should document how to check them. It's also good to have some real numbers so we know what we're talking about and whether that matters. In this case, it's 40% or 4ms, the latter is too much for just invoking uv (it's about the time we need to create a venv last time it checked). I feel strongly that when we make unintuitive decisions for performance, we should have the impact both documented and measurable.