wovensnake 0.3.4

A high-performance Python package manager built with Rust.
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
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
use flate2::read::GzDecoder;
use futures::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use once_cell::sync::OnceCell;
use reqwest;
use serde::Deserialize;
use std::env;
use std::error::Error;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use tar::Archive;
use zip::ZipArchive;

#[derive(Deserialize, Debug)]
struct GithubRelease {
    #[serde(rename = "tag_name")]
    _tag_name: String,
    assets: Vec<GithubAsset>,
}

#[derive(Deserialize, Debug)]
struct GithubAsset {
    name: String,
    browser_download_url: String,
}

#[derive(Deserialize, Debug)]
struct MetadataAsset {
    #[serde(rename = "name")]
    _name: String,
    version: String,
    platform: String,
    url: String,
    flavor: Option<String>,
    shared: Option<bool>,
}

const PYTHON_BUILD_STANDALONE_REPO: &str = "astral-sh/python-build-standalone";
const EMBEDDED_PYTHON_METADATA: &str = include_str!("../../scripts/data/python_downloads.json");
const PYTHON_METADATA_ENV: &str = "WOVENSNAKE_PYTHON_ASSETS_JSON";
static METADATA_CACHE: OnceCell<Vec<MetadataAsset>> = OnceCell::new();

/// Ensures that a specific Python version is available.
/// If not found locally in the managed directory, it downloads it.
pub async fn ensure_python_version(version: &str) -> Result<PathBuf, Box<dyn Error>> {
    let managed_path = get_managed_python_path(version)?;

    // Try to find the executable in common locations within the managed path
    if let Some(exe) = find_executable_in_managed_path(&managed_path) {
        return Ok(exe);
    }

    println!("Python {} not found in managed storage. Downloading...", version);
    if !managed_path.exists() {
        fs::create_dir_all(&managed_path)?;
    }

    if let Err(e) = download_and_extract_python(version, &managed_path).await {
        // Clean up on failure
        let _ = fs::remove_dir_all(&managed_path);
        return Err(e);
    }

    find_executable_in_managed_path(&managed_path).ok_or_else(|| {
        format!(
            "Failed to find python executable after extraction in {}",
            managed_path.display()
        )
        .into()
    })
}

fn find_executable_in_managed_path(managed_path: &Path) -> Option<PathBuf> {
    let candidates = if cfg!(windows) {
        vec![
            managed_path.join("python").join("install").join("python.exe"),
            managed_path.join("install").join("python.exe"),
            managed_path.join("python.exe"),
        ]
    } else {
        vec![
            managed_path.join("python").join("install").join("bin").join("python3"),
            managed_path.join("install").join("bin").join("python3"),
            managed_path.join("bin").join("python3"),
            managed_path.join("python3"),
        ]
    };

    candidates.into_iter().find(|candidate| candidate.exists())
}

fn get_managed_python_path(version: &str) -> Result<PathBuf, Box<dyn Error>> {
    let home = dirs::home_dir().ok_or("Could not find home directory")?;
    let managed_base = home.join(".woven").join("python").join(version);
    Ok(managed_base)
}

/// Lists all Python versions currently managed by `WovenSnake`.
pub fn list_managed_versions() -> Result<Vec<String>, Box<dyn Error>> {
    let home = dirs::home_dir().ok_or("Could not find home directory")?;
    let managed_base = home.join(".woven").join("python");

    if !managed_base.exists() {
        return Ok(vec![]);
    }

    let mut versions = Vec::new();
    for entry in fs::read_dir(managed_base)? {
        let entry = entry?;
        if entry.path().is_dir() {
            if let Some(name) = entry.file_name().to_str() {
                versions.push(name.to_string());
            }
        }
    }
    Ok(versions)
}

/// Removes a specific managed Python version.
pub fn remove_managed_version(version: &str) -> Result<(), Box<dyn Error>> {
    let managed_path = get_managed_python_path(version)?;
    if managed_path.exists() {
        fs::remove_dir_all(managed_path)?;
    }
    Ok(())
}

/// Removes all managed Python versions.
pub fn clear_managed_versions() -> Result<(), Box<dyn Error>> {
    let home = dirs::home_dir().ok_or("Could not find home directory")?;
    let managed_base = home.join(".woven").join("python");
    if managed_base.exists() {
        fs::remove_dir_all(managed_base)?;
    }
    Ok(())
}

