Skip to content

Commit

Permalink
Make system-site-packages project level
Browse files Browse the repository at this point in the history
  • Loading branch information
ischaojie committed Mar 26, 2024
1 parent 7200a5b commit 0c9523f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 44 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ _Unreleased_

- Always create `.gitignore` file in `rye init`. #919

- Added new `rye.tool.system-site-packages` config, so that the virtualenv
can be created with access to the system site packages. #925

<!-- released start -->

## 0.31.0
Expand Down
6 changes: 4 additions & 2 deletions rye/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::piptools::LATEST_PIP;
use crate::platform::{
get_app_dir, get_canonical_py_path, get_toolchain_python_bin, list_known_toolchains,
};
use crate::pyproject::latest_available_python_version;
use crate::pyproject::{latest_available_python_version, PyProject};
use crate::sources::py::{get_download_url, PythonVersion, PythonVersionRequest};
use crate::utils::{check_checksum, symlink_file, unpack_archive, CommandOutput, IoPathContext};
use crate::uv::UvBuilder;
Expand Down Expand Up @@ -142,6 +142,8 @@ pub fn ensure_self_venv_with_toolchain(

let py_bin = get_toolchain_python_bin(&version)?;

let pyproject = PyProject::discover()?;

// linux specific detection of shared libraries.
#[cfg(target_os = "linux")]
{
Expand All @@ -155,7 +157,7 @@ pub fn ensure_self_venv_with_toolchain(
&py_bin,
&version,
None,
Config::current().venv_system_site_packages(),
pyproject.system_site_packages(),
)?;
// write our marker
uv_venv.write_marker()?;
Expand Down
3 changes: 2 additions & 1 deletion rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ fn resolve_requirements_with_uv(
let venv_path = pyproject_toml.venv_path();
let py_bin = get_venv_python_bin(&venv_path);
let sources = ExpandedSources::from_sources(&pyproject_toml.sources()?)?;
let pyproject = PyProject::discover()?;

let uv = UvBuilder::new()
.with_output(output.quieter())
Expand All @@ -462,7 +463,7 @@ fn resolve_requirements_with_uv(
&py_bin,
py_ver,
None,
Config::current().venv_system_site_packages(),
pyproject.system_site_packages(),
)?;

for req in requirements {
Expand Down
2 changes: 1 addition & 1 deletion rye/src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
&python,
&project.venv_python_version()?,
None,
Config::current().venv_system_site_packages(),
project.system_site_packages(),
)?
.freeze()?;
} else {
Expand Down
16 changes: 0 additions & 16 deletions rye/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,6 @@ impl Config {
.unwrap_or(true)
}

/// Make the `.venv` to use the system site packages
pub fn venv_system_site_packages(&self) -> bool {
self.doc
.get("behavior")
.and_then(|x| x.get("venv-system-site-packages"))
.and_then(|x| x.as_bool())
.unwrap_or(false)
}

/// Returns the HTTP proxy that should be used.
pub fn http_proxy_url(&self) -> Option<String> {
std::env::var("http_proxy").ok().or_else(|| {
Expand Down Expand Up @@ -388,13 +379,6 @@ author = "John Doe <[email protected]>""#,
assert!(!cfg.venv_mark_sync_ignore());
}

#[test]
fn test_venv_system_site_packages() {
let (cfg_path, _temp_dir) = setup_config("[behavior]\nvenv-system-site-packages = true");
let cfg = Config::from_path(&cfg_path).expect("Failed to load config");
assert!(cfg.venv_system_site_packages());
}

#[test]
fn test_http_proxy_url() {
let (cfg_path, _temp_dir) = setup_config("[proxy]\nhttp = 'http://proxy.example.com'");
Expand Down
7 changes: 4 additions & 3 deletions rye/src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::bootstrap::{ensure_self_venv, fetch, FetchOptions};
use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::platform::get_app_dir;
use crate::pyproject::{normalize_package_name, read_venv_marker, ExpandedSources};
use crate::pyproject::{normalize_package_name, read_venv_marker, ExpandedSources, PyProject};
use crate::sources::py::PythonVersionRequest;
use crate::sync::{create_virtualenv, VenvMarker};
use crate::utils::{
Expand Down Expand Up @@ -110,6 +110,7 @@ pub fn install(
extra_requirements: &[Requirement],
output: CommandOutput,
) -> Result<(), Error> {
let pyproject = PyProject::discover()?;
let config = Config::current();
let sources = ExpandedSources::from_sources(&config.sources()?)?;
let app_dir = get_app_dir();
Expand Down Expand Up @@ -139,7 +140,7 @@ pub fn install(
&py_ver,
&target_venv_path,
requirement.name.as_str(),
config.venv_system_site_packages(),
pyproject.system_site_packages(),
)?;

if config.use_uv() {
Expand All @@ -152,7 +153,7 @@ pub fn install(
&py,
&py_ver,
None,
config.venv_system_site_packages(),
pyproject.system_site_packages(),
)?
.with_output(output)
.install(
Expand Down
10 changes: 1 addition & 9 deletions rye/src/piptools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::process::Command;
use anyhow::{bail, Context, Error};

use crate::bootstrap::ensure_self_venv;
use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::platform::get_app_dir;
use crate::sources::py::PythonVersion;
Expand Down Expand Up @@ -56,14 +55,7 @@ fn get_pip_tools_bin(py_ver: &PythonVersion, output: CommandOutput) -> Result<Pa
}

echo!(if output, "Creating virtualenv for pip-tools");
create_virtualenv(
output,
&self_venv,
py_ver,
&venv,
"pip-tools",
Config::current().venv_system_site_packages(),
)?;
create_virtualenv(output, &self_venv, py_ver, &venv, "pip-tools", false)?;

let mut cmd = Command::new(self_venv.join(VENV_BIN).join("pip"));
cmd.arg("--python")
Expand Down
21 changes: 21 additions & 0 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ impl Workspace {
pub fn lock_with_sources(&self) -> bool {
lock_with_sources(&self.doc)
}

/// Include system site packages when creating virtualenvs?
pub fn system_site_packages(&self) -> bool {
system_site_packages(&self.doc)
}
}

/// Check if recurse should be skipped into directory with this name
Expand Down Expand Up @@ -1005,6 +1010,14 @@ impl PyProject {
}
}

/// Include system site packages when creating virtualenvs?
pub fn system_site_packages(&self) -> bool {
match self.workspace {
Some(ref workspace) => workspace.system_site_packages(),
None => system_site_packages(&self.doc),
}
}

/// Save back changes
pub fn save(&self) -> Result<(), Error> {
let path = self.toml_path();
Expand Down Expand Up @@ -1279,6 +1292,14 @@ fn lock_with_sources(doc: &Document) -> bool {
.unwrap_or(false)
}

fn system_site_packages(doc: &Document) -> bool {
doc.get("tool")
.and_then(|x| x.get("rye"))
.and_then(|x| x.get("system-site-packages"))
.and_then(|x| x.as_bool())
.unwrap_or(false)
}

fn get_project_metadata(path: &Path) -> Result<Metadata, Error> {
let self_venv = ensure_self_venv(CommandOutput::Normal)?;
let mut metadata = Command::new(self_venv.join(VENV_BIN).join("python"));
Expand Down
5 changes: 2 additions & 3 deletions rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
let venv = pyproject.venv_path();
let py_ver = pyproject.venv_python_version()?;
let output = cmd.output;
let config = Config::current();

if cmd.pyproject.is_some()
&& cmd.mode != SyncMode::PythonOnly
Expand Down Expand Up @@ -176,7 +175,7 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
&py_ver,
&venv,
prompt,
config.venv_system_site_packages(),
pyproject.system_site_packages(),
)
.context("failed creating virtualenv ahead of sync")?;
}
Expand Down Expand Up @@ -264,7 +263,7 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
&py_path,
&py_ver,
None,
config.venv_system_site_packages(),
pyproject.system_site_packages(),
)?
.with_output(output)
.sync(&target_lockfile)?;
Expand Down
14 changes: 5 additions & 9 deletions rye/tests/test_sync.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fs;

use insta::{assert_snapshot, Settings};

use crate::common::{rye_cmd_snapshot, Space};
use insta::{assert_snapshot, Settings};
use toml_edit::value;

mod common;

Expand Down Expand Up @@ -266,13 +266,9 @@ fn test_autosync_remember() {
fn test_sync_include_system_site_packages() {
let space = Space::new();
space.init("my-project");
space
.rye_cmd()
.arg("config")
.arg("--set-bool")
.arg("behavior.venv-system-site-packages=true")
.status()
.expect("ok");
space.edit_toml("pyproject.toml", |doc| {
doc["tool"]["rye"]["system-site-packages"] = value(true);
});

space.rye_cmd().arg("sync").status().expect("ok");

Expand Down

0 comments on commit 0c9523f

Please sign in to comment.