pyenv-python 0.4.0

A pyenv shim for python that's much faster than pyenv.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#![forbid(unsafe_code)]

use std::{env, fmt, io};
use std::ffi::{OsStr, OsString};
use std::fmt::{Debug, Display, Formatter};
use std::fs::File;
use std::path::{Path, PathBuf};

use apply::Apply;
use is_executable::IsExecutable;
use same_file::Handle;
use thiserror::Error;

mod version;

/// A root `pyenv` directory.
#[derive(Debug)]
pub struct PyenvRoot {
    root: PathBuf,
}

impl Display for PyenvRoot {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.root.display())
    }
}

/// Why the pyenv root, i.e. what `$(pyenv root)` returns, could not be found.
///
/// See [`PyenvRoot::new`].
#[derive(Debug, Error)]
pub enum PyenvRootError {
    /// Either the environment variable `$PYENV_ROOT` does not exist,
    /// or the home directory does not exist
    /// (very unlikely, unless in an OS like wasm).
    #[error("the environment variable $PYENV_ROOT does not exist")]
    NoEnvVarOrHomeDir,
    /// The pyenv root is not a directory.
    #[error("pyenv root is not a directory: {root}")]
    NotADir { root: PathBuf },
    /// The pyenv root could not be accessed (usually it doesn't exist).
    #[error("could not find pyenv root: {root}")]
    IOError { root: PathBuf, source: io::Error },
}

impl PyenvRoot {
    /// Returns what `$(pyenv root)` would return.
    /// That is, `$PYENV_ROOT` or `$HOME/.pyenv` if they exist.
    ///
    /// See [`PyenvRootError`] for possible errors.
    pub fn new() -> Result<Self, PyenvRootError> {
        use PyenvRootError::*;
        let root = env::var_os("PYENV_ROOT")
            .map(|root| root.into())
            .or_else(|| dirs_next::home_dir().map(|home| home.join(".pyenv")))
            .ok_or(NoEnvVarOrHomeDir)?;
        match root.metadata() {
            Ok(metadata) => if metadata.is_dir() {
                Ok(Self { root })
            } else {
                Err(NotADir { root })
            },
            Err(source) => Err(IOError { root, source }),
        }
    }
}

/// Where the given [`PyenvVersion`] was found from.
#[derive(Debug, Copy, Clone)]
pub enum PyenvVersionFrom {
    Shell,
    Local,
    Global,
}

impl Display for PyenvVersionFrom {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::Shell => "shell",
            Self::Local => "local",
            Self::Global => "global",
        };
        write!(f, "{}", name)
    }
}

/// A `pyenv` version, either a `python` version or a virtualenv name,
/// and where it was looked-up from.
#[derive(Debug)]
pub struct PyenvVersion {
    version: String,
    from: PyenvVersionFrom,
}

impl Display for PyenvVersion {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "python {} from {}", self.version, self.from)
    }
}

impl PyenvRoot {
    /// Returns the current pyenv version as determined by
    /// <https://github.com/pyenv/pyenv#choosing-the-python-version>.
    fn version(&self) -> Result<PyenvVersion, ()> {
        self
            .root
            .as_path()
            .apply(version::pyenv_version)
            .ok_or(())
    }
    
    fn python_path(&self, path_components: &[&str]) -> UncheckedPythonPath {
        let mut path = self.root.clone();
        for path_component in path_components {
            path.push(path_component)
        }
        path.push("python");
        UncheckedPythonPath::from_existing(path)
    }
    
    fn python_version_path(&self, version: &PyenvVersion) -> UncheckedPythonPath {
        self.python_path(&[
            "versions",
            version.version.as_str(),
            "bin",
        ])
    }
    
    fn python_shim_path(&self) -> UncheckedPythonPath {
        self.python_path(&[
            "shims",
        ])
    }
}

/// A path that might be a `python` executable.
#[derive(Debug)]
pub struct UncheckedPythonPath {
    path: PathBuf,
}

impl Display for UncheckedPythonPath {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "unchecked({})", self.path.display())
    }
}

/// The path to an existing (likely) `python` executable.
#[derive(Debug, Eq)]
pub struct PythonExecutable {
    /// The name to execute this python executable as (arg0).
    /// If [`None`], then the file name of the [`PythonExecutable::path`] is used instead.
    name: Option<OsString>,
    /// The path to the python executable.
    path: PathBuf,
    /// An open handle to the python executable for file equality.
    handle: Handle,
}

