File: sync_requirements.py

package info (click to toggle)
rally-openstack 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,928 kB
  • sloc: python: 53,131; sh: 262; makefile: 38
file content (387 lines) | stat: -rw-r--r-- 13,972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env python
# All Rights Reserved.
#
#    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.

"""
Synchronizes, formats and prepares requirements to release(obtains and adds
maximum allowed version).
"""

import collections
import logging
import re
import sys
import textwrap

import pkg_resources
import requests


LOG = logging.getLogger(__name__)
if not LOG.handlers:
    LOG.addHandler(logging.StreamHandler())
    LOG.setLevel(logging.INFO)


GLOBAL_REQUIREMENTS_LOCATIONS = (
    "https://raw.githubusercontent.com/openstack/requirements/master/",
    "http://git.openstack.org/cgit/openstack/requirements/plain/"
)
RALLY_REQUIREMENTS_FILES = (
    "requirements.txt",
    "test-requirements.txt"
)
DO_NOT_TOUCH_TAG = "[do-not-touch]"


class Comment(object):
    def __init__(self, s=None, finished=False):
        self._comments = []
        self.is_finished = finished
        if s:
            self.append(s)

    def finish_him(self):
        self.is_finished = True

    def append(self, s):
        self._comments.append(s[1:].strip())

    def __str__(self):
        return textwrap.fill("\n".join(self._comments), width=80,
                             initial_indent="# ", subsequent_indent="# ")


_PYPI_CACHE = {}


class PYPIPackage(object):
    # NOTE(andreykurilin): one license can have different labels. Let's use
    #   unified variant.
    LICENSE_MAP = {"MIT license": "MIT",
                   "MIT License": "MIT",
                   "BSD License": "BSD",
                   "Apache 2.0": "Apache License, Version 2.0"}

    def __init__(self, package_name):
        self.package_name = package_name
        self._pypi_info = None
        self._pypi_license = None

    @property
    def pypi_info(self):
        if self._pypi_info is None:
            if self.package_name in _PYPI_CACHE:
                self._pypi_info = _PYPI_CACHE[self.package_name]
            else:
                resp = requests.get("https://pypi.org/pypi/%s/json" %
                                    self.package_name)
                if resp.status_code != 200:
                    print("An error occurred while checking '%s' package at "
                          "pypi." % self.package_name)
                    raise Exception(resp.text)
                self._pypi_info = resp.json()

                # let's cache it for the case when we need to sync requirements
                # and update upper constrains
                _PYPI_CACHE[self.package_name] = self._pypi_info
        return self._pypi_info

    @property
    def pypi_version(self):
        return self.pypi_info["info"]["version"]

    @property
    def pypi_license(self):
        if self._pypi_license is None:
            if self.pypi_info["info"]["license"]:
                self._pypi_license = self.pypi_info["info"]["license"]
            else:
                # try to parse classifiers
                prefix = "License :: OSI Approved :: "
                classifiers = [c[len(prefix):]
                               for c in self.pypi_info["info"]["classifiers"]
                               if c.startswith(prefix)]
                self._pypi_license = "/".join(classifiers)
            self._license = self.LICENSE_MAP.get(self._pypi_license,
                                                 self._pypi_license)
            if self._pypi_license == "UNKNOWN":
                self._pypi_license = None
        return self._license

    def __eq__(self, other):
        return (isinstance(other, PYPIPackage)
                and self.package_name == other.package_name)