async fn download_and_extract_python(version: &str, dest: &Path) -> Result<(), Box<dyn Error>> {
    let urls = resolve_python_assets(version).await?;
    let mut response = None;
    let mut last_url = String::new();

    for url in urls {
        last_url.clone_from(&url);
        let client = reqwest::Client::builder().user_agent("wovensnake").build()?;
        match client.get(&url).send().await {
            Ok(res) if res.status().is_success() => {
                response = Some(res);
                break;
            }
            Ok(res) => {
                println!("  Notice: URL {} returned {}", url, res.status());
            }
            Err(e) => {
                println!("  Notice: Failed to connect to {}: {}", url, e);
            }
        }
    }

    let response = response.ok_or_else(|| {
        format!(
            "Failed to download Python for version {}. No matching assets found or all downloads failed.",
            version
        )
    })?;

    let total_size = response.content_length().unwrap_or(0);
    let pb = ProgressBar::new(total_size);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")?
            .progress_chars("#>-"),
    );

    let mut temp_file = tempfile::NamedTempFile::new()?;
    let mut stream = response.bytes_stream();

    while let Some(item) = stream.next().await {
        let chunk = item.map_err(|e| format!("Error downloading: {}", e))?;
        temp_file.write_all(&chunk)?;
        pb.inc(chunk.len() as u64);
    }
    pb.finish_with_message("Download complete");

    println!("Extracting Python...");
    let is_zip = Path::new(&last_url)
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("zip"));
    extract_archive(temp_file.path(), dest, is_zip)?;

    // Post-installation patches (inspired by uv)
    if let Err(e) = apply_post_install_patches(dest, version) {
        println!("  Warning: Failed to apply post-install patches: {}", e);
    }

    Ok(())
}

fn apply_post_install_patches(dest: &Path, version: &str) -> Result<(), Box<dyn Error>> {
    // 1. Create EXTERNALLY-MANAGED file (PEP 668)
    // This prevents users from accidentally using pip to modify this managed python
    let install_dir = find_install_dir(dest).ok_or("Could not find install directory")?;

    let lib_dir = if cfg!(windows) {
        install_dir.join("Lib")
    } else {
        // On Unix, it's usually lib/python3.x
        let major_minor = version.split('.').take(2).collect::<Vec<_>>().join(".");
        install_dir.join("lib").join(format!("python{}", major_minor))
    };

    if lib_dir.exists() {
        let em_path = lib_dir.join("EXTERNALLY-MANAGED");
        if !em_path.exists() {
            let content = "[externally-managed]\nError=This Python installation is managed by WovenSnake.\n";
            fs::write(em_path, content)?;
        }
    }

    // 2. Ensure canonical executables (symlinks/copies)
    if cfg!(windows) {
        // On Windows: ensure python3.x.exe exists alongside python.exe
        let python_exe = install_dir.join("python.exe");
        if python_exe.exists() {
            let major_minor = version.split('.').take(2).collect::<Vec<_>>().join(".");
            let python_ver_exe = install_dir.join(format!("python{}.exe", major_minor));
            if !python_ver_exe.exists() {
                fs::copy(&python_exe, &python_ver_exe)?;
            }
        }
    } else {
        // On Unix: ensure 'python' symlink exists alongside 'python3'
        let bin_dir = install_dir.join("bin");
        let python3 = bin_dir.join("python3");
        let python = bin_dir.join("python");
        if python3.exists() && !python.exists() {
            #[cfg(unix)]
            {
                let _ = std::os::unix::fs::symlink(&python3, &python);
            }
        }
    }

    Ok(())
}

fn find_install_dir(base: &Path) -> Option<PathBuf> {
    // python-build-standalone usually extracts to a 'python' folder, then 'install'
    let candidates = vec![
        base.join("python").join("install"),
        base.join("install"),
        base.to_path_buf(),
    ];

    for c in candidates {
        if c.join(if cfg!(windows) { "python.exe" } else { "bin/python3" })
            .exists()
        {
            return Some(c);
        }
    }
    None
}

