|
| 1 | +import operator |
| 2 | +import re |
| 3 | +from collections import namedtuple |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +base_url = 'https://download.docker.com/linux/static/{0}/x86_64/' |
| 8 | +categories = [ |
| 9 | + 'edge', |
| 10 | + 'stable', |
| 11 | + 'test' |
| 12 | +] |
| 13 | + |
| 14 | + |
| 15 | +class Version(namedtuple('_Version', 'major minor patch rc edition')): |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def parse(cls, version): |
| 19 | + edition = None |
| 20 | + version = version.lstrip('v') |
| 21 | + version, _, rc = version.partition('-') |
| 22 | + if rc: |
| 23 | + if 'rc' not in rc: |
| 24 | + edition = rc |
| 25 | + rc = None |
| 26 | + elif '-' in rc: |
| 27 | + edition, rc = rc.split('-') |
| 28 | + |
| 29 | + major, minor, patch = version.split('.', 3) |
| 30 | + return cls(major, minor, patch, rc, edition) |
| 31 | + |
| 32 | + @property |
| 33 | + def major_minor(self): |
| 34 | + return self.major, self.minor |
| 35 | + |
| 36 | + @property |
| 37 | + def order(self): |
| 38 | + """Return a representation that allows this object to be sorted |
| 39 | + correctly with the default comparator. |
| 40 | + """ |
| 41 | + # rc releases should appear before official releases |
| 42 | + rc = (0, self.rc) if self.rc else (1, ) |
| 43 | + return (int(self.major), int(self.minor), int(self.patch)) + rc |
| 44 | + |
| 45 | + def __str__(self): |
| 46 | + rc = '-{}'.format(self.rc) if self.rc else '' |
| 47 | + edition = '-{}'.format(self.edition) if self.edition else '' |
| 48 | + return '.'.join(map(str, self[:3])) + edition + rc |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + results = set() |
| 53 | + for url in [base_url.format(cat) for cat in categories]: |
| 54 | + res = requests.get(url) |
| 55 | + content = res.text |
| 56 | + versions = [ |
| 57 | + Version.parse( |
| 58 | + v.strip('"').lstrip('docker-').rstrip('.tgz').rstrip('-x86_64') |
| 59 | + ) for v in re.findall( |
| 60 | + r'"docker-[0-9]+\.[0-9]+\.[0-9]+-.*tgz"', content |
| 61 | + ) |
| 62 | + ] |
| 63 | + sorted_versions = sorted( |
| 64 | + versions, reverse=True, key=operator.attrgetter('order') |
| 65 | + ) |
| 66 | + latest = sorted_versions[0] |
| 67 | + results.add(str(latest)) |
| 68 | + print(' '.join(results)) |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + main() |
0 commit comments