|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | | -from setuptools import setup, find_packages |
| 3 | +import os |
| 4 | +import re |
| 5 | + |
| 6 | +from setuptools import find_packages, setup |
4 | 7 |
|
5 | 8 | from auth_backends import __version__ |
6 | 9 |
|
|
11 | 14 | def load_requirements(*requirements_paths): |
12 | 15 | """ |
13 | 16 | Load all requirements from the specified requirements files. |
| 17 | +
|
| 18 | + Requirements will include any constraints from files specified |
| 19 | + with -c in the requirements files. |
14 | 20 | Returns a list of requirement strings. |
15 | 21 | """ |
16 | | - requirements = set() |
| 22 | + # Modified from original SEMGREP update to allow package name with '[]' in it |
| 23 | + |
| 24 | + requirements = {} |
| 25 | + constraint_files = set() |
| 26 | + |
| 27 | + # groups "my-package-name<=x.y.z,..." into ("my-package-name", "<=x.y.z,...") |
| 28 | + requirement_line_regex = re.compile(r"([a-zA-Z0-9-_.\[\]]+)([<>=][^#\s]+)?") |
| 29 | + |
| 30 | + def add_version_constraint_or_raise(current_line, current_requirements, add_if_not_present): |
| 31 | + regex_match = requirement_line_regex.match(current_line) |
| 32 | + if regex_match: |
| 33 | + package = regex_match.group(1) |
| 34 | + version_constraints = regex_match.group(2) |
| 35 | + existing_version_constraints = current_requirements.get(package, None) |
| 36 | + # it's fine to add constraints to an unconstrained package, but raise an error if there are already |
| 37 | + # constraints in place |
| 38 | + if existing_version_constraints and existing_version_constraints != version_constraints: |
| 39 | + raise BaseException(f'Multiple constraint definitions found for {package}:' |
| 40 | + f' "{existing_version_constraints}" and "{version_constraints}".' |
| 41 | + f'Combine constraints into one location with {package}' |
| 42 | + f'{existing_version_constraints},{version_constraints}.') |
| 43 | + if add_if_not_present or package in current_requirements: |
| 44 | + current_requirements[package] = version_constraints |
| 45 | + |
| 46 | + # process .in files and store the path to any constraint files that are pulled in |
17 | 47 | for path in requirements_paths: |
18 | 48 | with open(path) as reqs: |
19 | | - requirements.update( |
20 | | - line.split('#')[0].strip() for line in reqs |
21 | | - if is_requirement(line.strip()) |
22 | | - ) |
23 | | - return list(requirements) |
| 49 | + for line in reqs: |
| 50 | + if is_requirement(line): |
| 51 | + add_version_constraint_or_raise(line, requirements, True) |
| 52 | + if line and line.startswith('-c') and not line.startswith('-c http'): |
| 53 | + constraint_files.add(os.path.dirname(path) + '/' + line.split('#')[0].replace('-c', '').strip()) |
| 54 | + |
| 55 | + # process constraint files and add any new constraints found to existing requirements |
| 56 | + for constraint_file in constraint_files: |
| 57 | + with open(constraint_file) as reader: |
| 58 | + for line in reader: |
| 59 | + if is_requirement(line): |
| 60 | + add_version_constraint_or_raise(line, requirements, False) |
| 61 | + |
| 62 | + # process back into list of pkg><=constraints strings |
| 63 | + constrained_requirements = [f'{pkg}{version or ""}' for (pkg, version) in sorted(requirements.items())] |
| 64 | + return constrained_requirements |
24 | 65 |
|
25 | 66 |
|
26 | 67 | def is_requirement(line): |
27 | 68 | """ |
28 | | - Return True if the requirement line is a package requirement; |
29 | | - that is, it is not blank, a comment, a URL, or an included file. |
| 69 | + Return True if the requirement line is a package requirement. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + bool: True if the line is not blank, a comment, |
| 73 | + a URL, or an included file |
30 | 74 | """ |
31 | | - return line and not line.startswith(('-r', '#', '-e', 'git+', '-c')) |
| 75 | + # UPDATED VIA SEMGREP - if you need to remove/modify this method remove this line and add a comment specifying why |
| 76 | + |
| 77 | + return line and line.strip() and not line.startswith(('-r', '#', '-e', 'git+', '-c')) |
32 | 78 |
|
33 | 79 |
|
34 | 80 | setup( |
|
0 commit comments