version_mgr.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2020 Intel Corp.
  3. # SPDX-License-Identifier: Apache-2.0
  4. """
  5. Syntax of file:
  6. [
  7. {
  8. "version": "<commit>",
  9. "date": "<date>",
  10. "weekly: False,
  11. },
  12. ]
  13. """
  14. import json
  15. import argparse
  16. import urllib.request
  17. import os
  18. from git import Git
  19. from datetime import datetime
  20. VERSIONS_FILE = "versions.json"
  21. def parse_args():
  22. parser = argparse.ArgumentParser(
  23. description="Manage versions to be tested.")
  24. parser.add_argument('-l', '--list', action="store_true",
  25. help="List all published versions")
  26. parser.add_argument('-u', '--update',
  27. help="Update versions file from tree.")
  28. parser.add_argument('-L', '--latest', action="store_true",
  29. help="Get latest published version")
  30. parser.add_argument('-w', '--weekly', action="store_true",
  31. help="Mark as weekly")
  32. parser.add_argument('-v', '--verbose', action="store_true",
  33. help="Verbose output")
  34. return parser.parse_args()
  35. def get_versions():
  36. data = None
  37. if not os.path.exists('versions.json'):
  38. url = 'https://testing.zephyrproject.org/daily_tests/versions.json'
  39. urllib.request.urlretrieve(url, 'versions.json')
  40. with open("versions.json", "r") as fp:
  41. data = json.load(fp)
  42. return data
  43. def handle_compat(item):
  44. item_compat = {}
  45. if isinstance(item, str):
  46. item_compat['version'] = item
  47. item_compat['weekly'] = False
  48. item_compat['date'] = None
  49. else:
  50. item_compat = item
  51. return item_compat
  52. def show_versions():
  53. data = get_versions()
  54. for item in data:
  55. item_compat = handle_compat(item)
  56. is_weekly = item_compat.get('weekly', False)
  57. wstr = ""
  58. datestr = ""
  59. if args.verbose:
  60. if is_weekly:
  61. wstr = "(marked for weekly testing)"
  62. if item_compat.get('date'):
  63. pdate = datetime.strptime(item_compat['date'], '%Y-%m-%dT%H:%M:%S.%f')
  64. date = pdate.strftime("%b %d %Y %H:%M:%S")
  65. datestr = f"published on {date}"
  66. print(f"- {item_compat['version']} {datestr} {wstr}")
  67. else:
  68. print(f"{item_compat['version']}")
  69. def show_latest():
  70. data = get_versions()
  71. latest = data[-1]
  72. item_compat = handle_compat(latest)
  73. ver = item_compat.get("version")
  74. date = item_compat.get("date", False)
  75. is_weekly = item_compat.get('weekly')
  76. datestr = ""
  77. if date:
  78. datestr = f"published on {date}"
  79. if args.verbose:
  80. print(f"Latest version is {ver} {datestr}")
  81. if args.verbose and is_weekly:
  82. print("This version is marked for weekly testing.")
  83. if not args.verbose:
  84. print(f"{ver}")
  85. def update(git_tree, is_weekly=False):
  86. g = Git(git_tree)
  87. today = datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
  88. version = g.describe("--abbrev=12")
  89. published = False
  90. data = get_versions()
  91. if not is_weekly:
  92. wday = datetime.today().strftime('%A')
  93. if wday == 'Monday':
  94. is_weekly = True
  95. found = list(filter(lambda item: (isinstance(item, dict) and
  96. item.get('version') == version) or item == version, data))
  97. if found:
  98. published = True
  99. print("version already published")
  100. else:
  101. print(f"New version {version}, adding to file...")
  102. if data and not published:
  103. with open(VERSIONS_FILE, "w") as versions:
  104. item = {}
  105. item['version'] = version
  106. item['date'] = today
  107. item['weekly'] = is_weekly
  108. data.append(item)
  109. json.dump(data, versions)
  110. def main():
  111. global args
  112. args = parse_args()
  113. if args.update:
  114. update(args.update, args.weekly)
  115. elif args.list:
  116. show_versions()
  117. elif args.latest:
  118. show_latest()
  119. else:
  120. print("You did not specify any options")
  121. if __name__ == "__main__":
  122. main()