upgrade_strategy 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = r'\d+\.\d+'
  7. ENUMS = {
  8. 'ocrImage': {
  9. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-SNAPSHOT-ocr-es7'),
  10. 'STRIP_TEXT': '-SNAPSHOT-ocr-es7'
  11. },
  12. 'noocrImage': {
  13. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-SNAPSHOT-noocr'),
  14. 'STRIP_TEXT': '-SNAPSHOT-noocr'
  15. },
  16. }
  17. def newer_mapping(image_tags):
  18. output = {
  19. "tags": {},
  20. "app_version": ""
  21. }
  22. for key in image_tags.keys():
  23. STRIP_TEXT = ENUMS[key].get('STRIP_TEXT', None) if key in ENUMS else None
  24. RE_STABLE_VERSION = ENUMS[key].get('RE_STABLE_VERSION', None) if key in ENUMS else None
  25. if (STRIP_TEXT is None) or (RE_STABLE_VERSION is None):
  26. continue
  27. tags = {t.strip(STRIP_TEXT): t for t in image_tags[key] if RE_STABLE_VERSION.fullmatch(t)}
  28. version = semantic_versioning(list(tags))
  29. if not version:
  30. continue
  31. if key == 'ocrImage':
  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)))