Skip to content

Commit

Permalink
Allows usage of UV, CPython and pypy mirrors
Browse files Browse the repository at this point in the history
This enables enterprise use-cases:
   - Allows to use your own fork
   - Allow to target your own artifactory mirror (ex: https://jfrog.com/help/r/jfrog-artifactory-documentation/configure-vcs-repository-layout)
  • Loading branch information
Coruscant11 committed Jul 26, 2024
1 parent 2980970 commit b0802fa
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 121 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ codegen-units = 1
[package.metadata.cross.build.env]
passthrough = [
"PYAPP_ALLOW_UPDATES",
"PYAPP_DISTRIBUTION_CPYTHON_REPOSITORY",
"PYAPP_DISTRIBUTION_EMBED",
"PYAPP_DISTRIBUTION_FORMAT",
"PYAPP_DISTRIBUTION_PATH",
"PYAPP_DISTRIBUTION_PATH_PREFIX",
"PYAPP_DISTRIBUTION_PIP_AVAILABLE",
"PYAPP_DISTRIBUTION_PYPY_HOST",
"PYAPP_DISTRIBUTION_PYTHON_PATH",
"PYAPP_DISTRIBUTION_SITE_PACKAGES_PATH",
"PYAPP_DISTRIBUTION_SOURCE",
Expand Down Expand Up @@ -81,6 +83,7 @@ passthrough = [
"PYAPP_SKIP_INSTALL",
"PYAPP_UPGRADE_VIRTUALENV",
"PYAPP_UV_ENABLED",
"PYAPP_UV_REPOSITORY",
"PYAPP_UV_ONLY_BOOTSTRAP",
"PYAPP_UV_VERSION",
]
249 changes: 141 additions & 108 deletions build.rs

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion docs/config/distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ Setting the `PYAPP_PYTHON_VERSION` option will determine the distribution used a
| `3.11` |
| `3.12` |

The source for pre-built distributions is the [python-build-standalone](https://github.com/indygreg/python-build-standalone) project.
The source for pre-built distributions is the [python-build-standalone](https://github.com/indygreg/python-build-standalone) repository.
Distributions are downloaded from the release section.

Some distributions have [variants](https://gregoryszorc.com/docs/python-build-standalone/main/running.html) that may be configured with the `PYAPP_DISTRIBUTION_VARIANT` option:

| Platform | Options |
| --- | --- |
| Linux | <ul><li><code>v1</code></li><li><code>v2</code></li><li><code>v3</code> (default)</li><li><code>v4</code></li></ul> |

If you want to change the repository URL in order to targer your own GitHub fork/mirror, you can set the `PYAPP_DISTRIBUTION_CPYTHON_REPOSITORY` option.

### PyPy

| ID |
Expand All @@ -34,6 +37,7 @@ Some distributions have [variants](https://gregoryszorc.com/docs/python-build-st
| `pypy3.10` |

The source of distributions is the [PyPy](https://www.pypy.org) project.
If you want to change the host in order target your own fork/mirror, you can set the `PYAPP_DISTRIBUTION_PYPY_HOST` option.

## Custom

Expand Down
6 changes: 6 additions & 0 deletions docs/config/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ These options have no effect when the project installation is [disabled](#skippi

You may set the `PYAPP_UV_ENABLED` option to `true` or `1` to use [UV](https://github.com/astral-sh/uv) for virtual environment creation and project installation.

Executable is downloaded from the release section of the repository.

### Custom repository

You may use your own GitHub fork/mirror of [UV repository](https://github.com/astral-sh/uv) by setting the `PYAPP_UV_REPOSITORY` option.

### Version ### {: #uv-version }

You may use a specific `X.Y.Z` version by setting the `PYAPP_UV_VERSION` option.
Expand Down
4 changes: 4 additions & 0 deletions docs/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ The following is not intended to be a complete enumeration. Be sure to view the
| [Litestar](https://github.com/litestar-org/litestar-fullstack/blob/dc72eee78173790c3e91b0c095ac9e70ba91bedd/scripts/post-builds.py)
| [Preservation Workbench](https://github.com/Preservation-Workbench/PWCode/blob/e7777806be35bd60ca8c33e677ffd77e38b277d0/build/make.sh)
| [tidal-wave](https://github.com/ebb-earl-co/tidal-wave/blob/6358ede21adb715a053b1e6cc73968933c3bed05/BUILDME.md#pyapp-created-binaries)

## Industry

- [Amadeus](https://amadeus.com) <sup>\[[1](https://github.com/ofek/pyapp/pull/147)|[2](https://github.com/AmadeusITGroup/pyapp)\]</sup>
24 changes: 24 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ pub fn uv_only_bootstrap() -> bool {
env!("PYAPP_UV_ONLY_BOOTSTRAP") == "1"
}

pub fn uv_repository() -> String {
env!("PYAPP_UV_REPOSITORY")
.to_string()
.trim_end_matches('/')
.into()
}

pub fn uv_version() -> String {
env!("PYAPP_UV_VERSION").into()
}
Expand Down Expand Up @@ -297,3 +304,20 @@ pub fn installer_lock(name: &str, id: &str) -> PathBuf {
.join("locks")
.join(format!("installer-{}-{}", name, id))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn distribution_source_is_python_build_standalone_repository_by_default() {
let source = distribution_source();
assert!(source
.starts_with("https://github.com/indygreg/python-build-standalone/releases/download/"));
}

#[test]
fn uv_repository_is_official_one_by_default() {
assert_eq!("https://github.com/astral-sh/uv", uv_repository());
}
}
50 changes: 38 additions & 12 deletions src/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,22 @@ pub fn ensure_installer_available() -> Result<()> {
Ok(())
}

fn build_uv_download_url(version: &str, artifact_name: &str) -> String {
let repository = app::uv_repository();

if version == "any" {
format!(
"{}/releases/latest/download/{}",
&repository, &artifact_name,
)
} else {
format!(
"{}/releases/download/{}/{}",
&repository, &version, &artifact_name,
)
}
}

fn ensure_uv_available() -> Result<()> {
let uv_version = app::uv_version();
let lock_path = app::installer_lock("uv", &uv_version);
Expand All @@ -494,18 +510,7 @@ fn ensure_uv_available() -> Result<()> {
let mut f = fs::File::create(&temp_path)
.with_context(|| format!("unable to create temporary file: {}", &temp_path.display()))?;

let url = if uv_version == "any" {
format!(
"https://github.com/astral-sh/uv/releases/latest/download/{}",
&artifact_name,
)
} else {
format!(
"https://github.com/astral-sh/uv/releases/download/{}/{}",
&uv_version, &artifact_name,
)
};

let url = build_uv_download_url(&uv_version, &artifact_name);
network::download(&url, &mut f, "UV")?;

if artifact_name.ends_with(".zip") {
Expand Down Expand Up @@ -568,3 +573,24 @@ fn apply_project_features(install_target: String) -> String {
format!("{install_target}[{}]", app::pip_project_features())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn download_specific_uv_version_from_official_repository_by_default() {
assert_eq!(
"https://github.com/astral-sh/uv/releases/download/my_version/my_artifact",
build_uv_download_url("my_version", "my_artifact")
);
}

#[test]
fn download_latest_uv_version_from_official_repository_by_default() {
assert_eq!(
"https://github.com/astral-sh/uv/releases/latest/download/my_artifact",
build_uv_download_url("any", "my_artifact")
);
}
}

0 comments on commit b0802fa

Please sign in to comment.