impl PartialEq for PythonExecutable {
    fn eq(&self, other: &Self) -> bool {
        self.handle == other.handle
    }
}

impl Display for PythonExecutable {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.path.display())
    }
}

impl PythonExecutable {
    pub fn path(&self) -> &Path {
        self.path.as_path()
    }
    
    pub fn into_path(self) -> PathBuf {
        self.path
    }
    
    pub fn name(&self) -> &OsStr {
        self.name.as_deref()
            .unwrap_or_else(|| self.path.file_name()
                .expect("python executable should always have a file name (i.e. not root)")
            )
    }
    
    pub fn handle(&self) -> &Handle {
        &self.handle
    }
    
    pub fn file(&self) -> &File {
        self.handle.as_file()
    }
}

#[derive(Error, Debug)]
pub enum PyenvPythonExecutableError {
    #[error("python not found: {0}")]
    NotFound(#[from] io::Error),
    #[error("python must be executable")]
    NotExecutable,
}

impl PythonExecutable {
    /// Check that the `python` path actually points to an executable
    /// (can't verify that it's actually Python, but it's at least an executable named `python`).
    ///
    /// See [`PyenvPythonExecutableError`] for possible errors.
    pub fn new(path: PathBuf) -> Result<Self, (PyenvPythonExecutableError, PathBuf)> {
        use PyenvPythonExecutableError::*;
        match (|path: &Path| {
            let handle = Handle::from_path(path)?;
            if !path.is_executable() {
                // Means path must have a file name.
                return Err(NotExecutable);
            }
            Ok(handle)
        })(path.as_path()) {
            Ok(handle) => Ok(Self {
                name: None,
                path,
                handle,
            }),
            Err(e) => Err((e, path))
        }
    }
    
    pub fn current() -> io::Result<Self> {
        let name = env::args_os()
            .next()
            .map(PathBuf::from)
            .and_then(|path| path.file_name().map(|name| name.to_os_string()));
        // TODO What to do if arg0 doesn't exist or is `/` (no file name)?
        // TODO Do I just silently default to the current executable's name?
        // TODO Though in all normal invocations (like as a symlink), this won't happen.
        let path = env::current_exe()?;
        let handle = Handle::from_path(path.as_path())?;
        Ok(Self {
            name,
            path,
            handle,
        })
    }
}

impl UncheckedPythonPath {
    pub fn from_existing(path: PathBuf) -> Self {
        Self { path }
    }
    
    pub fn check(self) -> Result<PythonExecutable, (PyenvPythonExecutableError, PathBuf)> {
        PythonExecutable::new(self.path)
    }
}

pub trait HasPython {
    fn python(&self) -> &PythonExecutable;
    
    fn into_python(self) -> PythonExecutable;
}

impl HasPython for PythonExecutable {
    fn python(&self) -> &PythonExecutable {
        self
    }
    
    fn into_python(self) -> PythonExecutable {
        self
    }
}

/// An existing `pyenv` `python` executable.
#[derive(Debug)]
pub struct Pyenv {
    root: PyenvRoot,
    version: PyenvVersion,
    python_path: PythonExecutable,
}

impl Display for Pyenv {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "pyenv {} at {}", self.version, self.python_path)
    }
}

impl HasPython for Pyenv {
    fn python(&self) -> &PythonExecutable {
        &self.python_path
    }
    
    fn into_python(self) -> PythonExecutable {
        self.python_path
    }
}

/// Possible errors in looking up the current `pyenv` `python` executable.
#[derive(Error, Debug)]
pub enum PyenvError {
    // The `pyenv` root couldn't be found, so the `pyenv` version or `python` executable couldn't.
    #[error("pyenv python can't be found because no root was found: {error}")]
    NoRoot {
        #[from] error: PyenvRootError,
    },
    /// The `pyenv` version can't be found anywhere,
    /// neither the shell, local, or global versions.
    ///
    /// See <https://github.com/pyenv/pyenv#choosing-the-python-version> for the algorithm.
    #[error("pyenv python can't be found because no version was found in shell, local, or global using root {root}")]
    NoVersion {
        root: PyenvRoot,
    },
    /// The `pyenv` `python` executable can't be found or is not an executable.
    #[error("pyenv {version} can't be found at {python_path}")]
    NoExecutable {
        #[source] error: PyenvPythonExecutableError,
        root: PyenvRoot,
        version: PyenvVersion,
        python_path: PathBuf,
    },
}

