2 from flask.ext.sqlalchemy import SQLAlchemy
4 from urllib.parse import urlparse, quote_plus
6 from app.models import *
7 from app.tasks import celery
9 class TaskError(Exception):
10 def __init__(self, value):
13 return repr(self.value)
17 def __init__(self, url):
20 m = re.search("^\/([^\/]+)\/([^\/]+)\/?$", url.path)
26 self.baseUrl = "https://raw.githubusercontent.com/{}/{}/master" \
27 .format(user, repo.replace(".git", ""))
32 return self.baseUrl is not None
35 return "https://github.com/{}/{}".format(self.user, self.repo)
37 def getIssueTrackerURL(self):
38 return "https://github.com/{}/{}/issues/".format(self.user, self.repo)
40 def getModConfURL(self):
41 return self.baseUrl + "/mod.conf"
44 return self.baseUrl + "/description.txt"
46 def getScreenshotURL(self):
47 return self.baseUrl + "/screenshot.png"
49 def getCommitsURL(self, branch):
50 return "https://api.github.com/repos/{}/{}/commits?sha={}" \
51 .format(self.user, self.repo, urllib.parse.quote_plus(branch))
53 def getCommitDownload(self, commit):
54 return "https://github.com/{}/{}/archive/{}.zip" \
55 .format(self.user, self.repo, commit)
58 krock_list_cache = None
59 krock_list_cache_by_name = None
61 global krock_list_cache
62 global krock_list_cache_by_name
64 if krock_list_cache is None:
65 contents = urllib.request.urlopen("http://krock-works.16mb.com/MTstuff/modList.php").read().decode("utf-8")
66 list = json.loads(contents)
69 if not ("title" in x and "author" in x and \
70 "topicId" in x and "link" in x and x["link"] != ""):
74 m = re.search("\[([A-Za-z0-9_]+)\]", x["title"])
78 x["name"] = m.group(1)
84 "author": x["author"],
86 "topicId": x["topicId"],
90 krock_list_cache = [g(x) for x in list if h(x)]
91 krock_list_cache_by_name = {}
92 for x in krock_list_cache:
93 if not x["name"] in krock_list_cache_by_name:
94 krock_list_cache_by_name[x["name"]] = []
96 krock_list_cache_by_name[x["name"]].append(x)
98 return krock_list_cache, krock_list_cache_by_name
100 def findModInfo(author, name, link):
101 _, lookup = getKrockList()
104 if len(lookup[name]) == 1:
105 return lookup[name][0]
107 for x in lookup[name]:
108 if x["author"] == author:
114 def parseConf(string):
116 for line in string.split("\n"):
119 key = line[:idx-1].strip()
120 value = line[idx+1:].strip()
127 def getMeta(urlstr, author):
128 url = urlparse(urlstr)
131 if url.netloc == "github.com":
132 urlmaker = GithubURLMaker(url)
134 raise TaskError("Unsupported repo")
136 if not urlmaker.isValid():
137 raise TaskError("Error! Url maker not valid")
141 result["repo"] = urlmaker.getRepoURL()
142 result["issueTracker"] = urlmaker.getIssueTrackerURL()
145 contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
146 conf = parseConf(contents)
147 for key in ["name", "description", "title"]:
149 result[key] = conf[key]
153 print("mod.conf does not exist")
156 result["title"] = result["name"].replace("_", " ").title()
158 if not "description" in result:
160 contents = urllib.request.urlopen(urlmaker.getDescURL()).read().decode("utf-8")
161 result["description"] = contents.strip()
163 print("description.txt does not exist!")
165 if "description" in result:
166 desc = result["description"]
167 idx = desc.find(".") + 1
168 cutIdx = min(len(desc), 200 if idx < 5 else idx)
169 result["short_description"] = desc[:cutIdx]
171 info = findModInfo(author, result["name"], result["repo"])
173 result["forumId"] = info["topicId"]
178 def makeVCSRelease(id, branch):
179 release = PackageRelease.query.get(id)
181 raise TaskError("No such release!")
183 if release.package is None:
184 raise TaskError("No package attached to release")
186 url = urlparse(release.package.repo)
189 if url.netloc == "github.com":
190 urlmaker = GithubURLMaker(url)
192 raise TaskError("Unsupported repo")
194 if not urlmaker.isValid():
195 raise TaskError("Invalid github repo URL")
197 contents = urllib.request.urlopen(urlmaker.getCommitsURL(branch)).read().decode("utf-8")
198 commits = json.loads(contents)
200 if len(commits) == 0:
201 raise TaskError("No commits found")
203 release.url = urlmaker.getCommitDownload(commits[0]["sha"])
204 release.task_id = None