mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 05:14:18 +00:00
Merge pull request #483 from mxr/parse-reqs
Parse more operators in requirements
This commit is contained in:
commit
e0c9d513c3
2 changed files with 37 additions and 5 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import argparse
|
import argparse
|
||||||
|
import re
|
||||||
from typing import IO
|
from typing import IO
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
@ -10,6 +11,9 @@ FAIL = 1
|
||||||
|
|
||||||
|
|
||||||
class Requirement:
|
class Requirement:
|
||||||
|
UNTIL_COMPARISON = re.compile(b'={2,3}|!=|~=|>=?|<=?')
|
||||||
|
UNTIL_SEP = re.compile(rb'[^;\s]+')
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.value: Optional[bytes] = None
|
self.value: Optional[bytes] = None
|
||||||
self.comments: List[bytes] = []
|
self.comments: List[bytes] = []
|
||||||
|
|
@ -17,11 +21,20 @@ class Requirement:
|
||||||
@property
|
@property
|
||||||
def name(self) -> bytes:
|
def name(self) -> bytes:
|
||||||
assert self.value is not None, self.value
|
assert self.value is not None, self.value
|
||||||
|
name = self.value.lower()
|
||||||
for egg in (b'#egg=', b'&egg='):
|
for egg in (b'#egg=', b'&egg='):
|
||||||
if egg in self.value:
|
if egg in self.value:
|
||||||
return self.value.lower().partition(egg)[-1]
|
return name.partition(egg)[-1]
|
||||||
|
|
||||||
return self.value.lower().partition(b'==')[0]
|
m = self.UNTIL_SEP.match(name)
|
||||||
|
assert m is not None
|
||||||
|
|
||||||
|
name = m.group()
|
||||||
|
m = self.UNTIL_COMPARISON.search(name)
|
||||||
|
if not m:
|
||||||
|
return name
|
||||||
|
|
||||||
|
return name[:m.start()]
|
||||||
|
|
||||||
def __lt__(self, requirement: 'Requirement') -> int:
|
def __lt__(self, requirement: 'Requirement') -> int:
|
||||||
# \n means top of file comment, so always return True,
|
# \n means top of file comment, so always return True,
|
||||||
|
|
|
||||||
|
|
@ -33,9 +33,28 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement
|
||||||
(b'\nfoo\nbar\n', FAIL, b'bar\n\nfoo\n'),
|
(b'\nfoo\nbar\n', FAIL, b'bar\n\nfoo\n'),
|
||||||
(b'\nbar\nfoo\n', PASS, b'\nbar\nfoo\n'),
|
(b'\nbar\nfoo\n', PASS, b'\nbar\nfoo\n'),
|
||||||
(
|
(
|
||||||
b'pyramid==1\npyramid-foo==2\n',
|
b'pyramid-foo==1\npyramid>=2\n',
|
||||||
PASS,
|
FAIL,
|
||||||
b'pyramid==1\npyramid-foo==2\n',
|
b'pyramid>=2\npyramid-foo==1\n',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
b'a==1\n'
|
||||||
|
b'c>=1\n'
|
||||||
|
b'bbbb!=1\n'
|
||||||
|
b'c-a>=1;python_version>="3.6"\n'
|
||||||
|
b'e>=2\n'
|
||||||
|
b'd>2\n'
|
||||||
|
b'g<2\n'
|
||||||
|
b'f<=2\n',
|
||||||
|
FAIL,
|
||||||
|
b'a==1\n'
|
||||||
|
b'bbbb!=1\n'
|
||||||
|
b'c>=1\n'
|
||||||
|
b'c-a>=1;python_version>="3.6"\n'
|
||||||
|
b'd>2\n'
|
||||||
|
b'e>=2\n'
|
||||||
|
b'f<=2\n'
|
||||||
|
b'g<2\n',
|
||||||
),
|
),
|
||||||
(b'ocflib\nDjango\nPyMySQL\n', FAIL, b'Django\nocflib\nPyMySQL\n'),
|
(b'ocflib\nDjango\nPyMySQL\n', FAIL, b'Django\nocflib\nPyMySQL\n'),
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue