Skip to content

Commit

Permalink
feat: handle hashes without error (thanks to @dymart via #67) (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Horton <paul.horton@owasp.org>
  • Loading branch information
madpah authored Apr 3, 2024
1 parent 9227900 commit a9aafc9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
5 changes: 4 additions & 1 deletion requirements/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def parse(reqstr: Union[str, TextIO]) -> Iterator[Requirement]:
elif not line or line.startswith('#'):
# comments are lines that start with # only
continue
elif line.startswith('-r') or line.startswith('--requirement'):
elif not line or line.startswith('--hash='):
# hashes are lines that start with --hash=
continue
elif line.startswith(('-r', '--requirement')):
_, new_filename = line.split()
new_file_path = os.path.join(os.path.dirname(filename or '.'),
new_filename)
Expand Down
5 changes: 5 additions & 0 deletions requirements/requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,9 @@ def parse(cls, line: str) -> 'Requirement':
return cls.parse_editable(
re.sub(r'^(-e|--editable=?)\s*', '', line))

if ' --hash=' in line:
line = line[:line.find(' --hash=')]
if ' \\' in line:
line = line[:line.find(' \\')]

return cls.parse_line(line)
44 changes: 44 additions & 0 deletions tests/reqfiles/hash_1.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"specifier": true,
"local_file": false,
"name": "packaging",
"editable": false,
"subdirectory": null,
"uri": null,
"extras": [],
"vcs": null,
"path": null,
"line": "packaging==21.3",
"hash_name": null,
"hash": null,
"specs": [
[
"==",
"21.3"
]
],
"revision": null
},
{
"specifier": true,
"local_file": false,
"name": "packaging",
"editable": false,
"subdirectory": null,
"uri": null,
"extras": [],
"vcs": null,
"path": null,
"line": "packaging==21.3",
"hash_name": null,
"hash": null,
"specs": [
[
"==",
"21.3"
]
],
"revision": null
}
]
7 changes: 7 additions & 0 deletions tests/reqfiles/hash_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
packaging==21.3 \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522 \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
# testing
packaging==21.3 \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522 \
--hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522
23 changes: 23 additions & 0 deletions tests/reqfiles/hash_2.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"specifier": true,
"local_file": false,
"name": "packaging",
"editable": false,
"subdirectory": null,
"uri": null,
"extras": [],
"vcs": null,
"path": null,
"line": "packaging==21.3",
"hash_name": null,
"hash": null,
"specs": [
[
"==",
"21.3"
]
],
"revision": null
}
]
1 change: 1 addition & 0 deletions tests/reqfiles/hash_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packaging==21.3 --hash=sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522

0 comments on commit a9aafc9

Please sign in to comment.