|
1 | 1 | # -*- coding: utf-8 -*-
|
2 |
| -# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
3 |
| -# contributor license agreements. See the NOTICE file distributed with |
4 |
| -# this work for additional information regarding copyright ownership. |
5 |
| -# The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
| -# (the "License"); you may not use this file except in compliance with |
7 |
| -# the License. You may obtain a copy of the License at |
| 2 | +# NOTE: This file is auto-generated - DO NOT EDIT MANUALLY |
| 3 | +# Instead copy from https://github.com/StackStorm/st2/blob/master/scripts/dist_utils.py |
| 4 | + |
| 5 | +# Copyright 2019 Extreme Networks, Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
8 | 10 | #
|
9 | 11 | # http://www.apache.org/licenses/LICENSE-2.0
|
10 | 12 | #
|
|
15 | 17 | # limitations under the License.
|
16 | 18 |
|
17 | 19 | from __future__ import absolute_import
|
| 20 | + |
18 | 21 | import os
|
19 | 22 | import re
|
20 | 23 | import sys
|
21 | 24 |
|
22 | 25 | from distutils.version import StrictVersion
|
23 | 26 |
|
24 |
| -GET_PIP = 'curl https://bootstrap.pypa.io/get-pip.py | python' |
25 |
| - |
26 |
| -try: |
27 |
| - import pip |
28 |
| - from pip import __version__ as pip_version |
29 |
| -except ImportError as e: |
30 |
| - print('Failed to import pip: %s' % (str(e))) |
31 |
| - print('') |
32 |
| - print('Download pip:\n%s' % (GET_PIP)) |
33 |
| - sys.exit(1) |
34 |
| - |
35 |
| -try: |
36 |
| - # pip < 10.0 |
37 |
| - from pip.req import parse_requirements |
38 |
| -except ImportError: |
39 |
| - # pip >= 10.0 |
| 27 | +# NOTE: This script can't rely on any 3rd party dependency so we need to use this code here |
| 28 | +# |
| 29 | +# TODO: Why can't this script rely on 3rd party dependencies? Is it because it has to import |
| 30 | +# from pip? |
| 31 | +# |
| 32 | +# TODO: Dear future developer, if you are back here fixing a bug with how we parse |
| 33 | +# requirements files, please look into using the packaging package on PyPI: |
| 34 | +# https://packaging.pypa.io/en/latest/requirements/ |
| 35 | +# and specifying that in the `setup_requires` argument to `setuptools.setup()` |
| 36 | +# for subpackages. |
| 37 | +# At the very least we can vendorize some of their code instead of reimplementing |
| 38 | +# each piece of their code every time our parsing breaks. |
| 39 | +PY3 = sys.version_info[0] == 3 |
| 40 | + |
| 41 | +if PY3: |
| 42 | + text_type = str |
| 43 | +else: |
| 44 | + text_type = unicode # NOQA |
40 | 45 |
|
41 |
| - try: |
42 |
| - from pip._internal.req.req_file import parse_requirements |
43 |
| - except ImportError as e: |
44 |
| - print('Failed to import parse_requirements from pip: %s' % (str(e))) |
45 |
| - print('Using pip: %s' % (str(pip_version))) |
46 |
| - sys.exit(1) |
| 46 | +GET_PIP = 'curl https://bootstrap.pypa.io/get-pip.py | python' |
47 | 47 |
|
48 | 48 | __all__ = [
|
| 49 | + 'check_pip_is_installed', |
49 | 50 | 'check_pip_version',
|
50 | 51 | 'fetch_requirements',
|
51 | 52 | 'apply_vagrant_workaround',
|
|
54 | 55 | ]
|
55 | 56 |
|
56 | 57 |
|
57 |
| -def check_pip_version(): |
| 58 | +def check_pip_is_installed(): |
| 59 | + """ |
| 60 | + Ensure that pip is installed. |
| 61 | + """ |
| 62 | + try: |
| 63 | + import pip # NOQA |
| 64 | + except ImportError as e: |
| 65 | + print('Failed to import pip: %s' % (text_type(e))) |
| 66 | + print('') |
| 67 | + print('Download pip:\n%s' % (GET_PIP)) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + return True |
| 71 | + |
| 72 | + |
| 73 | +def check_pip_version(min_version='6.0.0'): |
58 | 74 | """
|
59 | 75 | Ensure that a minimum supported version of pip is installed.
|
60 | 76 | """
|
61 |
| - if StrictVersion(pip.__version__) < StrictVersion('6.0.0'): |
62 |
| - print("Upgrade pip, your version `{0}' " |
63 |
| - "is outdated:\n{1}".format(pip.__version__, GET_PIP)) |
| 77 | + check_pip_is_installed() |
| 78 | + |
| 79 | + import pip |
| 80 | + |
| 81 | + if StrictVersion(pip.__version__) < StrictVersion(min_version): |
| 82 | + print("Upgrade pip, your version '{0}' " |
| 83 | + "is outdated. Minimum required version is '{1}':\n{2}".format(pip.__version__, |
| 84 | + min_version, |
| 85 | + GET_PIP)) |
64 | 86 | sys.exit(1)
|
65 | 87 |
|
| 88 | + return True |
| 89 | + |
66 | 90 |
|
67 | 91 | def fetch_requirements(requirements_file_path):
|
68 | 92 | """
|
69 | 93 | Return a list of requirements and links by parsing the provided requirements file.
|
70 | 94 | """
|
71 | 95 | links = []
|
72 | 96 | reqs = []
|
73 |
| - for req in parse_requirements(requirements_file_path, session=False): |
74 |
| - if req.link: |
75 |
| - links.append(str(req.link)) |
76 |
| - reqs.append(str(req.req)) |
| 97 | + |
| 98 | + def _get_link(line): |
| 99 | + vcs_prefixes = ['git+', 'svn+', 'hg+', 'bzr+'] |
| 100 | + |
| 101 | + for vcs_prefix in vcs_prefixes: |
| 102 | + if line.startswith(vcs_prefix) or line.startswith('-e %s' % (vcs_prefix)): |
| 103 | + req_name = re.findall('.*#egg=(.+)([&|@]).*$', line) |
| 104 | + |
| 105 | + if not req_name: |
| 106 | + req_name = re.findall('.*#egg=(.+?)$', line) |
| 107 | + else: |
| 108 | + req_name = req_name[0] |
| 109 | + |
| 110 | + if not req_name: |
| 111 | + raise ValueError('Line "%s" is missing "#egg=<package name>"' % (line)) |
| 112 | + |
| 113 | + link = line.replace('-e ', '').strip() |
| 114 | + return link, req_name[0] |
| 115 | + |
| 116 | + return None, None |
| 117 | + |
| 118 | + with open(requirements_file_path, 'r') as fp: |
| 119 | + for line in fp.readlines(): |
| 120 | + line = line.strip() |
| 121 | + |
| 122 | + if line.startswith('#') or not line: |
| 123 | + continue |
| 124 | + |
| 125 | + link, req_name = _get_link(line=line) |
| 126 | + |
| 127 | + if link: |
| 128 | + links.append(link) |
| 129 | + else: |
| 130 | + req_name = line |
| 131 | + |
| 132 | + if ';' in req_name: |
| 133 | + req_name = req_name.split(';')[0].strip() |
| 134 | + |
| 135 | + reqs.append(req_name) |
| 136 | + |
77 | 137 | return (reqs, links)
|
78 | 138 |
|
79 | 139 |
|
|
0 commit comments