summaryrefslogtreecommitdiff
path: root/util/.marauder/marauder/version.py
blob: 6ad09d9fe6dc070ad6f443c09e9d0f1256d11d35 (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
37
38
39
40
import os
from io import BytesIO
import re
from urllib.request import urlopen
import sys

default = re.compile(r'(?:([0-9]+(?:[.-_][0-9]+)+))')  # Version()

""" Takes URL string and guesses version """
def version(url):
    filename = os.path.basename(url)
    version = default.findall(filename)
    if not version:
        x = os.path.basename(os.path.dirname(url))
        version = default.findall(x)  # Go up one directory and try that.
        if version:
            return version[0]
        version = re.findall(r'\S*?(:?[0-9-]+)', filename)  # v40 or program-83 for example.
        if version:
            return version[0]
        if url.endswith(".git"):
            return None  # FIXME: this is too slow.
            # print(f"Finding version for {url}", file=sys.stderr)
            latest_release = os.popen(
                f"git ls-remote --tags {url} 2>/dev/null | sort -Vk2 | awk 'END{{ print $2 }}' | grep -oE '[0-9]+(\\.[0-9]+)+'"
            ).read().strip()
            latest_commit = os.popen(
                f"git ls-remote --tags {url} 2>/dev/null | sort -Vk2 | awk 'END{{ print $1 }}'"
            ).read().strip()
            if latest_release and latest_commit:
                version = f"{latest_release}-git-{latest_commit[:8]}"
            elif latest_commit:
                version = f"1.0-git-{latest_commit[:8]}"
            else:
                return None
            return version

        if not version:  # If still no result.
            return None
    return version[0]