1 # Copyright (c) 2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from packaging import version as pkg_version
21 from openstack_requirements import requirement
24 PROJECT_REQUIREMENTS_FILES = ['requirements.txt']
25 QUALIFIER_CHARS = ['<', '>', '!', '=']
29 """Grab and return arguments"""
30 parser = argparse.ArgumentParser(
31 description='Check if project requirements have changed')
33 parser.add_argument('env_dir', help='tox environment directory')
34 return parser.parse_args()
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
54 def _extract_qualifier_version(specifier):
56 # Find qualifier (one or two chars).
57 if specifier[0] in QUALIFIER_CHARS and specifier[1] in QUALIFIER_CHARS:
59 qualifier = specifier[:index]
60 version = pkg_version.Version(specifier[index:])
61 return qualifier, version
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')
74 # Build a list of project requirements.
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,
82 for name, req in proj_reqs.items():
83 global_req = global_reqs.get(name)
86 global_req = global_req[0]
88 if not global_req.specifiers:
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' %
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))
105 print('Incompatible requirement found!')
107 print('Updated requirements match openstack/requirements')
110 if __name__ == '__main__':