Skip to content

Commit 5ee1cf6

Browse files
PY-27874 Requirements.txt: Support requirements with hashes
Signed-off-by: Nikita.Ashihmin <nikita.ashihmin@jetbrains.com> GitOrigin-RevId: e9e6f60392cdc50e42182cdfe2dc7b66c7b70d7a
1 parent e303ea7 commit 5ee1cf6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

python/python-psi-impl/src/com/jetbrains/python/packaging/PyRequirementParser.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ public final class PyRequirementParser {
152152
private static final @NotNull String REQUIREMENT_OPTIONS_GROUP = "options";
153153

154154
private static final @NotNull String REQUIREMENT_OPTIONS_REGEXP =
155-
"(?<" + REQUIREMENT_OPTIONS_GROUP + ">(" + LINE_WS_REGEXP + "+(--global-option|--install-option)=\"[^\"]*\")+)?";
155+
"(?<" +
156+
REQUIREMENT_OPTIONS_GROUP +
157+
">(" +
158+
LINE_WS_REGEXP +
159+
"+(--global-option|--install-option)=\"[^\"]*\"|" +
160+
LINE_WS_REGEXP +
161+
"+--hash=[^\\s]+)+)?";
156162

157163
private static final @NotNull String REQUIREMENT_GROUP = "requirement";
158164

python/testSrc/com/jetbrains/python/packaging/PyRequirementTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,30 @@ public void testCommentAtTheEnd() {
24332433
assertEquals(singletonList(requirement), PyRequirementParser.fromText(name + " \\\n--install-option=\"option\" # comment"));
24342434
}
24352435

2436+
// HASH OPTIONS
2437+
// Test for parsing requirements with hash options
2438+
public void testRequirementWithHash() {
2439+
doTest("certifi", "2018.4.16", "certifi==2018.4.16 --hash=sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7");
2440+
doTest("certifi", "2018.4.16",
2441+
"certifi==2018.4.16 --hash=sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7 --hash=sha256:9fa520c1bacfb634fa7af20a76bcbd3d5fb390481724c597da32c719a7dca4b0");
2442+
}
2443+
2444+
// Test for parsing requirements with hash options from text (including line continuation)
2445+
public void testRequirementWithHashFromText() {
2446+
final List<PyRequirement> requirements = PyRequirementParser.fromText(
2447+
"certifi==2018.4.16 \\\n --hash=sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7 \\\n --hash=sha256:9fa520c1bacfb634fa7af20a76bcbd3d5fb390481724c597da32c719a7dca4b0");
2448+
assertFalse(requirements.isEmpty());
2449+
2450+
final PyRequirement requirement = requirements.get(0);
2451+
assertNotNull(requirement);
2452+
assertEquals("certifi", requirement.getName());
2453+
assertEquals("2018.4.16", requirement.getVersionSpecs().get(0).getVersion());
2454+
2455+
final List<String> installOptions = requirement.getInstallOptions();
2456+
assertTrue(installOptions.contains("--hash=sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7"));
2457+
assertTrue(installOptions.contains("--hash=sha256:9fa520c1bacfb634fa7af20a76bcbd3d5fb390481724c597da32c719a7dca4b0"));
2458+
}
2459+
24362460
// ENV MARKERS
24372461
// TODO: https://www.python.org/dev/peps/pep-0426/#environment-markers, https://www.python.org/dev/peps/pep-0508/#environment-markers
24382462

0 commit comments

Comments
 (0)