Debian Codegraph @

requirements/parser.py

raw symbols SCIP
1 # encoding: utf-8
2
3 # This file is part of requirements-parser library.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
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 # Python 3.x only
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 # comments are lines that start with # only
64 continue
65 elif not line or line.startswith('--hash='):
66 # hashes are lines that start with --hash=
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 # Relative paths are not supported, but we can warn about them.
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 # Otherwise, parse it
95 if not unsupported:
96 yield Requirement.parse(line)