Skip to content

Commit

Permalink
Merge pull request #24 from mshenfield/issue-22-add-hash-information-…
Browse files Browse the repository at this point in the history
…to-requirements

Add hash and hash_name fragment
  • Loading branch information
davidfischer authored Jan 24, 2017
2 parents 4a43544 + 86c46b3 commit af136a1
Show file tree
Hide file tree
Showing 21 changed files with 2,191 additions and 1,833 deletions.
14 changes: 14 additions & 0 deletions requirements/fragment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Copied from pip
# https://github.com/pypa/pip/blob/281eb61b09d87765d7c2b92f6982b3fe76ccb0af/pip/index.py#L947
HASH_ALGORITHMS = set(['sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5'])


def parse_fragment(fragment_string):
"""Takes a fragment string nd returns a dict of the components"""
fragment_string = fragment_string.lstrip('#')
Expand All @@ -13,3 +18,12 @@ def parse_fragment(fragment_string):
fragment_string=fragment_string
)
)


def get_hash_info(d):
"""Returns the first matching hashlib name and value from a dict"""
for key in d.keys():
if key.lower() in HASH_ALGORITHMS:
return key, d[key]

return None, None
11 changes: 10 additions & 1 deletion requirements/requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from pkg_resources import Requirement as Req

from .fragment import parse_fragment
from .fragment import get_hash_info, parse_fragment
from .vcs import VCS, VCS_SCHEMES


Expand Down Expand Up @@ -50,6 +50,8 @@ class Requirement(object):
* ``name`` - the name of the requirement
* ``uri`` - the URI if this requirement was specified by URI
* ``path`` - the local path to the requirement
* ``hash_name`` - the type of hashing algorithm indicated in the line
* ``hash`` - the hash value indicated by the requirement line
* ``extras`` - a list of extras for this requirement
(eg. "mymodule[extra1, extra2]")
* ``specs`` - a list of specs for this requirement
Expand All @@ -67,6 +69,8 @@ def __init__(self, line):
self.uri = None
self.path = None
self.revision = None
self.hash_name = None
self.hash = None
self.extras = []
self.specs = []

Expand Down Expand Up @@ -104,6 +108,7 @@ def parse_editable(cls, line):
if groups['fragment']:
fragment = parse_fragment(groups['fragment'])
req.name = fragment.get('egg')
req.hash_name, req.hash = get_hash_info(fragment)
for vcs in VCS:
if req.uri.startswith(vcs):
req.vcs = vcs
Expand All @@ -114,6 +119,7 @@ def parse_editable(cls, line):
if groups['fragment']:
fragment = parse_fragment(groups['fragment'])
req.name = fragment.get('egg')
req.hash_name, req.hash = get_hash_info(fragment)
req.path = groups['path']

return req
Expand Down Expand Up @@ -143,6 +149,7 @@ def parse_line(cls, line):
if groups['fragment']:
fragment = parse_fragment(groups['fragment'])
req.name = fragment.get('egg')
req.hash_name, req.hash = get_hash_info(fragment)
for vcs in VCS:
if req.uri.startswith(vcs):
req.vcs = vcs
Expand All @@ -152,6 +159,7 @@ def parse_line(cls, line):
if groups['fragment']:
fragment = parse_fragment(groups['fragment'])
req.name = fragment.get('egg')
req.hash_name, req.hash = get_hash_info(fragment)
if groups['scheme'] == 'file':
req.local_file = True
elif '#egg=' in line:
Expand All @@ -162,6 +170,7 @@ def parse_line(cls, line):
if groups['fragment']:
fragment = parse_fragment(groups['fragment'])
req.name = fragment.get('egg')
req.hash_name, req.hash = get_hash_info(fragment)
req.path = groups['path']
else:
# This is a requirement specifier.
Expand Down
26 changes: 14 additions & 12 deletions tests/reqfiles/comment_2.expected
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
[
{
"specifier": true,
"local_file": false,
"name": "req",
"editable": false,
"uri": null,
"extras": [],
"vcs": null,
"path": null,
"line": "req==1.0 # comment",
"specifier": true,
"local_file": false,
"name": "req",
"editable": false,
"uri": null,
"extras": [],
"vcs": null,
"path": null,
"line": "req==1.0 # comment",
"hash_name": null,
"hash": null,
"specs": [
[
"==",
"==",
"1.0"
]
],
],
"revision": null
}
]
]
Loading

0 comments on commit af136a1

Please sign in to comment.