Merge "Add "securityContext" parameter in Kubernetes context"
[yardstick.git] / tools / os-requirements-check.py
1 # Copyright (c) 2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import argparse
16 import collections
17 import os
18 from packaging import version as pkg_version
19 import sys
20
21 from openstack_requirements import requirement
22
23
24 PROJECT_REQUIREMENTS_FILES = ['requirements.txt']
25 QUALIFIER_CHARS = ['<', '>', '!', '=']
26
27
28 def _grab_args():
29     """Grab and return arguments"""
30     parser = argparse.ArgumentParser(
31         description='Check if project requirements have changed')
32
33     parser.add_argument('env_dir', help='tox environment directory')
34     return parser.parse_args()
35
36
37 def _extract_reqs(file_name, blacklist=None):
38     blacklist = blacklist or {}
39     content = open(file_name, 'rt').read()
40     reqs = collections.defaultdict(tuple)
41     parsed = requirement.parse(content)
42     for name, entries in ((name, entries) for (name, entries) in parsed.items()
43                           if (name and name not in blacklist)):
44         list_reqs = [r for (r, line) in entries]
45         # Strip the comments out before checking if there are duplicates
46         list_reqs_stripped = [r._replace(comment='') for r in list_reqs]
47         if len(list_reqs_stripped) != len(set(list_reqs_stripped)):
48             print('Requirements file %s has duplicate entries for package '
49                   '"%s: %r' % (file_name, name, list_reqs))
50         reqs[name] = list_reqs
51     return reqs
52
53
54 def _extract_qualifier_version(specifier):
55     index = 1
56     # Find qualifier (one or two chars).
57     if specifier[0] in QUALIFIER_CHARS and specifier[1] in QUALIFIER_CHARS:
58         index = 2
59     qualifier = specifier[:index]
60     version = pkg_version.Version(specifier[index:])
61     return qualifier, version
62
63
64 def main():
65     args = _grab_args()
66
67     # Build a list of requirements from the global list in the
68     # openstack/requirements project so we can match them to the changes
69     env_dir = args.env_dir
70     req_dir = env_dir + '/src/os-requirements/'
71     global_reqs = _extract_reqs(req_dir + '/global-requirements.txt')
72     blacklist = _extract_reqs(req_dir + '/blacklist.txt')
73
74     # Build a list of project requirements.
75     failed = False
76     local_dir = os.getcwd()
77     for file_name in PROJECT_REQUIREMENTS_FILES:
78         print('Validating requirements file "%s"' % file_name)
79         proj_reqs = _extract_reqs(local_dir + '/' + file_name,
80                                   blacklist=blacklist)
81
82         for name, req in proj_reqs.items():
83             global_req = global_reqs.get(name)
84             if not global_req:
85                 continue
86             global_req = global_req[0]
87             req = req[0]
88             if not global_req.specifiers:
89                 continue
90
91             specifiers = global_req.specifiers.split(',')
92             for spec in specifiers:
93                 _, req_version = _extract_qualifier_version(req.specifiers)
94                 g_qualifier, g_version = _extract_qualifier_version(spec)
95                 if g_qualifier == '!=' and g_version == req_version:
96                     print('Package "%s" version %s is not compatible' %
97                           (name, req_version))
98                     failed = True
99                 if g_qualifier == '>=' and g_version > req_version:
100                     print('Package "%s" version %s outdated, minimum version '
101                           '%s' % (name, req_version, g_version))
102                     failed = True
103
104     if failed:
105         print('Incompatible requirement found!')
106         sys.exit(1)
107     print('Updated requirements match openstack/requirements')
108
109
110 if __name__ == '__main__':
111     main()