Skip to content
This repository was archived by the owner on May 5, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Pattern;

import static java.util.Objects.requireNonNull;
import static org.sonatype.goodies.packageurl.PackageUrlParser.parseNamespace;
Expand All @@ -38,6 +39,8 @@
*/
public class PackageUrlBuilder
{
private static final Pattern PYPI_NORMALIZED_NAME_PATTERN = Pattern.compile("[-_.]+");

private boolean typeSpecificTransformations = true;

private String type;
Expand Down Expand Up @@ -188,7 +191,8 @@ PackageUrl buildAndValidate(final boolean validate) {
break;

case "pypi":
correctedName = name.replace('_', '-');
// Equivalent of: https://peps.python.org/pep-0503/#normalized-names
correctedName = PYPI_NORMALIZED_NAME_PATTERN.matcher(name).replaceAll("-");
correctedName = MoreStrings.lowerCase(correctedName);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ class PackageUrlBuilderTest

@Test
void 'pypi name'() {
PackageUrlBuilder builder = new PackageUrlBuilder().type('pypi').name('fOo-BaR_baZ')
PackageUrlBuilder builder = new PackageUrlBuilder().type('pypi').name('fOo--BaR_.baZ')
builder.build().with {
assert name == 'foo-bar-baz'
}
builder.typeSpecificTransformations(false).build().with {
assert name == 'fOo-BaR_baZ'
assert name == 'fOo--BaR_.baZ'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,28 @@ class PackageUrlParserTest
assert version == '1.11.1'
}

parse('pkg:pypi/p2.Loader@0.1.0').with {
assert type == 'pypi'
assert name == 'p2-loader'
assert version == '0.1.0'
}
parseAsIs('pkg:pypi/p2.Loader@0.1.0').with {
assert type == 'pypi'
assert name == 'p2.Loader'
assert version == '0.1.0'
}

parse('pkg:pypi/cmc-csci046-.data-structures@1.0.7').with {
assert type == 'pypi'
assert name == 'cmc-csci046-data-structures'
assert version == '1.0.7'
}
parseAsIs('pkg:pypi/cmc-csci046-.data-structures@1.0.7').with {
assert type == 'pypi'
assert name == 'cmc-csci046-.data-structures'
assert version == '1.0.7'
}

parse('pkg:rpm/fedora/curl@7.50.3-1.fc25?arch=i386&distro=fedora-25').with {
assert type == 'rpm'
assert namespace == ['fedora']
Expand Down