fn get_metadata_entries() -> Result<&'static Vec<MetadataAsset>, Box<dyn Error>> {
    METADATA_CACHE.get_or_try_init(load_metadata_entries)
}

fn load_metadata_entries() -> Result<Vec<MetadataAsset>, Box<dyn Error>> {
    if let Ok(path) = env::var(PYTHON_METADATA_ENV) {
        let content = fs::read_to_string(path)?;
        let parsed: Vec<MetadataAsset> = serde_json::from_str(&content)?;
        return Ok(parsed);
    }
    let parsed: Vec<MetadataAsset> = serde_json::from_str(EMBEDDED_PYTHON_METADATA)?;
    Ok(parsed)
}

fn resolve_from_metadata(version: &str, platform_key: &str) -> Result<Option<Vec<String>>, Box<dyn Error>> {
    let entries = get_metadata_entries()?;
    let mut matches = Vec::new();
    for asset in entries {
        if !asset.platform.starts_with(platform_key) {
            continue;
        }

        let score = version_match_score(&asset.version, version);
        if score == 0 {
            continue;
        }

        let flavor_bonus = if asset.flavor.as_deref() == Some("install_only") {
            20
        } else {
            0
        };
        let shared_bonus = if asset.shared.unwrap_or(false) { 15 } else { 0 };
        matches.push((score + flavor_bonus + shared_bonus, asset.url.clone()));
    }

    if matches.is_empty() {
        return Ok(None);
    }

    matches.sort_by(|a, b| b.0.cmp(&a.0));
    let urls: Vec<String> = matches.into_iter().map(|(_, url)| url).collect();
    println!(
        "  INFO: Using embedded metadata catalog ({} entries) for platform {}.",
        urls.len(),
        platform_key
    );
    Ok(Some(urls))
}

fn version_match_score(asset_version: &str, requested_version: &str) -> i32 {
    if asset_version == requested_version {
        return 1000;
    }
    if asset_version.starts_with(&format!("{}.", requested_version)) {
        return 600;
    }
    if requested_version.starts_with(asset_version) {
        return 500;
    }
    let req_parts: Vec<_> = requested_version.split('.').collect();
    let asset_parts: Vec<_> = asset_version.split('.').collect();
    if req_parts.len() >= 2
        && asset_parts.len() >= 2
        && req_parts[0] == asset_parts[0]
        && req_parts[1] == asset_parts[1]
    {
        return 300;
    }
    0
}

async fn resolve_python_assets(version: &str) -> Result<Vec<String>, Box<dyn Error>> {
    let os = env::consts::OS;
    let arch = env::consts::ARCH;

    let platform_key = match (os, arch) {
        ("windows", "x86_64") => "x86_64-pc-windows-msvc",
        ("linux", "x86_64") => "x86_64-unknown-linux",
        ("linux", "aarch64") => "aarch64-unknown-linux",
        ("macos", "x86_64") => "x86_64-apple-darwin",
        ("macos", "aarch64") => "aarch64-apple-darwin",
        _ => return Err(format!("Unsupported platform: {} {}", os, arch).into()),
    };

    // Try to load from local cache first
    if let Ok(cached_urls) = load_cached_assets(version, platform_key) {
        if !cached_urls.is_empty() {
            return Ok(cached_urls);
        }
    }

    if let Some(metadata_urls) = resolve_from_metadata(version, platform_key)? {
        return Ok(metadata_urls);
    }

    println!("Resolving Python {} for {} via GitHub API...", version, platform_key);

    let client = reqwest::Client::builder().user_agent("wovensnake").build()?;

    // We fetch releases page by page until we find at least one matching asset
    let mut all_assets = Vec::new();
    for page in 1..=5 {
        let url = format!(
            "https://api.github.com/repos/{}/releases?page={}",
            PYTHON_BUILD_STANDALONE_REPO, page
        );

        let mut request = client.get(&url);
        if let Ok(token) = env::var("GITHUB_TOKEN") {
            request = request.header("Authorization", format!("token {}", token));
        }

        let releases: Vec<GithubRelease> = match request.send().await {
            Ok(res) => {
                if res.status() == 403 {
                    return Err("GitHub API rate limit exceeded. Please try again later or set a GITHUB_TOKEN.".into());
                }
                if !res.status().is_success() {
                    println!(
                        "  Warning: GitHub API returned {} on page {}. Body will be skipped.",
                        res.status(),
                        page
                    );
                    break;
                }
                res.json().await?
            }
            Err(e) => {
                println!("  Warning: Failed to fetch releases from GitHub (page {}): {}", page, e);
                break;
            }
        };

        if releases.is_empty() {
            break;
        }

        let mut found_in_page = false;
        for release in releases {
            for asset in release.assets {
                let name = asset.name.to_lowercase();
                let version_prefix = format!("cpython-{}", version);

                if name.starts_with(&version_prefix) && name.contains(platform_key) {
                    let score = if name.contains("install_only") { 100 } else { 0 }
                        + if name.contains("shared") { 50 } else { 0 }
                        + if name.contains("gnu") && os == "linux" { 25 } else { 0 };

                    all_assets.push((score, asset.browser_download_url));
                    found_in_page = true;
                }
            }
        }

        // If we found matches in this page, we probably don't need to look further
        // unless we want to be absolutely sure we have the "best" one, but usually
        // the latest releases are in the first pages.
        if found_in_page {
            break;
        }
    }

    // Sort by score descending
    all_assets.sort_by(|a, b| b.0.cmp(&a.0));

    let urls: Vec<String> = all_assets.into_iter().map(|(_, url)| url).collect();

    if urls.is_empty() {
        return Err(format!(
            "Could not find any Python assets for version {} on platform {}",
            version, platform_key
        )
        .into());
    }

    // Save to cache
    let _ = save_cached_assets(version, platform_key, &urls);

    Ok(urls)
}

