11cb0ef41Sopenharmony_ci#!/usr/bin/python3
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciimport itertools
41cb0ef41Sopenharmony_ciimport os
51cb0ef41Sopenharmony_ciimport re
61cb0ef41Sopenharmony_ciimport subprocess
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciHTML = r'''
91cb0ef41Sopenharmony_ci<!DOCTYPE html>
101cb0ef41Sopenharmony_ci<html>
111cb0ef41Sopenharmony_ci  <head>
121cb0ef41Sopenharmony_ci    <link rel="stylesheet" href="http://libuv.org/styles/vendor.css">
131cb0ef41Sopenharmony_ci    <link rel="stylesheet" href="http://libuv.org/styles/main.css">
141cb0ef41Sopenharmony_ci    <style>
151cb0ef41Sopenharmony_ci    table {{
161cb0ef41Sopenharmony_ci      border-spacing: 0;
171cb0ef41Sopenharmony_ci    }}
181cb0ef41Sopenharmony_ci    body table {{
191cb0ef41Sopenharmony_ci      margin: 0 0 0 12pt;
201cb0ef41Sopenharmony_ci    }}
211cb0ef41Sopenharmony_ci    th, td {{
221cb0ef41Sopenharmony_ci      padding: 2pt;
231cb0ef41Sopenharmony_ci      text-align: left;
241cb0ef41Sopenharmony_ci      vertical-align: top;
251cb0ef41Sopenharmony_ci    }}
261cb0ef41Sopenharmony_ci    table table {{
271cb0ef41Sopenharmony_ci      border-collapse: initial;
281cb0ef41Sopenharmony_ci      padding: 0 0 16pt 0;
291cb0ef41Sopenharmony_ci    }}
301cb0ef41Sopenharmony_ci    table table tr:nth-child(even) {{
311cb0ef41Sopenharmony_ci      background-color: #777;
321cb0ef41Sopenharmony_ci    }}
331cb0ef41Sopenharmony_ci    </style>
341cb0ef41Sopenharmony_ci  </head>
351cb0ef41Sopenharmony_ci  <body>
361cb0ef41Sopenharmony_ci    <table>{groups}</table>
371cb0ef41Sopenharmony_ci  </body>
381cb0ef41Sopenharmony_ci</html>
391cb0ef41Sopenharmony_ci'''
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciGROUPS = r'''
421cb0ef41Sopenharmony_ci<tr>
431cb0ef41Sopenharmony_ci  <td>{groups[0]}</td>
441cb0ef41Sopenharmony_ci  <td>{groups[1]}</td>
451cb0ef41Sopenharmony_ci  <td>{groups[2]}</td>
461cb0ef41Sopenharmony_ci  <td>{groups[3]}</td>
471cb0ef41Sopenharmony_ci</tr>
481cb0ef41Sopenharmony_ci'''
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciGROUP = r'''
511cb0ef41Sopenharmony_ci<table>
521cb0ef41Sopenharmony_ci  <tr>
531cb0ef41Sopenharmony_ci    <th>version</th>
541cb0ef41Sopenharmony_ci    <th>tarball</th>
551cb0ef41Sopenharmony_ci    <th>gpg</th>
561cb0ef41Sopenharmony_ci    <th>windows</th>
571cb0ef41Sopenharmony_ci  </tr>
581cb0ef41Sopenharmony_ci  {rows}
591cb0ef41Sopenharmony_ci</table>
601cb0ef41Sopenharmony_ci'''
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciROW = r'''
631cb0ef41Sopenharmony_ci<tr>
641cb0ef41Sopenharmony_ci  <td>
651cb0ef41Sopenharmony_ci    <a href="http://dist.libuv.org/dist/{tag}/">{tag}</a>
661cb0ef41Sopenharmony_ci  </td>
671cb0ef41Sopenharmony_ci  <td>
681cb0ef41Sopenharmony_ci    <a href="http://dist.libuv.org/dist/{tag}/libuv-{tag}.tar.gz">tarball</a>
691cb0ef41Sopenharmony_ci  </td>
701cb0ef41Sopenharmony_ci  <td>{maybe_gpg}</td>
711cb0ef41Sopenharmony_ci  <td>{maybe_exe}</td>
721cb0ef41Sopenharmony_ci</tr>
731cb0ef41Sopenharmony_ci'''
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciGPG = r'''
761cb0ef41Sopenharmony_ci<a href="http://dist.libuv.org/dist/{tag}/libuv-{tag}.tar.gz.sign">gpg</a>
771cb0ef41Sopenharmony_ci'''
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci# The binaries don't have a predictable name, link to the directory instead.
801cb0ef41Sopenharmony_ciEXE = r'''
811cb0ef41Sopenharmony_ci<a href="http://dist.libuv.org/dist/{tag}/">exe</a>
821cb0ef41Sopenharmony_ci'''
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cidef version(tag):
851cb0ef41Sopenharmony_ci  return list(map(int, re.match('^v(\d+)\.(\d+)\.(\d+)', tag).groups()))
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_cidef major_minor(tag):
881cb0ef41Sopenharmony_ci  return version(tag)[:2]
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_cidef row_for(tag):
911cb0ef41Sopenharmony_ci  maybe_gpg = ''
921cb0ef41Sopenharmony_ci  maybe_exe = ''
931cb0ef41Sopenharmony_ci  # We didn't start signing releases and producing Windows installers
941cb0ef41Sopenharmony_ci  # until v1.7.0.
951cb0ef41Sopenharmony_ci  if version(tag) >= version('v1.7.0'):
961cb0ef41Sopenharmony_ci    maybe_gpg = GPG.format(**locals())
971cb0ef41Sopenharmony_ci    maybe_exe = EXE.format(**locals())
981cb0ef41Sopenharmony_ci  return ROW.format(**locals())
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_cidef group_for(tags):
1011cb0ef41Sopenharmony_ci  rows = ''.join(row_for(tag) for tag in tags)
1021cb0ef41Sopenharmony_ci  return GROUP.format(rows=rows)
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci# Partition in groups of |n|.
1051cb0ef41Sopenharmony_cidef groups_for(groups, n=4):
1061cb0ef41Sopenharmony_ci  html = ''
1071cb0ef41Sopenharmony_ci  groups = groups[:] + [''] * (n - 1)
1081cb0ef41Sopenharmony_ci  while len(groups) >= n:
1091cb0ef41Sopenharmony_ci    html += GROUPS.format(groups=groups)
1101cb0ef41Sopenharmony_ci    groups = groups[n:]
1111cb0ef41Sopenharmony_ci  return html
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciif __name__ == '__main__':
1141cb0ef41Sopenharmony_ci  os.chdir(os.path.dirname(__file__))
1151cb0ef41Sopenharmony_ci  tags = subprocess.check_output(['git', 'tag'], text=True)
1161cb0ef41Sopenharmony_ci  tags = [tag for tag in tags.split('\n') if tag.startswith('v')]
1171cb0ef41Sopenharmony_ci  tags.sort(key=version, reverse=True)
1181cb0ef41Sopenharmony_ci  groups = [group_for(list(g)) for _, g in itertools.groupby(tags, major_minor)]
1191cb0ef41Sopenharmony_ci  groups = groups_for(groups)
1201cb0ef41Sopenharmony_ci  html = HTML.format(groups=groups).strip()
1211cb0ef41Sopenharmony_ci  html = re.sub('>\\s+<', '><', html)
1221cb0ef41Sopenharmony_ci  print(html)
123