Skip to content

Commit 9a615dc

Browse files
authored
Merge pull request #215 from zvezdan/fix-wheel-regex
Fix the regex for wheel file name format.
2 parents 946276c + bc6c1ef commit 9a615dc

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/wheel/FileBackedWheelCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Optional<File> findWheel(String library, String version, File pythonExecu
8585

8686
logger.info("Found artifacts: {}", foundWheel);
8787

88-
return foundWheel.map(it -> it.file);
88+
return foundWheel.map(it -> it.getFile());
8989
}
9090

9191
public File getCacheDir() {
@@ -95,8 +95,8 @@ public File getCacheDir() {
9595
private boolean wheelMatches(File pythonExecutable, PythonWheelDetails wheelDetails) {
9696
return supportedWheelFormats.matchesSupportedVersion(
9797
pythonExecutable,
98-
wheelDetails.pythonTag,
99-
wheelDetails.abiTag,
100-
wheelDetails.platformTag);
98+
wheelDetails.getPythonTag(),
99+
wheelDetails.getAbiTag(),
100+
wheelDetails.getPlatformTag());
101101
}
102102
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/wheel/PythonWheelDetails.java

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,36 @@
2323
public class PythonWheelDetails {
2424

2525
// PEP-0427
26-
public static final String WHEEL_FILE_FORMAT = "(?<dist>.*?)-(?<version>.*?)(-(.*?))?-(?<pythonTag>.*?)-(?<abiTag>.*?)-(?<platformTag>.*?).whl";
27-
28-
final File file;
29-
final String dist;
30-
final String version;
31-
final String pythonTag;
32-
final String abiTag;
33-
final String platformTag;
34-
35-
public PythonWheelDetails(File file, Matcher matcher) {
36-
this.file = file;
37-
this.dist = matcher.group("dist");
38-
this.version = matcher.group("version");
39-
this.pythonTag = matcher.group("pythonTag");
40-
this.abiTag = matcher.group("abiTag");
41-
this.platformTag = matcher.group("platformTag");
26+
private static final String WHEEL_FILE_FORMAT = "(?<dist>.+?)-(?<version>\\d.*?)(-(\\d.*?))?-(?<pythonTag>.+?)-(?<abiTag>.+?)-(?<platformTag>.+?).whl";
27+
28+
private static final Pattern WHEEL_PATTERN = Pattern.compile(WHEEL_FILE_FORMAT);
29+
private static final Pattern SNAPSHOT_PATTERN = Pattern.compile("_(?<snapshot>[A-Z]+)$");
30+
31+
private final File file;
32+
private final String dist;
33+
private final String version;
34+
private final String pythonTag;
35+
private final String abiTag;
36+
private final String platformTag;
37+
38+
private PythonWheelDetails(File wheelFile, Matcher matcher) {
39+
file = wheelFile;
40+
dist = matcher.group("dist");
41+
String matchedVersion = matcher.group("version");
42+
pythonTag = matcher.group("pythonTag");
43+
abiTag = matcher.group("abiTag");
44+
platformTag = matcher.group("platformTag");
45+
46+
Matcher snapshotMatcher = SNAPSHOT_PATTERN.matcher(matchedVersion);
47+
if (snapshotMatcher.find()) {
48+
version = snapshotMatcher.replaceFirst("-" + snapshotMatcher.group("snapshot"));
49+
} else {
50+
version = matchedVersion;
51+
}
52+
}
53+
54+
public File getFile() {
55+
return file;
4256
}
4357

4458
public String getDist() {
@@ -49,6 +63,18 @@ public String getVersion() {
4963
return version;
5064
}
5165

66+
public String getPythonTag() {
67+
return pythonTag;
68+
}
69+
70+
public String getAbiTag() {
71+
return abiTag;
72+
}
73+
74+
public String getPlatformTag() {
75+
return platformTag;
76+
}
77+
5278
@Override
5379
public String toString() {
5480
return "PythonWheelDetails{"
@@ -61,9 +87,8 @@ public String toString() {
6187
+ '}';
6288
}
6389

64-
static public Optional<PythonWheelDetails> fromFile(File file) {
65-
Pattern wheelPattern = Pattern.compile(WHEEL_FILE_FORMAT);
66-
Matcher matcher = wheelPattern.matcher(file.getName());
90+
public static Optional<PythonWheelDetails> fromFile(File file) {
91+
Matcher matcher = WHEEL_PATTERN.matcher(file.getName());
6792
if (!matcher.matches()) {
6893
return Optional.empty();
6994
}

pygradle-plugin/src/test/groovy/com/linkedin/gradle/python/wheel/PythonWheelDetailsTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class PythonWheelDetailsTest extends Specification {
3333

3434
def 'can parse wheel file with SNAPSHOT version'() {
3535
when:
36-
def wheelFile = new File("foo-1.0.0-SNAPSHOT-py2-none-any.whl")
36+
def wheelFile = new File("foo-1.0.0_SNAPSHOT-py2-none-any.whl")
3737
PythonWheelDetails pythonWheelDetails = PythonWheelDetails.fromFile(wheelFile).get()
3838
then:
3939
pythonWheelDetails.dist == "foo"
40-
pythonWheelDetails.version == "1.0.0"
40+
pythonWheelDetails.version == "1.0.0-SNAPSHOT"
4141
pythonWheelDetails.pythonTag == "py2"
4242
pythonWheelDetails.abiTag == "none"
4343
pythonWheelDetails.platformTag == "any"

0 commit comments

Comments
 (0)