summarylogtreecommitdiffstats
path: root/nvcheck.py
blob: 198032fdb1db5bd49189d89ca1872821d87706d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python

import json
import urllib.request
import re
from pyalpm import vercmp
from functools import cmp_to_key

github_repo = "yairm210/Unciv"
from_pattern = r'(.*)-patch(\d+)'
to_pattern = r'\1.\2'
prefix = ''

def custom_preproc(vers):
  if len(vers.split('.')) == 3:
    return vers + '.REL'
  else:
    return vers

def remove_prefix(s, prefix):
  if s.startswith(prefix):
    return s[len(prefix):]
  else:
    return s

# Check github tags
# Replace by regex before sorting. This is not currently supported by nvchecker
req = urllib.request.Request('https://api.github.com/repos/{}/tags'.format(github_repo))
body = None
with urllib.request.urlopen(req) as res:
  body = json.load(res)

versions = [ custom_preproc(remove_prefix(re.sub(from_pattern, to_pattern, it['name']), prefix)) for it in body ]
versions.sort(key=cmp_to_key(vercmp))

print(versions[-1])