-
Notifications
You must be signed in to change notification settings - Fork 10
/
requirements.py
executable file
·191 lines (160 loc) · 5.77 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python2
#
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.
"""Generate minimal requirements from `setup.py` + `requirements-devel.txt`."""
from __future__ import print_function
import argparse
import re
import sys
import mock
import pkg_resources
import setuptools
def parse_set(string):
"""Parse set from comma separated string."""
string = string.strip()
if string:
return set(string.split(","))
else:
return set()
def minver_error(pkg_name):
"""Report error about missing minimum version constraint and exit."""
print(
'ERROR: specify minimal version of "{}" using '
'">=" or "=="'.format(pkg_name),
file=sys.stderr
)
sys.exit(1)
def parse_pip_file(path):
"""Parse pip requirements file."""
# requirement lines sorted by importance
# also collect other pip commands
rdev = dict()
rnormal = []
stuff = []
try:
with open(path) as f:
for line in f:
line = line.strip()
# see https://pip.readthedocs.org/en/1.1/requirements.html
if line.startswith('-e'):
# devel requirement
splitted = line.split('#egg=')
rdev[splitted[1].lower()] = line
elif line.startswith('-r'):
# recursive file command
splitted = re.split('-r\\s+', line)
subrdev, subrnormal, substuff = parse_pip_file(splitted[1])
for k, v in subrdev.iteritems():
if k not in rdev:
rdev[k] = v
rnormal.extend(subrnormal)
result.extend(substuff)
elif line.startswith('-'):
# another special command we don't recognize
stuff.append(line)
else:
# ordenary requirement, similary to them used in setup.py
rnormal.append(line)
except IOError:
print(
'Warning: could not parse requirements file "{}"!',
file=sys.stderr
)
return rdev, rnormal, stuff
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Calculates requirements for different purposes',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-l', '--level',
choices=['min', 'pypi', 'dev'],
default='pypi',
help='Specifies desired requirements level.'
'"min" requests the minimal requirement that is specified, '
'"pypi" requests the maximum version that satisfies the '
'constrains and is available in PyPi. '
'"dev" includes experimental developer versions for VCSs.'
)
parser.add_argument(
'-e', '--extras',
default='',
help='Comma separated list of extras.',
type=parse_set
)
args = parser.parse_args()
result = dict()
requires = []
stuff = []
if args.level == 'dev':
result, requires, stuff = parse_pip_file('requirements-devel.txt')
with mock.patch.object(setuptools, 'setup') as mock_setup:
import setup
assert setup # silence warning about unused imports
# called arguments are in `mock_setup.call_args`
mock_args, mock_kwargs = mock_setup.call_args
requires = mock_kwargs.get('install_requires', [])
requires_extras = mock_kwargs.get('extras_require', {})
for e in args.extras:
if e in requires_extras:
requires.extend(requires_extras[e])
for pkg in pkg_resources.parse_requirements(requires):
# skip things we already know
# FIXME be smarter about merging things
if pkg.key in result:
continue
specs = dict(pkg.specs)
if (('>=' in specs) and ('>' in specs)) \
or (('<=' in specs) and ('<' in specs)):
print(
'ERROR: Do not specify such weird constraints! '
'("{}")'.format(pkg),
file=sys.stderr
)
sys.exit(1)
if '==' in specs:
result[pkg.key] = '{}=={}'.format(pkg.project_name, specs['=='])
elif '>=' in specs:
if args.level == 'min':
result[pkg.key] = '{}=={}'.format(
pkg.project_name,
specs['>=']
)
else:
result[pkg.key] = pkg
elif '>' in specs:
if args.level == 'min':
minver_error(pkg.project_name)
else:
result[pkg.key] = pkg
else:
if args.level == 'min':
minver_error(pkg.project_name)
else:
result[pkg.key] = pkg
for s in stuff:
print(s)
for k in sorted(result.iterkeys()):
print(result[k])