upgrade_strategy 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. for tag in temp_tags:
  12. tag = tag.split('.')
  13. for i in range(len(tag)):
  14. # Ignore the first two parts as they are supposed to have leading zeros
  15. if i in [0, 1]:
  16. continue
  17. # Add leading zero to single digit numbers
  18. if len(tag[i]) == 1:
  19. tag[i] = '0' + tag[i]
  20. tag = '.'.join(tag)
  21. tags[tag] = tag
  22. version = datetime_versioning(list(tags), '%y.%m.%d.%H.%M')
  23. if not version:
  24. return {}
  25. cleanVersion = ""
  26. for idx, part in enumerate(version.split('.')):
  27. # Ignore the first two parts as they are supposed to have leading zeros
  28. if idx in [0, 1]:
  29. cleanVersion += part + '.'
  30. continue
  31. # Remove leading zero
  32. cleanVersion += part.lstrip('0') + '.'
  33. # Remove trailing dot
  34. cleanVersion = cleanVersion.rstrip('.')
  35. return {
  36. 'tags': {key: cleanVersion},
  37. 'app_version': cleanVersion,
  38. }
  39. if __name__ == '__main__':
  40. try:
  41. versions_json = json.loads(sys.stdin.read())
  42. except ValueError:
  43. raise ValueError('Invalid json specified')
  44. print(json.dumps(newer_mapping(versions_json)))