2018-05-11 16:04:17 +02:00
|
|
|
import flask, json
|
2018-05-11 13:57:16 +02:00
|
|
|
from flask.ext.sqlalchemy import SQLAlchemy
|
|
|
|
import urllib.request
|
2018-05-11 16:04:17 +02:00
|
|
|
from urllib.parse import urlparse, quote_plus
|
2018-05-11 13:57:16 +02:00
|
|
|
from app import app
|
|
|
|
from app.models import *
|
2018-05-14 02:20:02 +02:00
|
|
|
from app.tasks import celery, TaskError
|
2018-05-11 16:04:17 +02:00
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
class GithubURLMaker:
|
|
|
|
def __init__(self, url):
|
|
|
|
# Rewrite path
|
|
|
|
import re
|
|
|
|
m = re.search("^\/([^\/]+)\/([^\/]+)\/?$", url.path)
|
|
|
|
if m is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
user = m.group(1)
|
|
|
|
repo = m.group(2)
|
2018-05-13 01:38:54 +02:00
|
|
|
self.baseUrl = "https://raw.githubusercontent.com/{}/{}/master" \
|
|
|
|
.format(user, repo.replace(".git", ""))
|
2018-05-11 16:04:17 +02:00
|
|
|
self.user = user
|
|
|
|
self.repo = repo
|
2018-05-11 13:57:16 +02:00
|
|
|
|
|
|
|
def isValid(self):
|
|
|
|
return self.baseUrl is not None
|
|
|
|
|
2018-05-12 18:28:04 +02:00
|
|
|
def getRepoURL(self):
|
2018-05-13 01:38:54 +02:00
|
|
|
return "https://github.com/{}/{}".format(self.user, self.repo)
|
2018-05-12 18:28:04 +02:00
|
|
|
|
|
|
|
def getIssueTrackerURL(self):
|
2018-05-13 01:38:54 +02:00
|
|
|
return "https://github.com/{}/{}/issues/".format(self.user, self.repo)
|
2018-05-12 18:28:04 +02:00
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
def getModConfURL(self):
|
|
|
|
return self.baseUrl + "/mod.conf"
|
|
|
|
|
2018-05-11 14:25:42 +02:00
|
|
|
def getDescURL(self):
|
|
|
|
return self.baseUrl + "/description.txt"
|
|
|
|
|
|
|
|
def getScreenshotURL(self):
|
2018-05-13 22:23:09 +02:00
|
|
|
return self.baseUrl + "/placeholder.png"
|
2018-05-11 14:25:42 +02:00
|
|
|
|
2018-05-11 16:04:17 +02:00
|
|
|
def getCommitsURL(self, branch):
|
2018-05-13 01:38:54 +02:00
|
|
|
return "https://api.github.com/repos/{}/{}/commits?sha={}" \
|
|
|
|
.format(self.user, self.repo, urllib.parse.quote_plus(branch))
|
2018-05-11 16:04:17 +02:00
|
|
|
|
|
|
|
def getCommitDownload(self, commit):
|
2018-05-13 01:38:54 +02:00
|
|
|
return "https://github.com/{}/{}/archive/{}.zip" \
|
|
|
|
.format(self.user, self.repo, commit)
|
2018-05-11 16:04:17 +02:00
|
|
|
|
|
|
|
|
2018-05-12 19:50:09 +02:00
|
|
|
krock_list_cache = None
|
|
|
|
krock_list_cache_by_name = None
|
|
|
|
def getKrockList():
|
|
|
|
global krock_list_cache
|
|
|
|
global krock_list_cache_by_name
|
|
|
|
|
|
|
|
if krock_list_cache is None:
|
|
|
|
contents = urllib.request.urlopen("http://krock-works.16mb.com/MTstuff/modList.php").read().decode("utf-8")
|
|
|
|
list = json.loads(contents)
|
|
|
|
|
|
|
|
def h(x):
|
|
|
|
if not ("title" in x and "author" in x and \
|
|
|
|
"topicId" in x and "link" in x and x["link"] != ""):
|
|
|
|
return False
|
|
|
|
|
|
|
|
import re
|
|
|
|
m = re.search("\[([A-Za-z0-9_]+)\]", x["title"])
|
|
|
|
if m is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
x["name"] = m.group(1)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def g(x):
|
|
|
|
return {
|
|
|
|
"title": x["title"],
|
|
|
|
"author": x["author"],
|
|
|
|
"name": x["name"],
|
|
|
|
"topicId": x["topicId"],
|
|
|
|
"link": x["link"],
|
|
|
|
}
|
|
|
|
|
|
|
|
krock_list_cache = [g(x) for x in list if h(x)]
|
|
|
|
krock_list_cache_by_name = {}
|
|
|
|
for x in krock_list_cache:
|
|
|
|
if not x["name"] in krock_list_cache_by_name:
|
|
|
|
krock_list_cache_by_name[x["name"]] = []
|
|
|
|
|
|
|
|
krock_list_cache_by_name[x["name"]].append(x)
|
|
|
|
|
|
|
|
return krock_list_cache, krock_list_cache_by_name
|
|
|
|
|
|
|
|
def findModInfo(author, name, link):
|
|
|
|
_, lookup = getKrockList()
|
|
|
|
|
|
|
|
if name in lookup:
|
|
|
|
if len(lookup[name]) == 1:
|
|
|
|
return lookup[name][0]
|
|
|
|
|
|
|
|
for x in lookup[name]:
|
|
|
|
if x["author"] == author:
|
|
|
|
return x
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
def parseConf(string):
|
|
|
|
retval = {}
|
|
|
|
for line in string.split("\n"):
|
|
|
|
idx = line.find("=")
|
|
|
|
if idx > 0:
|
|
|
|
key = line[:idx-1].strip()
|
|
|
|
value = line[idx+1:].strip()
|
|
|
|
retval[key] = value
|
|
|
|
|
|
|
|
return retval
|
|
|
|
|
2018-05-12 19:50:09 +02:00
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
@celery.task()
|
2018-05-12 19:50:09 +02:00
|
|
|
def getMeta(urlstr, author):
|
2018-05-11 13:57:16 +02:00
|
|
|
url = urlparse(urlstr)
|
|
|
|
|
|
|
|
urlmaker = None
|
|
|
|
if url.netloc == "github.com":
|
|
|
|
urlmaker = GithubURLMaker(url)
|
2018-05-11 16:04:17 +02:00
|
|
|
else:
|
|
|
|
raise TaskError("Unsupported repo")
|
2018-05-11 13:57:16 +02:00
|
|
|
|
|
|
|
if not urlmaker.isValid():
|
2018-05-11 16:04:17 +02:00
|
|
|
raise TaskError("Error! Url maker not valid")
|
2018-05-11 13:57:16 +02:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
2018-05-12 18:28:04 +02:00
|
|
|
result["repo"] = urlmaker.getRepoURL()
|
|
|
|
result["issueTracker"] = urlmaker.getIssueTrackerURL()
|
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
try:
|
|
|
|
contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
|
|
|
|
conf = parseConf(contents)
|
2018-05-11 14:25:42 +02:00
|
|
|
for key in ["name", "description", "title"]:
|
2018-05-11 13:57:16 +02:00
|
|
|
try:
|
|
|
|
result[key] = conf[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
except OSError:
|
|
|
|
print("mod.conf does not exist")
|
|
|
|
|
2018-05-12 18:28:04 +02:00
|
|
|
if "name" in result:
|
|
|
|
result["title"] = result["name"].replace("_", " ").title()
|
|
|
|
|
2018-05-11 14:25:42 +02:00
|
|
|
if not "description" in result:
|
|
|
|
try:
|
|
|
|
contents = urllib.request.urlopen(urlmaker.getDescURL()).read().decode("utf-8")
|
|
|
|
result["description"] = contents.strip()
|
|
|
|
except OSError:
|
|
|
|
print("description.txt does not exist!")
|
|
|
|
|
2018-05-12 18:28:04 +02:00
|
|
|
if "description" in result:
|
|
|
|
desc = result["description"]
|
|
|
|
idx = desc.find(".") + 1
|
|
|
|
cutIdx = min(len(desc), 200 if idx < 5 else idx)
|
|
|
|
result["short_description"] = desc[:cutIdx]
|
|
|
|
|
2018-05-12 19:50:09 +02:00
|
|
|
info = findModInfo(author, result["name"], result["repo"])
|
|
|
|
if info is not None:
|
|
|
|
result["forumId"] = info["topicId"]
|
|
|
|
|
2018-05-11 13:57:16 +02:00
|
|
|
return result
|
2018-05-11 16:04:17 +02:00
|
|
|
|
|
|
|
@celery.task()
|
|
|
|
def makeVCSRelease(id, branch):
|
|
|
|
release = PackageRelease.query.get(id)
|
2018-05-14 02:20:02 +02:00
|
|
|
|
2018-05-11 16:04:17 +02:00
|
|
|
if release is None:
|
|
|
|
raise TaskError("No such release!")
|
|
|
|
|
|
|
|
if release.package is None:
|
|
|
|
raise TaskError("No package attached to release")
|
|
|
|
|
|
|
|
url = urlparse(release.package.repo)
|
|
|
|
|
|
|
|
urlmaker = None
|
|
|
|
if url.netloc == "github.com":
|
|
|
|
urlmaker = GithubURLMaker(url)
|
|
|
|
else:
|
|
|
|
raise TaskError("Unsupported repo")
|
|
|
|
|
|
|
|
if not urlmaker.isValid():
|
|
|
|
raise TaskError("Invalid github repo URL")
|
|
|
|
|
|
|
|
contents = urllib.request.urlopen(urlmaker.getCommitsURL(branch)).read().decode("utf-8")
|
|
|
|
commits = json.loads(contents)
|
|
|
|
|
|
|
|
if len(commits) == 0:
|
|
|
|
raise TaskError("No commits found")
|
|
|
|
|
|
|
|
release.url = urlmaker.getCommitDownload(commits[0]["sha"])
|
|
|
|
release.task_id = None
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return release.url
|