Skip to content

Commit

Permalink
Add PYAPP_DISTRIBUTION_PATH_PREFIX option (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored May 15, 2024
1 parent b58e45a commit 88e70e5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ passthrough = [
"PYAPP_DISTRIBUTION_EMBED",
"PYAPP_DISTRIBUTION_FORMAT",
"PYAPP_DISTRIBUTION_PATH",
"PYAPP_DISTRIBUTION_PATH_PREFIX",
"PYAPP_DISTRIBUTION_PIP_AVAILABLE",
"PYAPP_DISTRIBUTION_PYTHON_PATH",
"PYAPP_DISTRIBUTION_SITE_PACKAGES_PATH",
Expand Down
35 changes: 31 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,23 @@ fn normalize_project_name(name: &String) -> String {
.to_lowercase()
}

fn normalize_relative_path(path: &String) -> String {
if env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
path.replace('/', "\\")
.strip_prefix('\\')
.unwrap_or(path)
.strip_suffix('\\')
.unwrap_or(path)
.to_string()
} else {
path.strip_prefix('/')
.unwrap_or(path)
.strip_suffix('/')
.unwrap_or(path)
.to_string()
}
}

fn embed_file(name: &str) -> PathBuf {
[
env::var("CARGO_MANIFEST_DIR").unwrap().as_str(),
Expand Down Expand Up @@ -572,8 +589,8 @@ fn set_python_path(distribution_source: &str) {
let distribution_variable = "PYAPP_DISTRIBUTION_PYTHON_PATH";
let on_windows = env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows";
let python_path = env::var(distribution_variable).unwrap_or_default();
let relative_path = if !python_path.is_empty() {
python_path
let mut relative_path = if !python_path.is_empty() {
normalize_relative_path(&python_path)
} else if !env::var("PYAPP_DISTRIBUTION_PATH")
.unwrap_or_default()
.is_empty()
Expand Down Expand Up @@ -608,6 +625,11 @@ fn set_python_path(distribution_source: &str) {
} else {
"bin/python3".to_string()
};

let path_prefix = env::var("PYAPP_DISTRIBUTION_PATH_PREFIX").unwrap_or_default();
if !path_prefix.is_empty() {
relative_path = normalize_relative_path(&path_prefix);
}
set_runtime_variable(distribution_variable, &relative_path);

let installation_variable = "PYAPP__INSTALLATION_PYTHON_PATH";
Expand All @@ -625,8 +647,8 @@ fn set_site_packages_path(distribution_source: &str) {
let on_windows = env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows";
let python_version = get_python_version();
let site_packages_path = env::var(distribution_variable).unwrap_or_default();
let relative_path = if !site_packages_path.is_empty() {
site_packages_path
let mut relative_path = if !site_packages_path.is_empty() {
normalize_relative_path(&site_packages_path)
} else if distribution_source.starts_with(DEFAULT_CPYTHON_SOURCE) {
if python_version == "3.7" {
if on_windows {
Expand Down Expand Up @@ -662,6 +684,11 @@ fn set_site_packages_path(distribution_source: &str) {
} else {
format!("lib/python{}/site-packages", python_version)
};

let path_prefix = env::var("PYAPP_DISTRIBUTION_PATH_PREFIX").unwrap_or_default();
if !path_prefix.is_empty() {
relative_path = normalize_relative_path(&path_prefix);
}
set_runtime_variable(distribution_variable, &relative_path);

let installation_variable = "PYAPP__INSTALLATION_SITE_PACKAGES_PATH";
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

***Added:***

- Add `PYAPP_DISTRIBUTION_PATH_PREFIX` option for easier configuring of custom distribution internal paths
- Add `PYAPP_ALLOW_UPDATES` option for enabling the `update` management command when project installation is skipped

***Fixed:***
Expand Down
4 changes: 4 additions & 0 deletions docs/config/distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ You may set the relative path to the Python executable after unpacking the archi

You may set the relative path to the [`site-packages`](https://docs.python.org/3/library/site.html) directory after unpacking the archive with the `PYAPP_DISTRIBUTION_SITE_PACKAGES_PATH` option. The default is `Lib\site-packages` on Windows and `lib/python<ID>/site-packages` on all other platforms where `<ID>` is the defined [distribution ID](#known).

### Path prefix

If the [Python executable](#python-location) and the [`site-packages` directory](#site-packages-location) are at the default locations but nested under top-level directories, you may set the `PYAPP_DISTRIBUTION_PATH_PREFIX` option to the common prefix of the two paths to avoid having to manually set those options.

### pip availability

You may indicate whether pip is already installed by setting the `PYAPP_DISTRIBUTION_PIP_AVAILABLE` option to `true` or `1`. This elides the check for installation when [upgraded virtual environments](installation.md#virtual-environments) are enabled.
Expand Down

0 comments on commit 88e70e5

Please sign in to comment.