class Requirement(PYPIPackage):
    RE_NAME = re.compile(r"[a-zA-Z0-9-._]+")
    RE_CONST_VERSION = re.compile(r"==[a-zA-Z0-9.]+")
    RE_MIN_VERSION = re.compile(r">=?[a-zA-Z0-9.]+")
    RE_MAX_VERSION = re.compile(r"<=?[a-zA-Z0-9.]+")
    RE_NE_VERSIONS = re.compile(r"!=[a-zA-Z0-9.]+")

    def __init__(self, package_name, version):
        super(Requirement, self).__init__(package_name)
        self.version = version
        self.do_not_touch = False

    def sync_max_version_with_pypy(self):
        if isinstance(self.version, dict) and not self.do_not_touch:
            self.version["max"] = "<=%s" % self.pypi_version

    @classmethod
    def parse_line(cls, line):
        match = cls.RE_NAME.match(line)
        if match:
            name = match.group()
            # remove name
            versions = line.replace(name, "")
            # remove comments
            versions = versions.split("#")[0]
            # remove python classifiers
            versions = versions.split(";")[0].strip()
            if not cls.RE_CONST_VERSION.match(versions):
                versions = versions.strip().split(",")
                min_version = None
                max_version = None
                ne_versions = []
                for version in versions:
                    if cls.RE_MIN_VERSION.match(version):
                        if min_version:
                            raise Exception("Found several min versions for "
                                            "%s package." % name)
                        min_version = version
                    elif cls.RE_MAX_VERSION.match(version):
                        if max_version:
                            raise Exception("Found several max versions for "
                                            "%s package." % name)
                        max_version = version
                    elif cls.RE_NE_VERSIONS.match(version):
                        ne_versions.append(version)
                versions = {"min": min_version,
                            "max": max_version,
                            "ne": ne_versions}
            return cls(name, versions)

    def __str__(self):
        if isinstance(self.version, dict):
            version = []

            min_equal_to_max = False
            if self.version["min"] and self.version["max"]:
                if (
                        self.version["min"].startswith(">=")
                        and self.version["max"].startswith("<=")
                        and self.version["min"][2:] == self.version["max"][2:]
                ):
                    # min and max versions are equal there is no need to write
                    # both of them
                    min_equal_to_max = True
                    version.append("==%s" % self.version["min"][2:])

            if not min_equal_to_max and self.version["min"]:
                version.append(self.version["min"])

            if not min_equal_to_max and self.version["ne"]:
                version.extend(self.version["ne"])

            if not min_equal_to_max and self.version["max"]:
                version.append(self.version["max"])

            version = ",".join(version)
        else:
            if self.do_not_touch:
                version = self.version
            else:
                # remove const version
                version = ">=%s" % self.version[2:]

        string = "%s%s" % (self.package_name, version)
        if self.pypi_license:
            # NOTE(andreykurilin): When I start implementation of this script,
            #   python-keystoneclient dependency string took around ~45-55
            #   chars, so let's use this length as indent. Feel free to modify
            #   it to lower or greater value.
            magic_number = 55
            if len(string) < magic_number:
                indent = magic_number - len(string)
            else:
                indent = 2
            string += " " * indent + "# " + self.pypi_license
        return string

    def __eq__(self, other):
        return (isinstance(other, self.__class__)
                and self.package_name == other.package_name)

    def __ne__(self, other):
        return not self.__eq__(other)


class UpperConstraint(PYPIPackage):

    RE_LINE = re.compile(
        r"(?P<package_name>[a-zA-Z0-9-._]+)===(?P<version>[a-zA-Z0-9.]+)")

    def __init__(self, package_name, version=None):
        super(UpperConstraint, self).__init__(package_name)
        self._version = version

    def __str__(self):
        return "%s===%s" % (self.package_name, self.version)

    @property
    def version(self):
        if self._version is None:
            self._version = self.pypi_version
        return self._version

    @classmethod
    def parse_line(cls, line):
        match = cls.RE_LINE.match(line)
        if match:
            return cls(**match.groupdict())

    def update(self, version):
        self._version = version

    def fetch_version(self):
        self._version = None
        return self.version


