posy 0.0.1

[wip] Python installer and package manager
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
use crate::prelude::*;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum SdistFormat {
    Zip,
    TarGz,
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct SdistName {
    pub distribution: PackageName,
    pub version: Version,
    pub format: SdistFormat,
}

impl TryFrom<&str> for SdistName {
    type Error = eyre::Report;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        static SDIST_NAME_RE: Lazy<Regex> =
            Lazy::new(|| Regex::new(r"^(.*)-([^-]*)\.(zip|tar\.gz)$").unwrap());

        match SDIST_NAME_RE.captures(&value) {
            None => bail!("invalid sdist name"),
            Some(captures) => {
                let distribution: PackageName =
                    captures.get(1).unwrap().as_str().parse()?;
                let version: Version = captures.get(2).unwrap().as_str().parse()?;
                let format = match captures.get(3).unwrap().as_str() {
                    "zip" => SdistFormat::Zip,
                    "tar.gz" => SdistFormat::TarGz,
                    _ => unreachable!(),
                };
                Ok(SdistName {
                    distribution,
                    version,
                    format,
                })
            }
        }
    }
}

try_from_str_boilerplate!(SdistName);

impl Display for SdistName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}-{}.{}",
            self.distribution.as_given(),
            self.version,
            match self.format {
                SdistFormat::Zip => "zip",
                SdistFormat::TarGz => "tar.gz",
            }
        )
    }
}

// https://packaging.python.org/specifications/binary-distribution-format/#file-name-convention
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct WheelName {
    pub distribution: PackageName,
    pub version: Version,
    // According to the spec, the build tag "sorts as an empty tuple if
    // unspecified, else sort as a two-item tuple with the first item being
    // the initial digits as an 'int', and the second item being the remainder
    // of the tag as a 'str'". This is ill-defined, b/c what the heck do
    // you do if there aren't any initial digits? So instead we do:
    //
    //   <no build tag> => (None, "")
    //   1              => (Some(1), "")
    //   1stuff         => (Some(1), "stuff")
    //   stuff          => (None, "stuff")
    //
    // And it sorts like a tuple, where None sorts ahead of Some.
    pub build_number: Option<u32>,
    pub build_name: String,
    // Should probably be OrderedSet, but there isn't one in the stdlib. Vec
    // takes less memory than a set, plus it preserves ordering, which is
    // somewhat nice, and removing duplicates doesn't really matter anyway
    // (.all_tags() will remove them too).
    pub py_tags: Vec<String>,
    pub abi_tags: Vec<String>,
    pub arch_tags: Vec<String>,
}

pub trait BinaryName {
    fn all_tags(&self) -> HashSet<String>;
}

impl BinaryName for WheelName {
    fn all_tags(&self) -> HashSet<String> {
        let mut retval = HashSet::new();
        for py in &self.py_tags {
            for abi in &self.abi_tags {
                for arch in &self.arch_tags {
                    retval.insert(format!("{}-{}-{}", py, abi, arch));
                }
            }
        }
        retval
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct PybiName {
    pub distribution: PackageName,
    pub version: Version,
    pub build_number: Option<u32>,
    pub build_name: String,
    pub arch_tags: Vec<String>,
}

impl BinaryName for PybiName {
    fn all_tags(&self) -> HashSet<String> {
        self.arch_tags.iter().cloned().collect()
    }
}

fn generic_parse<'a>(
    value: &'a str,
    suffix: &str,
    tag_parts: u8,
) -> Result<(PackageName, Version, Option<u32>, String, Vec<Vec<String>>)> {
    static VALID_CHARS: Lazy<Regex> =
        Lazy::new(|| Regex::new(r"^[A-Za-z0-9_.+!-]*$").unwrap());

    // names/versions/etc. will be further validated by their respective
    // constructors. This is just to rule out real ridiculous stuff, like
    // spaces or control characters. I *think* this includes all the
    // characters that might reasonably appear in a package
    // name/version/build tag? There isn't actually a spec here, so we'll
    // see.
    if !VALID_CHARS.is_match(value) {
        bail!("Invalid characters in {} name: {:?}", suffix, value);
    }
    let stem = value
        .strip_suffix(suffix)
        .ok_or_else(|| eyre!("expected {:?} to end in .{}", value, suffix))?;

    let mut pieces: Vec<&str> = stem.split('-').collect();

    static BUILD_TAG_SPLIT: Lazy<Regex> =
        Lazy::new(|| Regex::new(r"(^[0-9]*)(.*)$").unwrap());

    let build_number: Option<u32>;
    let build_name: String;
    if pieces.len() == 3 + tag_parts as usize {
        let build_tag = pieces.remove(2);
        if build_tag == "" {
            bail!("found empty build tag: {:?}", value);
        }
        // unwrap safe because: the regex cannot fail
        let captures = BUILD_TAG_SPLIT.captures(build_tag).unwrap();
        build_number = captures.get(1).map(|m| m.as_str().parse().ok()).flatten();
        // unwrap safe because: this group will always match something, even
        // if only the empty string
        build_name = captures.get(2).unwrap().as_str().into();
    } else {
        build_number = None;
        build_name = "".to_owned();
    }

    if pieces.len() != 2 + tag_parts as usize {
        bail!("can't parse binary name '{value}'");
    }

    let distribution: PackageName = pieces[0].try_into()?;
    let version: Version = pieces[1].try_into()?;
    let tag_sets: Vec<Vec<String>> = pieces[2..]
        .into_iter()
        .map(|compressed_tag| compressed_tag.split(".").map(|tag| tag.into()).collect())
        .collect();

    Ok((distribution, version, build_number, build_name, tag_sets))
}

fn format_build_tag(build_number: Option<u32>, build_name: &str) -> String {
    match (build_number, &build_name[..]) {
        (None, "") => String::from(""),
        (None, name) => format!("-{}", name),
        (Some(num), name) => format!("-{}{}", num, name),
    }
}

impl TryFrom<&str> for WheelName {
    type Error = eyre::Report;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let (distribution, version, build_number, build_name, mut tag_sets) =
            generic_parse(value, ".whl", 3)?;

