requirements/detail.py
Hervé Beraud fac394f79c Stop to use the __future__ module.
The __future__ module [1] was used in this context to ensure compatibility
between python 2 and python 3.

We previously dropped the support of python 2.7 [2] and now we only support
python 3 so we don't need to continue to use this module and the imports
listed below.

Imports commonly used and their related PEPs:
- `division` is related to PEP 238 [3]
- `print_function` is related to PEP 3105 [4]
- `unicode_literals` is related to PEP 3112 [5]
- `with_statement` is related to PEP 343 [6]
- `absolute_import` is related to PEP 328 [7]

[1] https://docs.python.org/3/library/__future__.html
[2] https://governance.openstack.org/tc/goals/selected/ussuri/drop-py27.html
[3] https://www.python.org/dev/peps/pep-0238
[4] https://www.python.org/dev/peps/pep-3105
[5] https://www.python.org/dev/peps/pep-3112
[6] https://www.python.org/dev/peps/pep-0343
[7] https://www.python.org/dev/peps/pep-0328

Change-Id: I15d451f6a832b2e698b28e7351a36ef7aea92abe
2020-06-02 20:56:04 +02:00

104 lines
3.2 KiB
Python

# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import json
import os
import six.moves.urllib.parse as urlparse
import six.moves.urllib.request as urlreq
import sys
import traceback
import pkg_resources
try:
PYPI_LOCATION = os.environ['PYPI_LOCATION']
except KeyError:
PYPI_LOCATION = 'http://pypi.org/project'
KEEP_KEYS = frozenset([
'author',
'author_email',
'maintainer',
'maintainer_email',
'license',
'summary',
'home_page',
])
def iter_names(req):
for k in (req.key, req.project_name):
yield k
yield k.title()
yield k.replace("-", "_")
yield k.replace("-", "_").title()
def release_data(req):
# Try to find it with various names...
attempted = []
for name in iter_names(req):
url = PYPI_LOCATION + "/%s/json" % (urlparse.quote(name))
if url in attempted:
continue
with contextlib.closing(urlreq.urlopen(url)) as uh:
if uh.getcode() != 200:
attempted.append(url)
continue
return json.loads(uh.read())
attempted = [" * %s" % u for u in attempted]
raise IOError("Could not find '%s' on pypi\nAttempted urls:\n%s"
% (req.key, "\n".join(attempted)))
def main():
if len(sys.argv) == 1:
print("%s requirement-file ..." % (sys.argv[0]), file=sys.stderr)
sys.exit(1)
for filename in sys.argv[1:]:
print("Analyzing file: %s" % (filename))
details = {}
with open(filename, "rb") as fh:
for line in fh.read().splitlines():
line = line.strip()
if line.startswith("#") or not line:
continue
req = pkg_resources.Requirement.parse(line)
print(" - processing: %s" % (req))
try:
raw_req_data = release_data(req)
except IOError:
traceback.print_exc()
details[req.key] = None
else:
req_info = {}
for (k, v) in raw_req_data.get('info', {}).items():
if k not in KEEP_KEYS:
continue
req_info[k] = v
details[req.key] = {
'requirement': str(req),
'info': req_info,
}
filename, _ext = os.path.splitext(filename)
with open("%s.json" % (filename), "wb") as fh:
fh.write(json.dumps(details, sort_keys=True, indent=4,
separators=(",", ": ")))
if __name__ == '__main__':
main()