check_upstream.py 8.3 KB
Newer Older
S
Shinwell Hu 已提交
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 41 42 43 44 45 46 47 48 49 50 51 52 53
#!/usr/bin/python3

import http.cookiejar
import urllib.request
import re
import yaml
import json
import sys
import subprocess
import requests

from urllib.parse import urljoin
from datetime import datetime

time_format = "%Y-%m-%dT%H:%M:%S%z"

def eprint(*args, **kwargs):
    print("DEBUG: ", *args, file=sys.stderr, **kwargs)

def load_last_query_result(info, force_reload=False):
    if force_reload:
        last_query = info.pop("last_query")
        eprint("{repo} > Force reload".format(repo=info["src_repo"]))
        return ""
    else:
        if "last_query" in info.keys():
            last_query = info.pop("last_query")
            #age = datetime.now() - datetime.strptime(last_query["time_stamp"], time_format)
            age = datetime.now() - last_query["time_stamp"].replace(tzinfo=None)
            if age.days < 7:
                eprint("{repo} > Reuse Last Query".format(repo=info["src_repo"]))
                return last_query["raw_data"]
            else:
                eprint("{repo} > Last Query Too Old.".format(repo=info["src_repo"]))
                return ""
        else:
            return ""

def clean_tags(tags, info):

    if info.get("tag_pattern", "") != "":
        pattern_regex = re.compile(info["tag_pattern"])
        result_list = [pattern_regex.sub("\\1", x) for x in tags]
    elif info.get("tag_prefix", "") != "":
        prefix_regex = re.compile(info["tag_prefix"])
        result_list = [prefix_regex.sub("", x) for x in tags]
    else:
        result_list = tags

    if info.get("seperator", ".") != ".":
        seperator_regex = re.compile(info["seperator"])
        result_list = [seperator_regex.sub(".", x) for x in result_list]

54 55
    result_list = [x for x in result_list if x[0].isdigit()]

S
Shinwell Hu 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    return result_list


def dirty_redirect_tricks(url, resp):
    cookie = set()
    href = ""
    need_trick = False
    for line in resp.splitlines():
        line = line.strip()
        if line.startswith("Redirecting"):
            eprint("Redirecting with document.cookie")
            need_trick = True
        m = re.search("document\.cookie=\"(.*)\";", line)
        if m:
            cookie = cookie | set(m.group(1).split(';'))
        m = re.search("document\.location\.href=\"(.*)\";", line)
        if m:
            href = m.group(1)
    new_url = urljoin(url, href)
    if "" in cookie: cookie.remove("") 
    return need_trick, new_url, list(cookie)


def check_hg(info):
    resp = load_last_query_result(info)
    if resp == "":
        headers = {
                'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64)'
                }
        url = urljoin(info["src_repo"] + "/", "json-tags")
        resp = requests.get(url, headers=headers)
        need_trick, url, cookie = dirty_redirect_tricks(url, resp.text)
        if need_trick:
            # I dont want to introduce another dependency on requests
            # but urllib handling cookie is outragely complex
            c_dict = {}
            for c in cookie:
                k, v = c.split('=')
                c_dict[k] = v
            resp = requests.get(url, headers=headers, cookies=c_dict)
S
Shinwell Hu 已提交
96
            resp = resp.text
S
Shinwell Hu 已提交
97 98 99

    last_query = {}
    last_query["time_stamp"] = datetime.now()
S
Shinwell Hu 已提交
100
    last_query["raw_data"] = resp
S
Shinwell Hu 已提交
101 102
    info["last_query"] = last_query
    # try and except ?
S
Shinwell Hu 已提交
103
    tags_json = json.loads(resp)
S
Shinwell Hu 已提交
104 105 106 107 108 109
    sort_tags = tags_json["tags"]
    sort_tags.sort(reverse=True, key=lambda x: x['date'][0])
    result_list = [tag['tag'] for tag in sort_tags]
    result_list = clean_tags(result_list, info)
    return result_list

S
Shinwell Hu 已提交
110
def check_metacpan(info):
S
Shinwell Hu 已提交
111 112
    resp = load_last_query_result(info)
    if resp == "":
S
Shinwell Hu 已提交
113 114 115 116 117 118 119 120 121 122 123 124
        headers = {
                'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64)'
                }
        url = urljoin("https://fastapi.metacpan.org/release/", info["src_repo"])
        resp = requests.get(url, headers=headers)
        resp = resp.text

    tags = []
    result_json = json.loads(resp)
    if result_json != {}:
        if "version" not in result_json.keys():
            eprint("{repo} > ERROR FOUND".format(repo=info["src_repo"]))
S
Shinwell Hu 已提交
125
            sys.exit(1)
S
Shinwell Hu 已提交
126 127 128 129 130
        else:
            tags.append(result_json["version"])
    else:
        eprint("{repo} found unsorted on cpan.metacpan.org".format(repo=info["src_repo"]))
        sys.exit(1)
