Skip to content

Commit

Permalink
Merge pull request #432 from joebonrichie/pypi-matching
Browse files Browse the repository at this point in the history
boulder: Pypi matching
  • Loading branch information
ikeycode authored Feb 22, 2025
2 parents 8c40556 + 4fbebd7 commit 39fb5ff
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
16 changes: 14 additions & 2 deletions boulder/src/draft/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::Upstream;

mod basic;
mod github;
mod pypi;

#[derive(Default)]
pub struct Metadata {
Expand All @@ -20,6 +21,7 @@ pub struct Source {
pub name: String,
pub version: String,
pub homepage: String,
pub uri: String,
}

impl Metadata {
Expand All @@ -32,6 +34,7 @@ impl Metadata {
if let Some(matched) = match matcher {
Matcher::Basic => basic::source(&upstream.uri),
Matcher::Github => github::source(&upstream.uri),
Matcher::Pypi => pypi::source(&upstream.uri),
} {
source = matched;
break;
Expand All @@ -45,16 +48,25 @@ impl Metadata {
pub fn upstreams(&self) -> String {
self.upstreams
.iter()
.map(|Upstream { uri, hash }| format!(" - {uri} : {hash}"))
.enumerate()
.map(|(i, Upstream { uri, hash })| {
let uri_to_use = if i == 0 && !self.source.uri.is_empty() {
&self.source.uri
} else {
uri.as_str()
};
format!(" - {uri_to_use} : {hash}")
})
.join("\n")
}
}

enum Matcher {
Basic,
Github,
Pypi,
}

impl Matcher {
const ALL: &'static [Self] = &[Self::Github, Self::Basic];
const ALL: &'static [Self] = &[Self::Github, Self::Pypi, Self::Basic];
}
1 change: 1 addition & 0 deletions boulder/src/draft/metadata/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ pub fn source(upstream: &Url) -> Option<Source> {
name,
version,
homepage: homepage.to_owned(),
uri: upstream.to_string(),
})
}
1 change: 1 addition & 0 deletions boulder/src/draft/metadata/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn source(upstream: &Url) -> Option<Source> {
name: project.to_lowercase(),
version,
homepage: format!("https://github.com/{owner}/{project}"),
uri: upstream.to_string(),
});
}

Expand Down
37 changes: 37 additions & 0 deletions boulder/src/draft/metadata/pypi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright © 2020-2025 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0

use regex::Regex;
use url::Url;

use crate::util;

use super::Source;

pub fn source(upstream: &Url) -> Option<Source> {
let regex = Regex::new(
r"^https://files\.pythonhosted\.org/packages/[a-f0-9]{2}/[a-f0-9]{2}/[a-f0-9]+/([^/]+)-([\d.]+)\.tar\.gz$",
)
.ok()?;

let filename = util::uri_file_name(upstream);

let captures = regex.captures(upstream.as_str())?;

let mut name = captures.get(1)?.as_str().to_owned();
let version = captures.get(2)?.as_str().to_owned();

let first_char = &name.chars().next().unwrap_or_default();

if !name.starts_with("python-") {
name = format!("python-{name}");
}

Some(Source {
name: name.to_owned(),
version,
homepage: format!("https://pypi.org/project/{name}"),
uri: format!("https://files.pythonhosted.org/packages/source/{first_char}/{name}/{filename}"),
})
}

0 comments on commit 39fb5ff

Please sign in to comment.