forked from astral-sh/uv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualenv.rs
More file actions
934 lines (876 loc) · 34.5 KB
/
virtualenv.rs
File metadata and controls
934 lines (876 loc) · 34.5 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! Create a virtual environment.
use std::env::consts::EXE_SUFFIX;
use std::io;
use std::io::{BufWriter, Write};
use std::path::Path;
use console::Term;
use fs_err::File;
use itertools::Itertools;
use owo_colors::OwoColorize;
use tracing::{debug, trace};
use uv_fs::{CWD, Simplified, cachedir};
use uv_preview::Preview;
use uv_pypi_types::Scheme;
use uv_python::managed::{PythonMinorVersionLink, create_link_to_executable};
use uv_python::{Interpreter, VirtualEnvironment};
use uv_shell::escape_posix_for_single_quotes;
use uv_version::version;
use uv_warnings::warn_user_once;
use crate::{Error, Prompt};
/// Activation scripts for the environment, with dependent paths templated out.
const ACTIVATE_TEMPLATES: &[(&str, &str)] = &[
("activate", include_str!("activator/activate")),
("activate.csh", include_str!("activator/activate.csh")),
("activate.fish", include_str!("activator/activate.fish")),
("activate.nu", include_str!("activator/activate.nu")),
("activate.ps1", include_str!("activator/activate.ps1")),
("activate.bat", include_str!("activator/activate.bat")),
("deactivate.bat", include_str!("activator/deactivate.bat")),
("pydoc.bat", include_str!("activator/pydoc.bat")),
(
"activate_this.py",
include_str!("activator/activate_this.py"),
),
];
const VIRTUALENV_PATCH: &str = include_str!("_virtualenv.py");
/// Very basic `.cfg` file format writer.
fn write_cfg(f: &mut impl Write, data: &[(String, String)]) -> io::Result<()> {
for (key, value) in data {
writeln!(f, "{key} = {value}")?;
}
Ok(())
}
/// Create a [`VirtualEnvironment`] at the given location.
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) fn create(
location: &Path,
interpreter: &Interpreter,
prompt: Prompt,
system_site_packages: bool,
on_existing: OnExisting,
relocatable: bool,
seed: bool,
upgradeable: bool,
preview: Preview,
) -> Result<VirtualEnvironment, Error> {
// Determine the base Python executable; that is, the Python executable that should be
// considered the "base" for the virtual environment.
//
// For consistency with the standard library, rely on `sys._base_executable`, _unless_ we're
// using a uv-managed Python (in which case, we can do better for symlinked executables).
let base_python = if cfg!(unix) && interpreter.is_standalone() {
interpreter.find_base_python()?
} else {
interpreter.to_base_python()?
};
debug!(
"Using base executable for virtual environment: {}",
base_python.display()
);
// Extract the prompt and compute the absolute path prior to validating the location; otherwise,
// we risk deleting (and recreating) the current working directory, which would cause the `CWD`
// queries to fail.
let prompt = match prompt {
Prompt::CurrentDirectoryName => CWD
.file_name()
.map(|name| name.to_string_lossy().to_string()),
Prompt::Static(value) => Some(value),
Prompt::None => None,
};
let absolute = std::path::absolute(location)?;
// Validate the existing location.
match location.metadata() {
Ok(metadata) if metadata.is_file() => {
return Err(Error::Io(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("File exists at `{}`", location.user_display()),
)));
}
Ok(metadata)
if metadata.is_dir()
&& location
.read_dir()
.is_ok_and(|mut dir| dir.next().is_none()) =>
{
// If it's an empty directory, we can proceed
trace!(
"Using empty directory at `{}` for virtual environment",
location.user_display()
);
}
Ok(metadata) if metadata.is_dir() => {
let is_virtualenv = uv_fs::is_virtualenv_base(location);
let name = if is_virtualenv {
"virtual environment"
} else {
"directory"
};
let hint = format!(
"Use the `{}` flag or set `{}` to replace the existing {name}",
"--clear".green(),
"UV_VENV_CLEAR=1".green()
);
// TODO(zanieb): We may want to consider omitting the hint in some of these cases, e.g.,
// when `--no-clear` is used do we want to suggest `--clear`?
let err = Err(Error::Io(io::Error::new(
io::ErrorKind::AlreadyExists,
format!(
"A {name} already exists at: {}\n\n{}{} {hint}",
location.user_display(),
"hint".bold().cyan(),
":".bold(),
),
)));
match on_existing {
OnExisting::Allow => {
debug!("Allowing existing {name} due to `--allow-existing`");
}
OnExisting::Remove(reason) => {
debug!("Removing existing {name} ({reason})");
// Before removing the virtual environment, we need to canonicalize the path
// because `Path::metadata` will follow the symlink but we're still operating on
// the unresolved path and will remove the symlink itself.
let location = location
.canonicalize()
.unwrap_or_else(|_| location.to_path_buf());
remove_virtualenv(&location)?;
fs_err::create_dir_all(&location)?;
}
OnExisting::Fail => return err,
// If not a virtual environment, fail without prompting.
OnExisting::Prompt if !is_virtualenv => return err,
OnExisting::Prompt => {
match confirm_clear(location, name)? {
Some(true) => {
debug!("Removing existing {name} due to confirmation");
// Before removing the virtual environment, we need to canonicalize the
// path because `Path::metadata` will follow the symlink but we're still
// operating on the unresolved path and will remove the symlink itself.
let location = location
.canonicalize()
.unwrap_or_else(|_| location.to_path_buf());
remove_virtualenv(&location)?;
fs_err::create_dir_all(&location)?;
}
Some(false) => return err,
// When we don't have a TTY, warn that the behavior will change in the future
None => {
warn_user_once!(
"A {name} already exists at `{}`. In the future, uv will require `{}` to replace it",
location.user_display(),
"--clear".green(),
);
}
}
}
}
}
Ok(_) => {
// It's not a file or a directory
return Err(Error::Io(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("Object already exists at `{}`", location.user_display()),
)));
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {
fs_err::create_dir_all(location)?;
}
Err(err) => return Err(Error::Io(err)),
}
// Use the absolute path for all further operations.
let location = absolute;
let bin_name = if cfg!(unix) {
"bin"
} else if cfg!(windows) {
"Scripts"
} else {
unimplemented!("Only Windows and Unix are supported")
};
let scripts = location.join(&interpreter.virtualenv().scripts);
// Add the CACHEDIR.TAG.
cachedir::ensure_tag(&location)?;
// Create a `.gitignore` file to ignore all files in the venv.
fs_err::write(location.join(".gitignore"), "*")?;
let mut using_minor_version_link = false;
let executable_target = if upgradeable && interpreter.is_standalone() {
if let Some(minor_version_link) = PythonMinorVersionLink::from_executable(
base_python.as_path(),
&interpreter.key(),
preview,
) {
if !minor_version_link.exists() {
base_python.clone()
} else {
let debug_symlink_term = if cfg!(windows) {
"junction"
} else {
"symlink directory"
};
debug!(
"Using {} {} instead of base Python path: {}",
debug_symlink_term,
&minor_version_link.symlink_directory.display(),
&base_python.display()
);
using_minor_version_link = true;
minor_version_link.symlink_executable.clone()
}
} else {
base_python.clone()
}
} else {
base_python.clone()
};
// Per PEP 405, the Python `home` is the parent directory of the interpreter.
// In preview mode, for standalone interpreters, this `home` value will include a
// symlink directory on Unix or junction on Windows to enable transparent Python patch
// upgrades.
let python_home = executable_target
.parent()
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"The Python interpreter needs to have a parent directory",
)
})?
.to_path_buf();
let python_home = python_home.as_path();
// Different names for the python interpreter
fs_err::create_dir_all(&scripts)?;
let executable = scripts.join(format!("python{EXE_SUFFIX}"));
#[cfg(unix)]
{
uv_fs::replace_symlink(&executable_target, &executable)?;
uv_fs::replace_symlink(
"python",
scripts.join(format!("python{}", interpreter.python_major())),
)?;
uv_fs::replace_symlink(
"python",
scripts.join(format!(
"python{}.{}",
interpreter.python_major(),
interpreter.python_minor(),
)),
)?;
if interpreter.gil_disabled() {
uv_fs::replace_symlink(
"python",
scripts.join(format!(
"python{}.{}t",
interpreter.python_major(),
interpreter.python_minor(),
)),
)?;
}
if interpreter.markers().implementation_name() == "pypy" {
uv_fs::replace_symlink(
"python",
scripts.join(format!("pypy{}", interpreter.python_major())),
)?;
uv_fs::replace_symlink("python", scripts.join("pypy"))?;
}
if interpreter.markers().implementation_name() == "graalpy" {
uv_fs::replace_symlink("python", scripts.join("graalpy"))?;
}
}
// On Windows, we use trampolines that point to an executable target. For standalone
// interpreters, this target path includes a minor version junction to enable
// transparent upgrades.
if cfg!(windows) {
if using_minor_version_link {
let target = scripts.join(WindowsExecutable::Python.exe(interpreter));
create_link_to_executable(target.as_path(), &executable_target)
.map_err(Error::Python)?;
let targetw = scripts.join(WindowsExecutable::Pythonw.exe(interpreter));
create_link_to_executable(targetw.as_path(), &executable_target)
.map_err(Error::Python)?;
if interpreter.gil_disabled() {
let targett = scripts.join(WindowsExecutable::PythonMajorMinort.exe(interpreter));
create_link_to_executable(targett.as_path(), &executable_target)
.map_err(Error::Python)?;
let targetwt = scripts.join(WindowsExecutable::PythonwMajorMinort.exe(interpreter));
create_link_to_executable(targetwt.as_path(), &executable_target)
.map_err(Error::Python)?;
}
} else {
// Always copy `python.exe`.
copy_launcher_windows(
WindowsExecutable::Python,
interpreter,
&base_python,
&scripts,
python_home,
)?;
match interpreter.implementation_name() {
"graalpy" => {
// For GraalPy, copy `graalpy.exe` and `python3.exe`.
copy_launcher_windows(
WindowsExecutable::GraalPy,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PythonMajor,
interpreter,
&base_python,
&scripts,
python_home,
)?;
}
"pypy" => {
// For PyPy, copy all versioned executables and all PyPy-specific executables.
copy_launcher_windows(
WindowsExecutable::PythonMajor,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PythonMajorMinor,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::Pythonw,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PyPy,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PyPyMajor,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PyPyMajorMinor,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PyPyw,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PyPyMajorMinorw,
interpreter,
&base_python,
&scripts,
python_home,
)?;
}
_ => {
// For all other interpreters, copy `pythonw.exe`.
copy_launcher_windows(
WindowsExecutable::Pythonw,
interpreter,
&base_python,
&scripts,
python_home,
)?;
// If the GIL is disabled, copy `venvlaunchert.exe` and `venvwlaunchert.exe`.
if interpreter.gil_disabled() {
copy_launcher_windows(
WindowsExecutable::PythonMajorMinort,
interpreter,
&base_python,
&scripts,
python_home,
)?;
copy_launcher_windows(
WindowsExecutable::PythonwMajorMinort,
interpreter,
&base_python,
&scripts,
python_home,
)?;
}
}
}
}
}
#[cfg(not(any(unix, windows)))]
{
compile_error!("Only Windows and Unix are supported")
}
// Add all the activate scripts for different shells
for (name, template) in ACTIVATE_TEMPLATES {
let path_sep = if cfg!(windows) { ";" } else { ":" };
let relative_site_packages = [
interpreter.virtualenv().purelib.as_path(),
interpreter.virtualenv().platlib.as_path(),
]
.iter()
.dedup()
.map(|path| {
pathdiff::diff_paths(path, &interpreter.virtualenv().scripts)
.expect("Failed to calculate relative path to site-packages")
})
.map(|path| path.simplified().to_str().unwrap().replace('\\', "\\\\"))
.join(path_sep);
let virtual_env_dir = match (relocatable, name.to_owned()) {
(true, "activate") => {
r#"'"$(dirname -- "$(dirname -- "$(realpath -- "$SCRIPT_PATH")")")"'"#.to_string()
}
(true, "activate.bat") => r"%~dp0..".to_string(),
(true, "activate.fish") => {
r#"'"$(dirname -- "$(cd "$(dirname -- "$(status -f)")"; and pwd)")"'"#.to_string()
}
(true, "activate.nu") => r"(path self | path dirname | path dirname)".to_string(),
(false, "activate.nu") => {
format!(
"'{}'",
escape_posix_for_single_quotes(location.simplified().to_str().unwrap())
)
}
// Note:
// * relocatable activate scripts appear not to be possible in csh.
// * `activate.ps1` is already relocatable by default.
_ => escape_posix_for_single_quotes(location.simplified().to_str().unwrap()),
};
let activator = template
.replace("{{ VIRTUAL_ENV_DIR }}", &virtual_env_dir)
.replace("{{ BIN_NAME }}", bin_name)
.replace(
"{{ VIRTUAL_PROMPT }}",
prompt.as_deref().unwrap_or_default(),
)
.replace("{{ PATH_SEP }}", path_sep)
.replace("{{ RELATIVE_SITE_PACKAGES }}", &relative_site_packages);
fs_err::write(scripts.join(name), activator)?;
}
let mut pyvenv_cfg_data: Vec<(String, String)> = vec![
(
"home".to_string(),
python_home.simplified_display().to_string(),
),
(
"implementation".to_string(),
interpreter
.markers()
.platform_python_implementation()
.to_string(),
),
("uv".to_string(), version().to_string()),
(
"version_info".to_string(),
interpreter.markers().python_full_version().string.clone(),
),
(
"include-system-site-packages".to_string(),
if system_site_packages {
"true".to_string()
} else {
"false".to_string()
},
),
];
if relocatable {
pyvenv_cfg_data.push(("relocatable".to_string(), "true".to_string()));
}
if seed {
pyvenv_cfg_data.push(("seed".to_string(), "true".to_string()));
}
if let Some(prompt) = prompt {
pyvenv_cfg_data.push(("prompt".to_string(), prompt));
}
if cfg!(windows) && interpreter.markers().implementation_name() == "graalpy" {
pyvenv_cfg_data.push((
"venvlauncher_command".to_string(),
python_home
.join("graalpy.exe")
.simplified_display()
.to_string(),
));
}
let mut pyvenv_cfg = BufWriter::new(File::create(location.join("pyvenv.cfg"))?);
write_cfg(&mut pyvenv_cfg, &pyvenv_cfg_data)?;
drop(pyvenv_cfg);
// Construct the path to the `site-packages` directory.
let site_packages = location.join(&interpreter.virtualenv().purelib);
fs_err::create_dir_all(&site_packages)?;
// If necessary, create a symlink from `lib64` to `lib`.
// See: https://github.com/python/cpython/blob/b228655c227b2ca298a8ffac44d14ce3d22f6faa/Lib/venv/__init__.py#L135C11-L135C16
#[cfg(unix)]
if interpreter.pointer_size().is_64()
&& interpreter.markers().os_name() == "posix"
&& interpreter.markers().sys_platform() != "darwin"
{
match fs_err::os::unix::fs::symlink("lib", location.join("lib64")) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {}
Err(err) => {
return Err(err.into());
}
}
}
// Populate `site-packages` with a `_virtualenv.py` file.
fs_err::write(site_packages.join("_virtualenv.py"), VIRTUALENV_PATCH)?;
fs_err::write(site_packages.join("_virtualenv.pth"), "import _virtualenv")?;
Ok(VirtualEnvironment {
scheme: Scheme {
purelib: location.join(&interpreter.virtualenv().purelib),
platlib: location.join(&interpreter.virtualenv().platlib),
scripts: location.join(&interpreter.virtualenv().scripts),
data: location.join(&interpreter.virtualenv().data),
include: location.join(&interpreter.virtualenv().include),
},
root: location,
executable,
base_executable: base_python,
})
}
/// Prompt a confirmation that the virtual environment should be cleared.
///
/// If not a TTY, returns `None`.
fn confirm_clear(location: &Path, name: &'static str) -> Result<Option<bool>, io::Error> {
let term = Term::stderr();
if term.is_term() {
let prompt = format!(
"A {name} already exists at `{}`. Do you want to replace it?",
location.user_display(),
);
let hint = format!(
"Use the `{}` flag or set `{}` to skip this prompt",
"--clear".green(),
"UV_VENV_CLEAR=1".green()
);
Ok(Some(uv_console::confirm_with_hint(
&prompt, &hint, &term, true,
)?))
} else {
Ok(None)
}
}
/// Perform a safe removal of a virtual environment.
pub fn remove_virtualenv(location: &Path) -> Result<(), Error> {
// On Windows, if the current executable is in the directory, defer self-deletion since Windows
// won't let you unlink a running executable.
#[cfg(windows)]
if let Ok(itself) = std::env::current_exe() {
let target = std::path::absolute(location)?;
if itself.starts_with(&target) {
debug!("Detected self-delete of executable: {}", itself.display());
self_replace::self_delete_outside_path(location)?;
}
}
// We defer removal of the `pyvenv.cfg` until the end, so if we fail to remove the environment,
// uv can still identify it as a Python virtual environment that can be deleted.
for entry in fs_err::read_dir(location)? {
let entry = entry?;
let path = entry.path();
if path == location.join("pyvenv.cfg") {
continue;
}
if path.is_dir() {
fs_err::remove_dir_all(&path)?;
} else {
fs_err::remove_file(&path)?;
}
}
match fs_err::remove_file(location.join("pyvenv.cfg")) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => return Err(err.into()),
}
// Remove the virtual environment directory itself
match fs_err::remove_dir_all(location) {
Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
// If the virtual environment is a mounted file system, e.g., in a Docker container, we
// cannot delete it — but that doesn't need to be a fatal error
Err(err) if err.kind() == io::ErrorKind::ResourceBusy => {
debug!(
"Skipping removal of `{}` directory due to {err}",
location.display(),
);
}
Err(err) => return Err(err.into()),
}
Ok(())
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum RemovalReason {
/// The removal was explicitly requested, i.e., with `--clear`.
UserRequest,
/// The environment can be removed because it is considered temporary, e.g., a build
/// environment.
TemporaryEnvironment,
/// The environment can be removed because it is managed by uv, e.g., a project or tool
/// environment.
ManagedEnvironment,
}
impl std::fmt::Display for RemovalReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::UserRequest => f.write_str("requested with `--clear`"),
Self::ManagedEnvironment => f.write_str("environment is managed by uv"),
Self::TemporaryEnvironment => f.write_str("environment is temporary"),
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum OnExisting {
/// Prompt before removing an existing directory.
///
/// If a TTY is not available, fail.
#[default]
Prompt,
/// Fail if the directory already exists and is non-empty.
Fail,
/// Allow an existing directory, overwriting virtual environment files while retaining other
/// files in the directory.
Allow,
/// Remove an existing directory.
Remove(RemovalReason),
}
impl OnExisting {
pub fn from_args(allow_existing: bool, clear: bool, no_clear: bool) -> Self {
if allow_existing {
Self::Allow
} else if clear {
Self::Remove(RemovalReason::UserRequest)
} else if no_clear {
Self::Fail
} else {
Self::Prompt
}
}
}
#[derive(Debug, Copy, Clone)]
enum WindowsExecutable {
/// The `python.exe` executable (or `venvlauncher.exe` launcher shim).
Python,
/// The `python3.exe` executable (or `venvlauncher.exe` launcher shim).
PythonMajor,
/// The `python3.<minor>.exe` executable (or `venvlauncher.exe` launcher shim).
PythonMajorMinor,
/// The `python3.<minor>t.exe` executable (or `venvlaunchert.exe` launcher shim).
PythonMajorMinort,
/// The `pythonw.exe` executable (or `venvwlauncher.exe` launcher shim).
Pythonw,
/// The `pythonw3.<minor>t.exe` executable (or `venvwlaunchert.exe` launcher shim).
PythonwMajorMinort,
/// The `pypy.exe` executable.
PyPy,
/// The `pypy3.exe` executable.
PyPyMajor,
/// The `pypy3.<minor>.exe` executable.
PyPyMajorMinor,
/// The `pypyw.exe` executable.
PyPyw,
/// The `pypy3.<minor>w.exe` executable.
PyPyMajorMinorw,
/// The `graalpy.exe` executable.
GraalPy,
}
impl WindowsExecutable {
/// The name of the Python executable.
fn exe(self, interpreter: &Interpreter) -> String {
match self {
Self::Python => String::from("python.exe"),
Self::PythonMajor => {
format!("python{}.exe", interpreter.python_major())
}
Self::PythonMajorMinor => {
format!(
"python{}.{}.exe",
interpreter.python_major(),
interpreter.python_minor()
)
}
Self::PythonMajorMinort => {
format!(
"python{}.{}t.exe",
interpreter.python_major(),
interpreter.python_minor()
)
}
Self::Pythonw => String::from("pythonw.exe"),
Self::PythonwMajorMinort => {
format!(
"pythonw{}.{}t.exe",
interpreter.python_major(),
interpreter.python_minor()
)
}
Self::PyPy => String::from("pypy.exe"),
Self::PyPyMajor => {
format!("pypy{}.exe", interpreter.python_major())
}
Self::PyPyMajorMinor => {
format!(
"pypy{}.{}.exe",
interpreter.python_major(),
interpreter.python_minor()
)
}
Self::PyPyw => String::from("pypyw.exe"),
Self::PyPyMajorMinorw => {
format!(
"pypy{}.{}w.exe",
interpreter.python_major(),
interpreter.python_minor()
)
}
Self::GraalPy => String::from("graalpy.exe"),
}
}
/// The name of the launcher shim.
fn launcher(self, interpreter: &Interpreter) -> &'static str {
match self {
Self::Python | Self::PythonMajor | Self::PythonMajorMinor
if interpreter.gil_disabled() =>
{
"venvlaunchert.exe"
}
Self::Python | Self::PythonMajor | Self::PythonMajorMinor => "venvlauncher.exe",
Self::Pythonw if interpreter.gil_disabled() => "venvwlaunchert.exe",
Self::Pythonw => "venvwlauncher.exe",
Self::PythonMajorMinort => "venvlaunchert.exe",
Self::PythonwMajorMinort => "venvwlaunchert.exe",
// From 3.13 on these should replace the `python.exe` and `pythonw.exe` shims.
// These are not relevant as of now for PyPy as it doesn't yet support Python 3.13.
Self::PyPy | Self::PyPyMajor | Self::PyPyMajorMinor => "venvlauncher.exe",
Self::PyPyw | Self::PyPyMajorMinorw => "venvwlauncher.exe",
Self::GraalPy => "venvlauncher.exe",
}
}
}
/// <https://github.com/python/cpython/blob/d457345bbc6414db0443819290b04a9a4333313d/Lib/venv/__init__.py#L261-L267>
/// <https://github.com/pypa/virtualenv/blob/d9fdf48d69f0d0ca56140cf0381edbb5d6fe09f5/src/virtualenv/create/via_global_ref/builtin/cpython/cpython3.py#L78-L83>
///
/// There's two kinds of applications on windows: Those that allocate a console (python.exe)
/// and those that don't because they use window(s) (pythonw.exe).
fn copy_launcher_windows(
executable: WindowsExecutable,
interpreter: &Interpreter,
base_python: &Path,
scripts: &Path,
python_home: &Path,
) -> Result<(), Error> {
// First priority: the `python.exe` and `pythonw.exe` shims.
let shim = interpreter
.stdlib()
.join("venv")
.join("scripts")
.join("nt")
.join(executable.exe(interpreter));
match fs_err::copy(shim, scripts.join(executable.exe(interpreter))) {
Ok(_) => return Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err.into());
}
}
// Second priority: the `venvlauncher.exe` and `venvwlauncher.exe` shims.
// These are equivalent to the `python.exe` and `pythonw.exe` shims, which were
// renamed in Python 3.13.
let shim = interpreter
.stdlib()
.join("venv")
.join("scripts")
.join("nt")
.join(executable.launcher(interpreter));
match fs_err::copy(shim, scripts.join(executable.exe(interpreter))) {
Ok(_) => return Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err.into());
}
}
// Third priority: on Conda at least, we can look for the launcher shim next to
// the Python executable itself.
let shim = base_python.with_file_name(executable.launcher(interpreter));
match fs_err::copy(shim, scripts.join(executable.exe(interpreter))) {
Ok(_) => return Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err.into());
}
}
// Fourth priority: if the launcher shim doesn't exist, assume this is
// an embedded Python. Copy the Python executable itself, along with
// the DLLs, `.pyd` files, and `.zip` files in the same directory.
match fs_err::copy(
base_python.with_file_name(executable.exe(interpreter)),
scripts.join(executable.exe(interpreter)),
) {
Ok(_) => {
// Copy `.dll` and `.pyd` files from the top-level, and from the
// `DLLs` subdirectory (if it exists).
for directory in [
python_home,
interpreter.sys_base_prefix().join("DLLs").as_path(),
] {
let entries = match fs_err::read_dir(directory) {
Ok(read_dir) => read_dir,
Err(err) if err.kind() == io::ErrorKind::NotFound => {
continue;
}
Err(err) => {
return Err(err.into());
}
};
for entry in entries {
let entry = entry?;
let path = entry.path();
if path.extension().is_some_and(|ext| {
ext.eq_ignore_ascii_case("dll") || ext.eq_ignore_ascii_case("pyd")
}) {
if let Some(file_name) = path.file_name() {
fs_err::copy(&path, scripts.join(file_name))?;
}
}
}
}
// Copy `.zip` files from the top-level.
match fs_err::read_dir(python_home) {
Ok(entries) => {
for entry in entries {
let entry = entry?;
let path = entry.path();
if path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("zip"))
{
if let Some(file_name) = path.file_name() {
fs_err::copy(&path, scripts.join(file_name))?;
}
}
}
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err.into());
}
}
return Ok(());
}
Err(err) if err.kind() == io::ErrorKind::NotFound => {}
Err(err) => {
return Err(err.into());
}
}
Err(Error::NotFound(base_python.user_display().to_string()))
}