aboutsummaryrefslogtreecommitdiffstats
path: root/tools/os-requirements-check.py
blob: c19fbce55fa9064730bba5315b13559ff3ee7bd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold }
# Copyright (c) 2017 Intel Corporation
#
# 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 argparse
import collections
import os
from packaging import version as pkg_version
import sys

from openstack_requirements import requirement


PROJECT_REQUIREMENTS_FILES = ['requirements.txt']
QUALIFIER_CHARS = ['<', '>', '!', '=']


def _grab_args():
    """Grab and return arguments"""
    parser = argparse.ArgumentParser(
        description='Check if project requirements have changed')

    parser.add_argument('env_dir', help='tox environment directory')
    return parser.parse_args()


def _extract_reqs(file_name, blacklist=None):
    blacklist = blacklist or {}
    content = open(file_name, 'rt').read()
    reqs = collections.defaultdict(tuple)
    parsed = requirement.parse(content)
    for name, entries in ((name, entries) for (name, entries) in parsed.items()
                          if (name and name not in blacklist)):
        list_reqs = [r for (r, line) in entries]
        # Strip the comments out before checking if there are duplicates
        list_reqs_stripped = [r._replace(comment='') for r in list_reqs]
        if len(list_reqs_stripped) != len(set(list_reqs_stripped)):
            print('Requirements file %s has duplicate entries for package '
                  '"%s: %r' % (file_name, name, list_reqs))
        reqs[name] = list_reqs
    return reqs


def _extract_qualifier_version(specifier):
    index = 1
    # Find qualifier (one or two chars).
    if specifier[0] in QUALIFIER_CHARS and specifier[1] in QUALIFIER_CHARS:
        index = 2
    qualifier = specifier[:index]
    version = pkg_version.Version(specifier[index:])
    return qualifier, version


def main():
    args = _grab_args()

    # Build a list of requirements from the global list in the
    # openstack/requirements project so we can match them to the changes
    env_dir = args.env_dir
    req_dir = env_dir + '/src/os-requirements/'
    global_reqs = _extract_reqs(req_dir + '/global-requirements.txt')
    blacklist = _extract_reqs(req_dir + '/blacklist.txt')

    # Build a list of project requirements.
    failed = False
    local_dir = os.getcwd()
    for file_name in PROJECT_REQUIREMENTS_FILES:
        print('Validating requirements file "%s"' % file_name)
        proj_reqs = _extract_reqs(local_dir + '/' + file_name,
                                  blacklist=blacklist)

        for name, req in proj_reqs.items():
            global_req = global_reqs.get(name)
            if not global_req:
                continue
            global_req = global_req[0]
            req = req[0]
            if not global_req.specifiers:
                continue

            specifiers = global_req.specifiers.split(',')
            for spec in specifiers:
                _, req_version = _extract_qualifier_version(req.specifiers)
                g_qualifier, g_version = _extract_qualifier_version(spec)
                if g_qualifier == '!=' and g_version == req_version:
                    print('Package "%s" version %s is not compatible' %
                          (name, req_version))
                    failed = True
                if g_qualifier == '>=' and g_version > req_version:
                    print('Package "%s" version %s outdated, minimum version '
                          '%s' % (name, req_version, g_version))
                    failed = True

    if failed:
        print('Incompatible requirement found!')
        sys.exit(1)
    print('Updated requirements match openstack/requirements')


if __name__ == '__main__':
    main()