        tag_sets.reverse();

        Ok(WheelName {
            distribution,
            version,
            build_number,
            build_name,
            py_tags: tag_sets.pop().unwrap(),
            abi_tags: tag_sets.pop().unwrap(),
            arch_tags: tag_sets.pop().unwrap(),
        })
    }
}

try_from_str_boilerplate!(WheelName);

impl TryFrom<&str> for PybiName {
    type Error = eyre::Report;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let (distribution, version, build_number, build_name, mut tag_sets) =
            generic_parse(value, ".pybi", 1)?;

        Ok(PybiName {
            distribution,
            version,
            build_number,
            build_name,
            arch_tags: tag_sets.pop().unwrap(),
        })
    }
}

try_from_str_boilerplate!(PybiName);

impl Display for WheelName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{dist}-{ver}{build}-{py_tags}-{abi_tags}-{arch_tags}.whl",
            dist = self.distribution.as_given(),
            ver = self.version,
            build = format_build_tag(self.build_number, &self.build_name),
            py_tags = self.py_tags.join("."),
            abi_tags = self.abi_tags.join("."),
            arch_tags = self.arch_tags.join("."),
        )
    }
}

impl Display for PybiName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{dist}-{ver}{build}-{arch_tags}.pybi",
            dist = self.distribution.as_given(),
            ver = self.version,
            build = format_build_tag(self.build_number, &self.build_name),
            arch_tags = self.arch_tags.join("."),
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, SerializeDisplay)]
pub enum ArtifactName {
    Sdist(SdistName),
    Wheel(WheelName),
    Pybi(PybiName),
}

impl ArtifactName {
    pub fn distribution(&self) -> &PackageName {
        match self {
            ArtifactName::Sdist(inner) => &inner.distribution,
            ArtifactName::Wheel(inner) => &inner.distribution,
            ArtifactName::Pybi(inner) => &inner.distribution,
        }
    }

    pub fn version(&self) -> &Version {
        match self {
            ArtifactName::Sdist(inner) => &inner.version,
            ArtifactName::Wheel(inner) => &inner.version,
            ArtifactName::Pybi(inner) => &inner.version,
        }
    }

    pub fn inner_as<T: Clone + UnwrapFromArtifactName>(&self) -> Option<&T> {
        T::try_unwrap_from(self)
    }

    // A pybi with multiple tags, or with a multi-platform tag like universal2, can be
    // treated as multiple single-platform pybis that we select between. This expands a
    // single multi-platform pybi name into multiple single-platform pybi names.
    pub fn split_multiplatform_pybis(&self) -> Vec<ArtifactName> {
        match self {
            ArtifactName::Pybi(name) => {
                name.arch_tags
                    .iter()
                    .flat_map(
                        // expand universal2 into individual tags
                        |tag| {
                            if tag.starts_with("macosx_")
                                && tag.ends_with("_universal2")
                            {
                                let prefix = tag.strip_suffix("_universal2").unwrap();
                                vec![
                                    format!("{prefix}_arm64"),
                                    format!("{prefix}_x86_64"),
                                ]
                            } else {
                                vec![tag.to_string()]
                            }
                        },
                    )
                    .map(|tag| {
                        ArtifactName::Pybi(PybiName {
                            arch_tags: vec![tag],
                            ..name.clone()
                        })
                    })
                    .collect()
            }
            _ => vec![self.clone()],
        }
    }
}