S
Shinwell Hu 已提交
131

S
Shinwell Hu 已提交
132 133 134 135 136
    last_query = {}
    last_query["time_stamp"] = datetime.now()
    last_query["raw_data"] = resp
    info["last_query"] = last_query
    return tags
S
Shinwell Hu 已提交
137

S
Shinwell Hu 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
def check_pypi(info):
    resp = load_last_query_result(info)
    tags = []
    if resp == "":
        headers = {
                'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64)'
                }
        url = urljoin("https://pypi.org/pypi/", info["src_repo"] + "/json")
        resp = requests.get(url, headers=headers)
        resp = resp.text

    result_json = json.loads(resp)
    if result_json != {}:
        tags.append(result_json["info"]["version"])
    else:
        eprint("{repo} > No Response or JSON parse failed".format(repo=info["src_repo"]))
        sys.exit(1)
    return tags

def __check_subprocess(cmd_list):
    subp = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
    resp = subp.stdout.read().decode("utf-8")
    if subp.wait() != 0:
        eprint("{cmd} > encount errors".format(cmd=" ".join(cmd_list)))
        sys.exit(1)
    return resp

def __check_svn_helper(repo_url):
    eprint("{repo} > Using svn ls".format(repo=repo_url))
    cmd_list = ["/usr/bin/svn", "ls", "-v", repo_url]
    return __check_subprocess(cmd_list)

def __check_git_helper(repo_url):
    eprint("{repo} > Using git ls-remote".format(repo=repo_url))
    cmd_list = ["git", "ls-remote", "--tags", repo_url]
    return __check_subprocess(cmd_list)

def __svn_resp_to_tags(resp):
    tags = []
    for line in resp.splitlines():
        items = line.split()
        for item in items:
            if item[-1] == "/":
                tags.append(item[:-1])
                break
    return tags

def __git_resp_to_tags(resp):
S
Shinwell Hu 已提交
186 187 188 189 190 191
    tags = []
    pattern = re.compile("^([^ \t]*)[ \t]*refs\/tags\/([^ \t]*)")
    for line in resp.splitlines():
        m = pattern.match(line)
        if m:
            tags.append(m.group(2))
S
Shinwell Hu 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    return tags

def check_git (info):
    resp = load_last_query_result(info)
    if resp == "":
        resp = __check_git_helper(info["src_repo"])
        last_query={}
        last_query["time_stamp"] = datetime.now()
        last_query["raw_data"] = resp
        info["last_query"] = last_query

    tags = __git_resp_to_tags(resp)
    tags = clean_tags(tags, info)

    return tags

def check_github(info):
    resp = load_last_query_result(info)
    if info.get("query_type", "git-ls") != "git-ls":
        resp = ""

    repo_url = "https://github.com/" + info["src_repo"] + ".git"

    if resp == "":
        resp = __check_git_helper(repo_url)
        last_query = {}
        last_query["time_stamp"] = datetime.now()
        last_query["raw_data"] = resp
        info["last_query"] = last_query
        info["query_type"] = "git-ls"

    tags = __git_resp_to_tags(resp)
    tags = clean_tags(tags, info)
    return tags

def check_gnome(info):
    resp = load_last_query_result(info)
    repo_url = "https://gitlab.gnome.org/GNOME/"+info["src_repo"]+".git"

    if resp == "":
        resp = __check_git_helper(repo_url)
        last_query={}
        last_query["time_stamp"] = datetime.now()
        last_query["raw_data"] = resp
        info["last_query"] = last_query

    tags = __git_resp_to_tags(resp)
S
Shinwell Hu 已提交
239 240 241
    tags = clean_tags(tags, info)
    return tags

S
Shinwell Hu 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
def check_svn(info):
    resp = load_last_query_result(info)
    repo_url = info["src_repo"] + "/tags"
    if resp == "":
        resp = __check_svn_helper(repo_url)
        last_query = {}
        last_query["time_stamp"] = datetime.now()
        last_query["raw_data"] = resp
        info["last_query"] = last_query

    tags = __svn_resp_to_tags(resp)
    tags = clean_tags(tags, info)
    return tags


S
Shinwell Hu 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
if __name__ == "__main__":
    pass
"""
def compare_tags (a, b)
	arr_a = a.split(".")
	arr_b = b.split(".")
	len = [arr_a.length, arr_b.length].min
	idx = 0
	while idx < len do
		res1 = arr_a[idx].to_i <=> arr_b[idx].to_i
		return res1 if res1 != 0
		res2 = arr_a[idx].length <=> arr_b[idx].length
		return -res2 if res2 != 0
		res3 = arr_a[idx][-1].to_i <=> arr_b[idx][-1].to_i
		return res3 if res3 != 0
		idx = idx + 1
	end
	return arr_a.length <=> arr_b.length
end

def sort_tags (tags)
	tags.sort! { |a, b|
		compare_tags(a,b)
	}
	return tags
end

"""