Repository: astral-sh/ruff
File: crates/ty_site_packages/src/lib.rs (Rust)

Code before:

    if kind != CondaEnvironmentKind::from_prefix_path(system, &path) {
        return None;
    }

    Some(path)
}

#[cfg(not(target_family = "wasm"))]
pub(crate) fn binary_from_path(
    system: &dyn System,
    binary: &str,
    paths: &str,
) -> Option<SystemPathBuf> {
    let binary = which::which_in_global(binary, Some(&paths)).ok()?.next()?;
    let binary = SystemPathBuf::from_path_buf(binary).ok()?;

    // sanity check to filter out shims
    let sys_prefix = SysPrefixPath::new(&binary, SysPrefixPathOrigin::PythonBinary, system).ok()?;
    site_packages_directories_from_sys_prefix(
        &sys_prefix,
        None,
        PythonImplementation::Unknown,
        system,
    )
    .ok()?;

    Some(binary)
}

/// A parser for `pyvenv.cfg` files: metadata files for virtual environments.
///
/// Note that a `pyvenv.cfg` file *looks* like a `.ini` file, but actually isn't valid `.ini` syntax!
///
/// See also: <https://snarky.ca/how-virtual-environments-work/>
#[derive(Debug)]
struct PyvenvCfgParser<'s> {
    source: &'s str,
    cursor: Cursor<'s>,
    line_number: NonZeroUsize,
    data: RawPyvenvCfg<'s>,
}

impl<'s> PyvenvCfgParser<'s> {
    fn new(source: &'s str) -> Self {
        Self {
            source,
            cursor: Cursor::new(source),
            line_number: NonZeroUsize::new(1).unwrap(),
            data: RawPyvenvCfg::default(),
        }

Review comment (suggestion):
I think this validation is slightly misplaced -- it would be simpler if we added this as a condition out in `PythonEnvironment::discover`, after we construct the new `PythonEnvironment` we can call `site_packages_paths()` on it and ensure we don't get an error result. That has the same effect, but with less duplication of `PythonEnvironment` internals.

Code after:
            Self::Base
        }
    }
}

/// Read `CONDA_PREFIX` and confirm that it has the expected kind
pub(crate) fn conda_environment_from_env(
    system: &dyn System,
    kind: CondaEnvironmentKind,
) -> Option<SystemPathBuf> {
    let dir = system
        .env_var(EnvVars::CONDA_PREFIX)
        .ok()
        .filter(|value| !value.is_empty())?;
    let path = SystemPathBuf::from(dir);

    if kind != CondaEnvironmentKind::from_prefix_path(system, &path) {
        return None;
    }

    Some(path)
}

#[cfg(target_family = "wasm")]
pub(crate) fn environment_from_binary(
    _system: &dyn System,
    _binary: &str,
) -> Option<PythonEnvironment> {
    None
}

#[cfg(not(target_family = "wasm"))]
pub(crate) fn environment_from_binary(
    system: &dyn System,
    binary: &str,
) -> Option<PythonEnvironment> {
    let binary = which::WhichConfig::new_with_sys(system)
        .binary_name(binary.into())
        .first_result()
        .ok()?;
    let binary = SystemPathBuf::from_path_buf(binary).ok()?;
    let env = PythonEnvironment::new(binary, SysPrefixPathOrigin::PythonBinary, system).ok()?;

    // TODO: replace this with better shim support, e.g. pyenv
    // sanity check to filter out shims
    env.site_packages_paths(system).ok()?;

    Some(env)
}

/// A parser for `pyvenv.cfg` files: metadata files for virtual environments.