pub trait UnwrapFromArtifactName {
    fn try_unwrap_from(value: &ArtifactName) -> Option<&Self>;
}

macro_rules! impl_unwrap {
    ($enum:ident, $type:ty) => {
        impl UnwrapFromArtifactName for $type {
            fn try_unwrap_from(value: &ArtifactName) -> Option<&$type> {
                if let ArtifactName::$enum(inner) = value {
                    Some(inner)
                } else {
                    None
                }
            }
        }
    };
}

impl_unwrap!(Sdist, SdistName);
impl_unwrap!(Wheel, WheelName);
impl_unwrap!(Pybi, PybiName);

impl TryFrom<&str> for ArtifactName {
    type Error = eyre::Report;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        if value.ends_with(".whl") {
            Ok(ArtifactName::Wheel(value.try_into()?))
        } else if value.ends_with(".pybi") {
            Ok(ArtifactName::Pybi(value.try_into()?))
        } else {
            Ok(ArtifactName::Sdist(value.try_into()?))
        }
    }
}

impl Display for ArtifactName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ArtifactName::Sdist(inner) => write!(f, "{}", &inner),
            ArtifactName::Wheel(inner) => write!(f, "{}", &inner),
            ArtifactName::Pybi(inner) => write!(f, "{}", &inner),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_sdist_name_from_str() {
        let sn: SdistName = "trio-0.19a0.tar.gz".try_into().unwrap();
        assert_eq!(sn.distribution, "trio".try_into().unwrap());
        assert_eq!(sn.version, "0.19a0".try_into().unwrap());
    }

    #[test]
    fn test_wheel_name_from_str() {
        let n: WheelName = "trio-0.18.0-py3-none-any.whl".try_into().unwrap();
        assert_eq!(n.distribution, "trio".try_into().unwrap());
        assert_eq!(n.version, "0.18.0".try_into().unwrap());
        assert_eq!(n.build_number, None);
        assert_eq!(n.build_name, "");
        assert_eq!(n.py_tags, vec!["py3"]);
        assert_eq!(n.abi_tags, vec!["none"]);
        assert_eq!(n.arch_tags, vec!["any"]);

        assert_eq!(n.to_string(), "trio-0.18.0-py3-none-any.whl");
    }

    #[test]
    fn test_wheel_name_from_str_harder() {
        let n: WheelName = "foo.bar-0.1b3-1local-py2.py3-none-any.whl"
            .try_into()
            .unwrap();
        assert_eq!(n.distribution, "foo.bar".try_into().unwrap());
        assert_eq!(n.version, "0.1b3".try_into().unwrap());
        assert_eq!(n.build_number, Some(1));
        assert_eq!(n.build_name, "local");
        assert_eq!(n.py_tags, vec!["py2", "py3"],);
        assert_eq!(n.abi_tags, vec!["none"]);
        assert_eq!(n.arch_tags, vec!["any"]);

        assert_eq!(
            n.all_tags(),
            vec!["py2-none-any", "py3-none-any"]
                .into_iter()
                .map(|s| s.to_owned())
                .collect()
        );

        assert_eq!(n.to_string(), "foo.bar-0.1b3-1local-py2.py3-none-any.whl");
    }

    #[test]
    fn test_pybi_name_from_str() {
        let n: PybiName = "cpython-3.10b1-manylinux_2_17_x86_64.pybi"
            .try_into()
            .unwrap();
        assert_eq!(n.distribution, "cpython".try_into().unwrap());
        assert_eq!(n.version, "3.10b1".try_into().unwrap());
        assert_eq!(n.build_number, None);
        assert_eq!(n.build_name, "");
        assert_eq!(n.arch_tags, vec!["manylinux_2_17_x86_64"]);

        assert_eq!(n.to_string(), "cpython-3.10b1-manylinux_2_17_x86_64.pybi");
    }

    #[test]
    fn test_pybi_name_from_str_harder() {
        let n: PybiName = "foo.bar-0.1b3-1local-win32.win_amd64.pybi"
            .try_into()
            .unwrap();
        assert_eq!(n.distribution, "foo.bar".try_into().unwrap());
        assert_eq!(n.version, "0.1b3".try_into().unwrap());
        assert_eq!(n.build_number, Some(1));
        assert_eq!(n.build_name, "local");
        assert_eq!(n.arch_tags, vec!["win32", "win_amd64"]);

        assert_eq!(n.to_string(), "foo.bar-0.1b3-1local-win32.win_amd64.pybi");
    }
}