Better Python detection (fix #187)

This commit is contained in:
Roey Darwish Dror
2019-08-14 12:38:22 +03:00
parent 7cfdc69b63
commit 7ac46d311e
2 changed files with 21 additions and 3 deletions

View File

@@ -78,7 +78,7 @@ fn run() -> Result<(), Error> {
let mut builder = formatted_timed_builder();
if config.verbose() {
builder.filter(Some("topgrade"), LevelFilter::Debug);
builder.filter(Some("topgrade"), LevelFilter::Trace);
}
builder.init();

View File

@@ -4,8 +4,11 @@ use crate::terminal::{print_separator, print_warning};
use crate::utils::{require, require_option, which};
use failure::ResultExt;
use ini::Ini;
use log::{debug, error};
use serde::Deserialize;
use std::path::PathBuf;
use std::io;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use walkdir::WalkDir;
static OS_RELEASE_PATH: &str = "/etc/os-release";
@@ -29,6 +32,16 @@ pub enum Distribution {
Solus,
}
/// Returns whether the given Python path is the system Python in Arch Linux
fn arch_system_python(path: &Path) -> Result<bool, io::Error> {
debug!("Python is in {}", path.display());
Command::new("pacman").arg("-Qqo").arg(&path).output().map(|output| {
debug!("Pacman output: {:?}", output);
let Output { status, stdout, .. } = output;
status.success() && String::from_utf8(stdout).unwrap() == "python\n"
})
}
impl Distribution {
fn parse_os_release(os_release: &ini::Ini) -> Result<Self, Error> {
let section = os_release.general_section();
@@ -111,7 +124,12 @@ fn upgrade_arch_linux(sudo: &Option<PathBuf>, cleanup: bool, run_type: RunType)
if let Some(yay) = which("yay") {
if let Some(python) = which("python") {
if python != PathBuf::from("/usr/bin/python") {
let is_system_python = arch_system_python(&python).unwrap_or_else(|e| {
error!("Error detecting system Python: {}", e);
false
});
if !is_system_python {
print_warning(format!(
"Python detected at {:?}, which is probably not the system Python.
It's dangerous to run yay since Python based AUR packages will be installed in the wrong location",