Skip to content

Commit

Permalink
Merge pull request #8 from StackStorm/bugfix/sync-distutils
Browse files Browse the repository at this point in the history
Sync dist_utils.py with latest from st2 repo
  • Loading branch information
armab committed Apr 29, 2020
2 parents f39b46b + 3038937 commit 4ab6fd6
Showing 1 changed file with 96 additions and 36 deletions.
132 changes: 96 additions & 36 deletions dist_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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
# NOTE: This file is auto-generated - DO NOT EDIT MANUALLY
# Instead copy from https://github.com/StackStorm/st2/blob/master/scripts/dist_utils.py

# Copyright 2019 Extreme Networks, Inc.
#
# 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
#
Expand All @@ -15,37 +17,36 @@
# limitations under the License.

from __future__ import absolute_import

import os
import re
import sys

from distutils.version import StrictVersion

GET_PIP = 'curl https://bootstrap.pypa.io/get-pip.py | python'

try:
import pip
from pip import __version__ as pip_version
except ImportError as e:
print('Failed to import pip: %s' % (str(e)))
print('')
print('Download pip:\n%s' % (GET_PIP))
sys.exit(1)

try:
# pip < 10.0
from pip.req import parse_requirements
except ImportError:
# pip >= 10.0
# NOTE: This script can't rely on any 3rd party dependency so we need to use this code here
#
# TODO: Why can't this script rely on 3rd party dependencies? Is it because it has to import
# from pip?
#
# TODO: Dear future developer, if you are back here fixing a bug with how we parse
# requirements files, please look into using the packaging package on PyPI:
# https://packaging.pypa.io/en/latest/requirements/
# and specifying that in the `setup_requires` argument to `setuptools.setup()`
# for subpackages.
# At the very least we can vendorize some of their code instead of reimplementing
# each piece of their code every time our parsing breaks.
PY3 = sys.version_info[0] == 3

if PY3:
text_type = str
else:
text_type = unicode # NOQA

try:
from pip._internal.req.req_file import parse_requirements
except ImportError as e:
print('Failed to import parse_requirements from pip: %s' % (str(e)))
print('Using pip: %s' % (str(pip_version)))
sys.exit(1)
GET_PIP = 'curl https://bootstrap.pypa.io/get-pip.py | python'

__all__ = [
'check_pip_is_installed',
'check_pip_version',
'fetch_requirements',
'apply_vagrant_workaround',
Expand All @@ -54,26 +55,85 @@
]


def check_pip_version():
def check_pip_is_installed():
"""
Ensure that pip is installed.
"""
try:
import pip # NOQA
except ImportError as e:
print('Failed to import pip: %s' % (text_type(e)))
print('')
print('Download pip:\n%s' % (GET_PIP))
sys.exit(1)

return True


def check_pip_version(min_version='6.0.0'):
"""
Ensure that a minimum supported version of pip is installed.
"""
if StrictVersion(pip.__version__) < StrictVersion('6.0.0'):
print("Upgrade pip, your version `{0}' "
"is outdated:\n{1}".format(pip.__version__, GET_PIP))
check_pip_is_installed()

import pip

if StrictVersion(pip.__version__) < StrictVersion(min_version):
print("Upgrade pip, your version '{0}' "
"is outdated. Minimum required version is '{1}':\n{2}".format(pip.__version__,
min_version,
GET_PIP))
sys.exit(1)

return True


def fetch_requirements(requirements_file_path):
"""
Return a list of requirements and links by parsing the provided requirements file.
"""
links = []
reqs = []
for req in parse_requirements(requirements_file_path, session=False):
if req.link:
links.append(str(req.link))
reqs.append(str(req.req))

def _get_link(line):
vcs_prefixes = ['git+', 'svn+', 'hg+', 'bzr+']

for vcs_prefix in vcs_prefixes:
if line.startswith(vcs_prefix) or line.startswith('-e %s' % (vcs_prefix)):
req_name = re.findall('.*#egg=(.+)([&|@]).*$', line)

if not req_name:
req_name = re.findall('.*#egg=(.+?)$', line)
else:
req_name = req_name[0]

if not req_name:
raise ValueError('Line "%s" is missing "#egg=<package name>"' % (line))

link = line.replace('-e ', '').strip()
return link, req_name[0]

return None, None

with open(requirements_file_path, 'r') as fp:
for line in fp.readlines():
line = line.strip()

if line.startswith('#') or not line:
continue

link, req_name = _get_link(line=line)

if link:
links.append(link)
else:
req_name = line

if ';' in req_name:
req_name = req_name.split(';')[0].strip()

reqs.append(req_name)

return (reqs, links)


Expand Down

0 comments on commit 4ab6fd6

Please sign in to comment.