fn get_cache_path() -> Result<PathBuf, Box<dyn Error>> {
    let home = dirs::home_dir().ok_or("Could not find home directory")?;
    let cache_dir = home.join(".woven").join("cache");
    if !cache_dir.exists() {
        fs::create_dir_all(&cache_dir)?;
    }
    Ok(cache_dir.join("python_assets.json"))
}

fn load_cached_assets(version: &str, platform: &str) -> Result<Vec<String>, Box<dyn Error>> {
    let path = get_cache_path()?;
    if !path.exists() {
        return Ok(vec![]);
    }

    let content = fs::read_to_string(path)?;
    let cache: serde_json::Value = serde_json::from_str(&content)?;

    let key = format!("{}-{}", version, platform);
    if let Some(urls) = cache.get(&key) {
        let urls: Vec<String> = serde_json::from_value(urls.clone())?;
        return Ok(urls);
    }

    Ok(vec![])
}

fn save_cached_assets(version: &str, platform: &str, urls: &[String]) -> Result<(), Box<dyn Error>> {
    let path = get_cache_path()?;
    let mut cache: serde_json::Map<String, serde_json::Value> = if path.exists() {
        let content = fs::read_to_string(&path)?;
        serde_json::from_str(&content).unwrap_or_default()
    } else {
        serde_json::Map::new()
    };

    let key = format!("{}-{}", version, platform);
    cache.insert(key, serde_json::to_value(urls)?);

    let content = serde_json::to_string_pretty(&cache)?;
    fs::write(path, content)?;
    Ok(())
}

fn extract_archive(archive_path: &Path, dest: &Path, is_zip: bool) -> Result<(), Box<dyn Error>> {
    let file = fs::File::open(archive_path)?;

    if is_zip {
        let mut archive = ZipArchive::new(file)?;
        for i in 0..archive.len() {
            let mut file = archive.by_index(i)?;
            let outpath = match file.enclosed_name() {
                Some(path) => dest.join(path),
                None => continue,
            };

            if (*file.name()).ends_with('/') {
                fs::create_dir_all(&outpath)?;
            } else {
                if let Some(p) = outpath.parent() {
                    if !p.exists() {
                        fs::create_dir_all(p)?;
                    }
                }
                let mut outfile = fs::File::create(&outpath)?;
                io::copy(&mut file, &mut outfile)?;
            }
        }
    } else {
        let tar = GzDecoder::new(file);
        let mut archive = Archive::new(tar);
        archive.unpack(dest)?;
    }

    Ok(())
}