upgrade_strategy 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python3
  2. import json
  3. import re
  4. import sys
  5. from catalog_update.upgrade_strategy import semantic_versioning
  6. RE_STABLE_VERSION_BASE = re.compile(r'\d+\.\d+\.\d+')
  7. ENUMS = {
  8. 'image': {
  9. 'RE_STABLE_VERSION': RE_STABLE_VERSION_BASE,
  10. },
  11. 'plusImage': {
  12. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-plus'),
  13. 'STRIP_TEXT': '-plus'
  14. },
  15. }
  16. def newer_mapping(image_tags):
  17. output = {
  18. "tags": {},
  19. "app_version": ""
  20. }
  21. for key in image_tags.keys():
  22. STRIP_TEXT = ENUMS[key].get('STRIP_TEXT', None) if key in ENUMS else None
  23. RE_STABLE_VERSION = ENUMS[key].get('RE_STABLE_VERSION', None) if key in ENUMS else None
  24. if (STRIP_TEXT is None) or (RE_STABLE_VERSION is None):
  25. continue
  26. tags = {t.strip(STRIP_TEXT): t for t in image_tags[key] if RE_STABLE_VERSION.fullmatch(t)}
  27. version = semantic_versioning(list(tags))
  28. if not version:
  29. continue
  30. # 16 is the "default" (Also tied to latest tag)
  31. if key == 'image':
  32. output['app_version'] = version
  33. output['tags'][key] = tags[version]
  34. return output
  35. if __name__ == '__main__':
  36. try:
  37. versions_json = json.loads(sys.stdin.read())
  38. except ValueError:
  39. raise ValueError('Invalid json specified')
  40. print(json.dumps(newer_mapping(versions_json)))