Skip to content
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

refactor: update pip if extern managed and global.break-system-packages is true #634

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions src/steps/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,20 +425,53 @@ pub fn run_pip3_update(ctx: &ExecutionContext) -> Result<()> {
.output_checked_utf8()
.map_err(|_| SkipStep("pip does not exist".to_string()))?;

let check_externally_managed = "import sysconfig; from os import path; print('Y') if path.isfile(path.join(sysconfig.get_path('stdlib'), 'EXTERNALLY-MANAGED')) else print('N')";
Command::new(&python3)
.args(["-c", check_externally_managed])
let check_extern_managed_script = "import sysconfig; from os import path; print('Y') if path.isfile(path.join(sysconfig.get_path('stdlib'), 'EXTERNALLY-MANAGED')) else print('N')";
let output = Command::new(&python3)
.args(["-c", check_extern_managed_script])
.output_checked_utf8()?;
let stdout = output.stdout.trim();
let extern_managed = match stdout {
"N" => false,
"Y" => true,
_ => unreachable!("unexpected output from `check_extern_managed_script`"),
};

let allow_break_sys_pkg = match Command::new(&python3)
.args(["-m", "pip", "config", "get", "global.break-system-packages"])
.output_checked_utf8()
.map_err(|_| SkipStep("pip may be externally managed".to_string()))
.and_then(|output| match output.stdout.trim() {
"N" => Ok(()),
"Y" => Err(SkipStep("pip is externally managed".to_string())),
_ => {
print_warning("Unexpected output when checking EXTERNALLY-MANAGED");
print_warning(output.stdout.trim());
Err(SkipStep("pip may be externally managed".to_string()))
}
})?;
{
Ok(output) => {
let stdout = output.stdout.trim();
stdout
.parse::<bool>()
.expect("unexpected output that is not `true` or `false`")
}
// it can fail because this key may not be set
//
// ```sh
// $ pip --version
// pip 23.0.1 from /usr/lib/python3/dist-packages/pip (python 3.11)
//
// $ pip config get global.break-system-packages
// ERROR: No such key - global.break-system-packages
//
// $ echo $?
// 1
// ```
Err(_) => false,
};

debug!("pip3 externally managed: {} ", extern_managed);
debug!("pip3 global.break-system-packages: {}", allow_break_sys_pkg);

// Even though pip3 is externally managed, we should still update it if
// `global.break-system-packages` is true.
if extern_managed && !allow_break_sys_pkg {
return Err(SkipStep(
"Skip pip3 update as it is externally managed and global.break-system-packages is not true".to_string(),
)
.into());
}

print_separator("pip3");
if env::var("VIRTUAL_ENV").is_ok() {
Expand Down