summaryrefslogtreecommitdiff
path: root/scripts/versions.py
blob: 77aaf4f1825c8c87ed3765950eb1d1a341c18b90 (plain)
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
import operator
import re
from collections import namedtuple

import requests

base_url = 'https://download.docker.com/linux/static/{0}/x86_64/'
categories = [
    'edge',
    'stable',
    'test'
]


class Version(namedtuple('_Version', 'major minor patch rc edition')):

    @classmethod
    def parse(cls, version):
        edition = None
        version = version.lstrip('v')
        version, _, rc = version.partition('-')
        if rc:
            if 'rc' not in rc:
                edition = rc
                rc = None
            elif '-' in rc:
                edition, rc = rc.split('-')

        major, minor, patch = version.split('.', 3)
        return cls(major, minor, patch, rc, edition)

    @property
    def major_minor(self):
        return self.major, self.minor

    @property
    def order(self):
        """Return a representation that allows this object to be sorted
        correctly with the default comparator.
        """
        # rc releases should appear before official releases
        rc = (0, self.rc) if self.rc else (1, )
        return (int(self.major), int(self.minor), int(self.patch)) + rc

    def __str__(self):
        rc = '-{}'.format(self.rc) if self.rc else ''
        edition = '-{}'.format(self.edition) if self.edition else ''
        return '.'.join(map(str, self[:3])) + edition + rc


def main():
    results = set()
    for url in [base_url.format(cat) for cat in categories]:
        res = requests.get(url)
        content = res.text
        versions = [
            Version.parse(
                v.strip('"').lstrip('docker-').rstrip('.tgz').rstrip('-x86_64')
            ) for v in re.findall(
                r'"docker-[0-9]+\.[0-9]+\.[0-9]+-.*tgz"', content
            )
        ]
        sorted_versions = sorted(
            versions, reverse=True, key=operator.attrgetter('order')
        )
        latest = sorted_versions[0]
        results.add(str(latest))
    print(' '.join(results))

if __name__ == '__main__':
    main()