Make directory a required argument (#5573)

This commit is contained in:
Charlie Marsh 2024-07-29 16:47:34 -04:00 committed by GitHub
parent e46c24d3cf
commit 37388f0987
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 26 deletions

View file

@ -16,7 +16,7 @@ pub static PYTHON_VERSIONS_FILENAME: &str = ".python-versions";
/// Prefers `.python-versions` then `.python-version`.
/// If only one Python version is desired, use [`request_from_version_files`] which prefers the `.python-version` file.
pub async fn requests_from_version_file(
directory: Option<&Path>,
directory: &Path,
) -> Result<Option<Vec<PythonRequest>>, std::io::Error> {
if let Some(versions) = read_versions_file(directory).await? {
Ok(Some(
@ -40,7 +40,7 @@ pub async fn requests_from_version_file(
/// Prefers `.python-version` then the first entry of `.python-versions`.
/// If multiple Python versions are desired, use [`requests_from_version_files`] instead.
pub async fn request_from_version_file(
directory: Option<&Path>,
directory: &Path,
) -> Result<Option<PythonRequest>, std::io::Error> {
if let Some(version) = read_version_file(directory).await? {
Ok(Some(PythonRequest::parse(&version)))
@ -61,15 +61,9 @@ pub async fn write_version_file(version: &str) -> Result<(), std::io::Error> {
fs::tokio::write(PYTHON_VERSION_FILENAME, format!("{version}\n")).await
}
async fn read_versions_file(
directory: Option<&Path>,
) -> Result<Option<Vec<String>>, std::io::Error> {
let file_path = directory.map(|pth| pth.join(PYTHON_VERSIONS_FILENAME));
let path = file_path
.as_deref()
.unwrap_or(Path::new(PYTHON_VERSIONS_FILENAME));
match fs::tokio::read_to_string(path).await {
async fn read_versions_file(directory: &Path) -> Result<Option<Vec<String>>, std::io::Error> {
let path = directory.join(PYTHON_VERSIONS_FILENAME);
match fs::tokio::read_to_string(&path).await {
Ok(content) => {
debug!("Reading requests from `{}`", path.display());
Ok(Some(
@ -89,13 +83,9 @@ async fn read_versions_file(
}
}
async fn read_version_file(directory: Option<&Path>) -> Result<Option<String>, std::io::Error> {
let file_path = directory.map(|pth| pth.join(PYTHON_VERSION_FILENAME));
let path = file_path
.as_deref()
.unwrap_or(Path::new(PYTHON_VERSION_FILENAME));
match fs::tokio::read_to_string(path).await {
async fn read_version_file(directory: &Path) -> Result<Option<String>, std::io::Error> {
let path = directory.join(PYTHON_VERSION_FILENAME);
match fs::tokio::read_to_string(&path).await {
Ok(content) => {
debug!("Reading requests from `{}`", path.display());
Ok(content

View file

@ -161,9 +161,7 @@ impl FoundInterpreter {
let python_request = if let Some(request) = python_request {
Some(request)
// (2) Request from `.python-version`
} else if let Some(request) =
request_from_version_file(Some(workspace.install_path())).await?
{
} else if let Some(request) = request_from_version_file(workspace.install_path()).await? {
Some(request)
// (3) `Requires-Python` in `pyproject.toml`
} else {

View file

@ -109,7 +109,7 @@ pub(crate) async fn run(
let python_request = if let Some(request) = python.as_deref() {
Some(PythonRequest::parse(request))
// (2) Request from `.python-version`
} else if let Some(request) = request_from_version_file(Some(&directory)).await? {
} else if let Some(request) = request_from_version_file(&directory).await? {
Some(request)
// (3) `Requires-Python` in `pyproject.toml`
} else {

View file

@ -55,7 +55,7 @@ pub(crate) async fn install(
}
None
} else {
requests_from_version_file(None).await?
requests_from_version_file(&std::env::current_dir()?).await?
};
version_file_requests.unwrap_or_else(|| vec![PythonRequest::Any])
} else {

View file

@ -49,7 +49,7 @@ pub(crate) async fn pin(
let Some(request) = request else {
// Display the current pinned Python version
if let Some(pins) = requests_from_version_file(None).await? {
if let Some(pins) = requests_from_version_file(&std::env::current_dir()?).await? {
for pin in pins {
writeln!(printer.stdout(), "{}", pin.to_canonical_string())?;
if let Some(virtual_project) = &virtual_project {
@ -126,7 +126,10 @@ pub(crate) async fn pin(
request.to_canonical_string()
};
let existing = request_from_version_file(None).await.ok().flatten();
let existing = request_from_version_file(&std::env::current_dir()?)
.await
.ok()
.flatten();
write_version_file(&output).await?;
if let Some(existing) = existing

View file

@ -140,7 +140,10 @@ async fn venv_impl(
let mut interpreter_request = python_request.map(PythonRequest::parse);
if preview.is_enabled() && interpreter_request.is_none() {
interpreter_request = request_from_version_file(None).await.into_diagnostic()?;
interpreter_request =
request_from_version_file(&std::env::current_dir().into_diagnostic()?)
.await
.into_diagnostic()?;
}
if preview.is_disabled() && relocatable {
warn_user_once!("`--relocatable` is experimental and may change without warning");