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