impl Pyenv {
    /// Looks up the current `pyenv` `python` executable and version,
    /// or returns which part could not be found.
    ///
    /// See [`PyenvError`] for possible errors.
    pub fn new() -> Result<Self, PyenvError> {
        use PyenvError::*;
        let root = PyenvRoot::new()?;
        // Have to use `match` here instead of `map_err()?` so rustc can see the moves are disjoint.
        let version = match root.version() {
            Err(()) => return Err(NoVersion { root }),
            Ok(version) => version,
        };
        let python_path = match root.python_version_path(&version).check() {
            Err((error, python_path)) => return Err(NoExecutable {
                error,
                root,
                version,
                python_path,
            }),
            Ok(path) => path,
        };
        Ok(Self {
            root,
            version,
            python_path,
        })
    }
}

/// A `python` executable, either a `pyenv` one or the system `python`
/// (i.e. whatever else is in `$PATH`).
#[derive(Debug)]
pub enum Python {
    Pyenv(Pyenv),
    System(PythonExecutable),
}

impl Display for Python {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Pyenv(pyenv) =>
                write!(f, "{}", pyenv),
            Self::System(python_executable) =>
                write!(f, "system python on $PATH at {}", python_executable),
        }
    }
}

impl HasPython for Python {
    fn python(&self) -> &PythonExecutable {
        match self {
            Self::Pyenv(pyenv) => pyenv.python(),
            Self::System(python) => python.python(),
        }
    }
    
    fn into_python(self) -> PythonExecutable {
        match self {
            Self::Pyenv(pyenv) => pyenv.into_python(),
            Self::System(python) => python.into_python(),
        }
    }
}

#[derive(Error, Debug)]
pub enum SystemPythonError {
    #[error("failed to get current executable: {0}")]
    NoCurrentExe(#[from] io::Error),
    #[error("no $PATH to search")]
    NoPath,
    #[error("no other python in $PATH")]
    NotInPath,
}

impl Python {
    /// Lookup the current system `python`, i.e., whatever next is in `$PATH`
    /// that's not the current executable or a `pyenv` shim.
    ///
    /// Pass a [`PyenvRoot`] to avoid `pyenv` shims.
    /// If there is no `pyenv` root than [`None`] will work.
    ///
    /// Specifically, this returns the next `python` on `$PATH`,
    /// excluding the current executable and `$PYENV_ROOT/shims/python`.
    /// Otherwise, an infinite loop would be formed between ourselves and `$PYENV_ROOT/shims/python`.
    ///
    /// See [`SystemPythonError`] for possible errors.
    pub fn system(pyenv_root: Option<PyenvRoot>) -> Result<PythonExecutable, SystemPythonError> {
        use SystemPythonError::*;
        let current_python = PythonExecutable::current()?;
        let pyenv_shim_python = pyenv_root
            .map(|root| root.python_shim_path())
            .and_then(|path| path.check().ok());
        let path_var = env::var_os("PATH").ok_or(NoPath)?;
        env::split_paths(&path_var)
            .map(|mut path| {
                path.push(current_python.name());
                path
            })
            .map(UncheckedPythonPath::from_existing)
            .filter_map(|python| python.check().ok())
            .find(|python| python != &current_python && Some(python) != pyenv_shim_python.as_ref())
            .ok_or(NotInPath)
    }
}

#[derive(Error, Debug)]
#[error("couldn't find pyenv and system python: {pyenv}, {system}")]
pub struct PythonError {
    pub pyenv: PyenvError,
    pub system: SystemPythonError,
}

impl Python {
    /// Lookup a `python` executable.
    ///
    /// If a `pyenv` `python` cannot be found (see [`Pyenv::new`]),
    /// try finding the system `python` (see [`Python::system`]).
    /// If neither can be found, return the errors for both in [`PythonError`].
    pub fn new() -> Result<Self, PythonError> {
        match Pyenv::new() {
            Ok(pyenv) => Ok(Self::Pyenv(pyenv)),
            Err(pyenv_error) => match Self::system(None) {
                Ok(system_python) => Ok(Self::System(system_python)),
                Err(system_python_error) => Err(PythonError {
                    pyenv: pyenv_error,
                    system: system_python_error,
                }),
            },
        }
    }
}