Parse the package dependencies listed in a requirements file but strips specific versioning information. Args: fname (str): path to requirements file with_version (bool, default=False): if True include version specs Returns: list[str]: list of requirements items
(fname='requirements.txt', with_version=True)
| 60 | |
| 61 | |
| 62 | def parse_requirements(fname='requirements.txt', with_version=True): |
| 63 | """Parse the package dependencies listed in a requirements file but strips |
| 64 | specific versioning information. |
| 65 | |
| 66 | Args: |
| 67 | fname (str): path to requirements file |
| 68 | with_version (bool, default=False): if True include version specs |
| 69 | |
| 70 | Returns: |
| 71 | list[str]: list of requirements items |
| 72 | |
| 73 | CommandLine: |
| 74 | python -c "import setup; print(setup.parse_requirements())" |
| 75 | """ |
| 76 | import re |
| 77 | import sys |
| 78 | from os.path import exists |
| 79 | require_fpath = fname |
| 80 | |
| 81 | def parse_line(line): |
| 82 | """Parse information from a line in a requirements text file.""" |
| 83 | if line.startswith('-r '): |
| 84 | # Allow specifying requirements in other files |
| 85 | target = line.split(' ')[1] |
| 86 | for info in parse_require_file(target): |
| 87 | yield info |
| 88 | else: |
| 89 | info = {'line': line} |
| 90 | if line.startswith('-e '): |
| 91 | info['package'] = line.split('#egg=')[1] |
| 92 | else: |
| 93 | # Remove versioning from the package |
| 94 | pat = '(' + '|'.join(['>=', '==', '>']) + ')' |
| 95 | parts = re.split(pat, line, maxsplit=1) |
| 96 | parts = [p.strip() for p in parts] |
| 97 | |
| 98 | info['package'] = parts[0] |
| 99 | if len(parts) > 1: |
| 100 | op, rest = parts[1:] |
| 101 | if ';' in rest: |
| 102 | # Handle platform specific dependencies |
| 103 | # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies |
| 104 | version, platform_deps = map(str.strip, |
| 105 | rest.split(';')) |
| 106 | info['platform_deps'] = platform_deps |
| 107 | else: |
| 108 | version = rest # NOQA |
| 109 | info['version'] = (op, version) |
| 110 | yield info |
| 111 | |
| 112 | def parse_require_file(fpath): |
| 113 | with open(fpath, 'r') as f: |
| 114 | for line in f.readlines(): |
| 115 | line = line.strip() |
| 116 | if line and not line.startswith('#'): |
| 117 | for info in parse_line(line): |
| 118 | yield info |
| 119 |
no test coverage detected