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
(fname='requirements.txt', with_version=True)
| 85 | |
| 86 | |
| 87 | def parse_requirements(fname='requirements.txt', with_version=True): |
| 88 | """ |
| 89 | Parse the package dependencies listed in a requirements file but strips |
| 90 | specific versioning information. |
| 91 | |
| 92 | Args: |
| 93 | fname (str): path to requirements file |
| 94 | with_version (bool, default=False): if True include version specs |
| 95 | |
| 96 | Returns: |
| 97 | List[str]: list of requirements items |
| 98 | |
| 99 | CommandLine: |
| 100 | python -c "import setup; print(setup.parse_requirements())" |
| 101 | """ |
| 102 | import sys |
| 103 | from os.path import exists |
| 104 | import re |
| 105 | require_fpath = fname |
| 106 | |
| 107 | def parse_line(line): |
| 108 | """ |
| 109 | Parse information from a line in a requirements text file |
| 110 | """ |
| 111 | if line.startswith('-r '): |
| 112 | # Allow specifying requirements in other files |
| 113 | target = line.split(' ')[1] |
| 114 | for info in parse_require_file(target): |
| 115 | yield info |
| 116 | else: |
| 117 | info = {'line': line} |
| 118 | if line.startswith('-e '): |
| 119 | info['package'] = line.split('#egg=')[1] |
| 120 | else: |
| 121 | # Remove versioning from the package |
| 122 | pat = '(' + '|'.join(['>=', '==', '>']) + ')' |
| 123 | parts = re.split(pat, line, maxsplit=1) |
| 124 | parts = [p.strip() for p in parts] |
| 125 | |
| 126 | info['package'] = parts[0] |
| 127 | if len(parts) > 1: |
| 128 | op, rest = parts[1:] |
| 129 | if ';' in rest: |
| 130 | # Handle platform specific dependencies |
| 131 | # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies |
| 132 | version, platform_deps = map(str.strip, |
| 133 | rest.split(';')) |
| 134 | info['platform_deps'] = platform_deps |
| 135 | else: |
| 136 | version = rest # NOQA |
| 137 | info['version'] = (op, version) |
| 138 | yield info |
| 139 | |
| 140 | def parse_require_file(fpath): |
| 141 | with open(fpath, 'r') as f: |
| 142 | for line in f.readlines(): |
| 143 | line = line.strip() |
| 144 | if line and not line.startswith('#'): |
no test coverage detected