def parse_data(raw_data, include_comments=True, dependency_cls=Requirement):
    # first elem is None to simplify checks of last elem in requirements
    requirements = [None]
    for line in raw_data.split("\n"):
        if line.startswith("#"):
            if not include_comments:
                continue

            if getattr(requirements[-1], "is_finished", True):
                requirements.append(Comment())

            requirements[-1].append(line)
        elif line == "":
            # just empty line
            if isinstance(requirements[-1], Comment):
                requirements[-1].finish_him()
            requirements.append(Comment(finished=True))
        else:
            if (isinstance(requirements[-1], Comment)
                    and not requirements[-1].is_finished):
                requirements[-1].finish_him()

            # parse_line
            dep = dependency_cls.parse_line(line)
            if dep:
                if (isinstance(requirements[-1], Comment)
                        and DO_NOT_TOUCH_TAG in str(requirements[-1])):
                    dep.do_not_touch = True
                requirements.append(dep)

    for i in range(len(requirements) - 1, 0, -1):
        # remove empty lines at the end of file
        if isinstance(requirements[i], Comment):
            if str(requirements[i]) == "":
                requirements.pop(i)
        else:
            break
    return collections.OrderedDict(
        (v if isinstance(v, Comment) else v.package_name, v)
        for v in requirements if v)


def _fetch_from_gr(filename):
    """Try to fetch data from OpenStack global-requirements repo"""
    for i in range(0, len(GLOBAL_REQUIREMENTS_LOCATIONS)):
        url = GLOBAL_REQUIREMENTS_LOCATIONS[i] + filename
        LOG.debug("Try to obtain %s from %s" % (filename, url))
        try:
            return requests.get(url).text
        except requests.ConnectionError as e:
            LOG.exception(e)
    raise Exception("Unable to obtain %s" % filename)


def _write_requirements(filename, requirements):
    """Saves requirements to file."""
    if isinstance(requirements, dict):
        requirements = requirements.values()
    LOG.info("Saving requirements to %s." % filename)
    with open(filename, "w") as f:
        for entity in requirements:
            f.write(str(entity))
            f.write("\n")


def sync_requirements():
    """Synchronizes Rally requirements with OpenStack global-requirements."""
    LOG.info("Obtaining global-requirements of OpenStack...")
    raw_gr = _fetch_from_gr("global-requirements.txt")

    # NOTE(andreykurilin): global-requirements includes comments which can be
    #   unrelated to Rally project, so let's just ignore them
    gr = parse_data(raw_gr, include_comments=False)
    for file_name in RALLY_REQUIREMENTS_FILES:
        LOG.debug("Processing '%s'." % file_name)
        with open(file_name) as f:
            requirements = parse_data(f.read())
        for name, req in requirements.items():
            if isinstance(req, Requirement) and not req.do_not_touch:
                if name in gr:
                    req.version = gr[req.package_name].version
                else:
                    # it not g-r requirements
                    if isinstance(req.version, dict):
                        req.version["max"] = None
        _write_requirements(file_name, requirements)


def update_upper_constraints():
    """Obtains latest version of packages and put them to upper-constraints."""
    LOG.info("Obtaining upper-constrains from OpenStack...")
    raw_g_uc = _fetch_from_gr("upper-constraints.txt")
    # NOTE(andreykurilin): global OpenStack upper-constraints file includes
    #   comments which can be unrelated to Rally project, so let's just ignore
    #   them.
    global_uc = parse_data(raw_g_uc,
                           include_comments=False,
                           dependency_cls=UpperConstraint)

    our_uc = [UpperConstraint(package_name=p.project_name, version=p.version)
              for p in pkg_resources.working_set
              # do not include the current package at u-c
              if p.project_name != "rally-openstack"]

    for package in our_uc:
        if package.package_name in global_uc:
            # we cannot use whatever we want versions in CI. OpenStack CI
            # ignores versions listed in requirements of
            # particular project and use versions from global u-c file.
            # It means that we need to suggest to use the same versions
            package.update(global_uc[package.package_name].version)

    our_uc = sorted(our_uc, key=lambda o: o.package_name.upper())
    _write_requirements("upper-constraints.txt", our_uc)


def main():
    sync_requirements()
    update_upper_constraints()


if __name__ == "__main__":
    sys.exit(main())