| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os |
| 20 |
import warnings |
| 21 |
from typing import Iterator, TextIO, Union |
| 22 |
|
| 23 |
from .requirement import Requirement |
| 24 |
|
| 25 |
_UNSUPPORTED_OPTIONS = { |
| 26 |
'-c': 'Unused option -c (constraint). Skipping.', |
| 27 |
'--constraint': 'Unused option -c (constraint). Skipping.', |
| 28 |
'-r': 'Unused option -r (requirement). Skipping.', |
| 29 |
'--requirement': 'Unused option -r (requirement). Skipping.', |
| 30 |
'--no-binary': 'Unused option --no-binary. Skipping', |
| 31 |
'--only-binary': 'Unused option --only-binary. Skipping', |
| 32 |
'--prefer-binary': 'Unused option --prefer-binary. Skipping', |
| 33 |
'--require-hashes': 'Unused option --require-hashes. Skipping', |
| 34 |
'--pre': 'Unused option --pre. Skipping', |
| 35 |
'--trusted-host': 'Unused option --trusted-host. Skipping', |
| 36 |
'--use-feature': 'Unused option --use-feature. Skipping', |
| 37 |
'-Z': 'Unused option -Z (always-unzip). Skipping.', |
| 38 |
'--always-unzip': 'Unused option --always-unzip. Skipping.' |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
def parse(reqstr: Union[str, TextIO]) -> Iterator[Requirement]: |
| 43 |
""" |
| 44 |
Parse a requirements file into a list of Requirements |
| 45 |
|
| 46 |
See: pip/req.py:parse_requirements() |
| 47 |
|
| 48 |
:param reqstr: a string or file like object containing requirements |
| 49 |
:returns: a *generator* of Requirement objects |
| 50 |
""" |
| 51 |
filename = getattr(reqstr, 'name', None) |
| 52 |
|
| 53 |
|
| 54 |
if not isinstance(reqstr, str): |
| 55 |
reqstr = reqstr.read() |
| 56 |
|
| 57 |
for line in reqstr.splitlines(): |
| 58 |
line = line.strip() |
| 59 |
|
| 60 |
if line == '': |
| 61 |
continue |
| 62 |
elif not line or line.startswith('#'): |
| 63 |
|
| 64 |
continue |
| 65 |
elif not line or line.startswith('--hash='): |
| 66 |
|
| 67 |
continue |
| 68 |
elif line.startswith(('-r', '--requirement')): |
| 69 |
_, new_filename = line.split() |
| 70 |
new_file_path = os.path.join(os.path.dirname(filename or '.'), |
| 71 |
new_filename) |
| 72 |
with open(new_file_path) as f: |
| 73 |
for requirement in parse(f): |
| 74 |
yield requirement |
| 75 |
elif line.startswith('.'): |
| 76 |
|
| 77 |
warnings.warn( |
| 78 |
f'Relative path in {filename or "?"}: {line} is not supported. Skippining', stacklevel=2 |
| 79 |
) |
| 80 |
continue |
| 81 |
elif line.startswith('-f') or line.startswith('--find-links') or \ |
| 82 |
line.startswith('-i') or line.startswith('--index-url') or \ |
| 83 |
line.startswith('--extra-index-url') or \ |
| 84 |
line.startswith('--no-index'): |
| 85 |
warnings.warn('Private repos not supported. Skipping.', stacklevel=2) |
| 86 |
continue |
| 87 |
else: |
| 88 |
unsupported: bool = False |
| 89 |
for param in _UNSUPPORTED_OPTIONS.keys(): |
| 90 |
if line.startswith(param): |
| 91 |
warnings.warn(str(_UNSUPPORTED_OPTIONS.get(param)), stacklevel=2) |
| 92 |
unsupported = True |
| 93 |
|
| 94 |
|
| 95 |
if not unsupported: |
| 96 |
yield Requirement.parse(line) |