-
Notifications
You must be signed in to change notification settings - Fork 7
/
parse_requirements.py
41 lines (30 loc) · 1.1 KB
/
parse_requirements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
def parse_requirements(requirements, ignore=('setuptools',)):
"""Read dependencies from file and strip off version numbers.
Supports extra requirements and '>=' and '<=' version qualifiers.
"""
with open(requirements) as f:
packages = set()
for line in f:
line = line.strip()
if not line or line.startswith(('#', '-r', '--')):
continue
# XXX: Ugly hack!
extras = []
def get_extras(match):
extras.append(match.group(1))
line = re.sub(r'\s*\[(.*?)\]', get_extras, line)
if '#egg=' in line:
line = line.split('#egg=')[1]
pkg = re.split('[=<>]=', line)[0].strip()
if pkg not in ignore:
if extras:
pkg = 'pkg [%s]' % extras[0]
packages.add(pkg)
return tuple(packages)
if __name__ == '__main__':
import sys
print("\n".join(sorted(parse_requirements(
sys.argv[1] if sys.argv[1:] else 'requirements.txt'))))