|
| 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 |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +import os |
| 19 | +import re |
| 20 | +import sys |
| 21 | + |
| 22 | +from distutils.version import StrictVersion |
| 23 | + |
| 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 |
| 40 | + |
| 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) |
| 47 | + |
| 48 | +__all__ = [ |
| 49 | + 'check_pip_version', |
| 50 | + 'fetch_requirements', |
| 51 | + 'apply_vagrant_workaround', |
| 52 | + 'get_version_string', |
| 53 | + 'parse_version_string' |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +def check_pip_version(min_version='6.0.0'): |
| 58 | + """ |
| 59 | + Ensure that a minimum supported version of pip is installed. |
| 60 | + """ |
| 61 | + if StrictVersion(pip.__version__) < StrictVersion(min_version): |
| 62 | + print("Upgrade pip, your version '{0}' " |
| 63 | + "is outdated. Minimum required version is '{1}':\n{2}".format(pip.__version__, |
| 64 | + min_version, |
| 65 | + GET_PIP)) |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | + |
| 69 | +def fetch_requirements(requirements_file_path): |
| 70 | + """ |
| 71 | + Return a list of requirements and links by parsing the provided requirements file. |
| 72 | + """ |
| 73 | + links = [] |
| 74 | + reqs = [] |
| 75 | + for req in parse_requirements(requirements_file_path, session=False): |
| 76 | + # Note: req.url was used before 9.0.0 and req.link is used in all the recent versions |
| 77 | + link = getattr(req, 'link', getattr(req, 'url', None)) |
| 78 | + if link: |
| 79 | + links.append(str(link)) |
| 80 | + reqs.append(str(req.req)) |
| 81 | + return (reqs, links) |
| 82 | + |
| 83 | + |
| 84 | +def apply_vagrant_workaround(): |
| 85 | + """ |
| 86 | + Function which detects if the script is being executed inside vagrant and if it is, it deletes |
| 87 | + "os.link" attribute. |
| 88 | + Note: Without this workaround, setup.py sdist will fail when running inside a shared directory |
| 89 | + (nfs / virtualbox shared folders). |
| 90 | + """ |
| 91 | + if os.environ.get('USER', None) == 'vagrant': |
| 92 | + del os.link |
| 93 | + |
| 94 | + |
| 95 | +def get_version_string(init_file): |
| 96 | + """ |
| 97 | + Read __version__ string for an init file. |
| 98 | + """ |
| 99 | + |
| 100 | + with open(init_file, 'r') as fp: |
| 101 | + content = fp.read() |
| 102 | + version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", |
| 103 | + content, re.M) |
| 104 | + if version_match: |
| 105 | + return version_match.group(1) |
| 106 | + |
| 107 | + raise RuntimeError('Unable to find version string in %s.' % (init_file)) |
| 108 | + |
| 109 | + |
| 110 | +# alias for get_version_string |
| 111 | +parse_version_string = get_version_string |
0 commit comments