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)
| 18 | |
| 19 | |
| 20 | def parse_requirements(fname='requirements.txt', with_version=True): |
| 21 | """Parse the package dependencies listed in a requirements file but strips |
| 22 | specific versioning information. |
| 23 | |
| 24 | Args: |
| 25 | fname (str): path to requirements file |
| 26 | with_version (bool, default=False): if True include version specs |
| 27 | |
| 28 | Returns: |
| 29 | List[str]: list of requirements items |
| 30 | |
| 31 | CommandLine: |
| 32 | python -c "import setup; print(setup.parse_requirements())" |
| 33 | """ |
| 34 | import re |
| 35 | import sys |
| 36 | from os.path import exists |
| 37 | |
| 38 | require_fpath = fname |
| 39 | |
| 40 | def parse_line(line): |
| 41 | """Parse information from a line in a requirements text file.""" |
| 42 | if line.startswith('-r '): |
| 43 | # Allow specifying requirements in other files |
| 44 | target = line.split(' ')[1] |
| 45 | for info in parse_require_file(target): |
| 46 | yield info |
| 47 | else: |
| 48 | info = {'line': line} |
| 49 | if line.startswith('-e '): |
| 50 | info['package'] = line.split('#egg=')[1] |
| 51 | else: |
| 52 | # Remove versioning from the package |
| 53 | pat = '(' + '|'.join(['>=', '==', '>']) + ')' |
| 54 | parts = re.split(pat, line, maxsplit=1) |
| 55 | parts = [p.strip() for p in parts] |
| 56 | |
| 57 | info['package'] = parts[0] |
| 58 | if len(parts) > 1: |
| 59 | op, rest = parts[1:] |
| 60 | if ';' in rest: |
| 61 | # Handle platform specific dependencies |
| 62 | # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies |
| 63 | version, platform_deps = map(str.strip, |
| 64 | rest.split(';')) |
| 65 | info['platform_deps'] = platform_deps |
| 66 | else: |
| 67 | version = rest # NOQA |
| 68 | if '--' in version: |
| 69 | # the `extras_require` doesn't accept options. |
| 70 | version = version.split('--')[0].strip() |
| 71 | info['version'] = (op, version) |
| 72 | yield info |
| 73 | |
| 74 | def parse_require_file(fpath): |
| 75 | with open(fpath, 'r') as f: |
| 76 | for line in f.readlines(): |
| 77 | line = line.strip() |
no test coverage detected