upgrade_strategy 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/python3
  2. import json
  3. import sys
  4. import re
  5. from catalog_update.upgrade_strategy import datetime_versioning
  6. RE_STABLE_VERSION = re.compile(r'^(\d+\.){4}\d+$')
  7. def newer_mapping(image_tags):
  8. key = list(image_tags.keys())[0]
  9. temp_tags = {t: t for t in image_tags[key] if RE_STABLE_VERSION.fullmatch(t)}
  10. tags = {}
  11. # Format tags so they can be parsed by datetime_versioning
  12. for tag in temp_tags:
  13. tag = tag.split('.')
  14. for i in range(len(tag)):
  15. # Ignore the first two parts as they are supposed to have leading zeros
  16. if i in [0, 1]:
  17. continue
  18. # Add leading zero to single digit numbers
  19. if len(tag[i]) == 1:
  20. tag[i] = '0' + tag[i]
  21. tag = '.'.join(tag)
  22. tags[tag] = tag
  23. # Get the latest version
  24. version = datetime_versioning(list(tags), '%y.%m.%H.%M.%S')
  25. if not version:
  26. return {}
  27. cleanVersion = ""
  28. # Covert the tag back to the original format
  29. for idx, part in enumerate(version.split('.')):
  30. # Ignore the first two parts as they are supposed to have leading zeros
  31. if idx in [0, 1]:
  32. cleanVersion += part + '.'
  33. continue
  34. if len(part) == 2 and part[0] == '0':
  35. cleanVersion += part[1] + '.'
  36. # Remove trailing dot
  37. cleanVersion = cleanVersion.rstrip('.')
  38. return {
  39. 'tags': {key: cleanVersion},
  40. 'app_version': cleanVersion,
  41. }
  42. if __name__ == '__main__':
  43. try:
  44. versions_json = json.loads(sys.stdin.read())
  45. except ValueError:
  46. raise ValueError('Invalid json specified')
  47. print(json.dumps(newer_mapping(versions_json)))