upgrade_strategy_disable 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 idx == 2:
  35. # Preserve the trailing zero on third part, but trim the leading zero
  36. cleanVersion += part.lstrip('0') + '.'
  37. continue
  38. if len(part) == 2 and part[0] == '0':
  39. cleanVersion += part[1] + '.'
  40. # Remove trailing dot
  41. cleanVersion = cleanVersion.rstrip('.')
  42. return {
  43. 'tags': {key: cleanVersion},
  44. 'app_version': cleanVersion,
  45. }
  46. if __name__ == '__main__':
  47. try:
  48. versions_json = json.loads(sys.stdin.read())
  49. except ValueError:
  50. raise ValueError('Invalid json specified')
  51. print(json.dumps(newer_mapping(versions_json)))