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/" + user + "/" + repo.replace(".git", "") + "/master"
31 return self.baseUrl is not None
34 return "https://github.com/" + self.user + "/" + self.repo + ".git"
36 def getIssueTrackerURL(self):
37 return "https://github.com/" + self.user + "/" + self.repo + "/issues/"
39 def getModConfURL(self):
40 return self.baseUrl + "/mod.conf"
43 return self.baseUrl + "/description.txt"
45 def getScreenshotURL(self):
46 return self.baseUrl + "/screenshot.png"
48 def getCommitsURL(self, branch):
49 return "https://api.github.com/repos/" + self.user + "/" + self.repo + "/commits?sha" + urllib.parse.quote_plus(branch)
51 def getCommitDownload(self, commit):
52 return "https://github.com/" + self.user + "/" + self.repo + "/archive/" + commit + ".zip"
55 krock_list_cache = None
56 krock_list_cache_by_name = None
58 global krock_list_cache
59 global krock_list_cache_by_name
61 if krock_list_cache is None:
62 contents = urllib.request.urlopen("http://krock-works.16mb.com/MTstuff/modList.php").read().decode("utf-8")
63 list = json.loads(contents)
66 if not ("title" in x and "author" in x and \
67 "topicId" in x and "link" in x and x["link"] != ""):
71 m = re.search("\[([A-Za-z0-9_]+)\]", x["title"])
75 x["name"] = m.group(1)
81 "author": x["author"],
83 "topicId": x["topicId"],
87 krock_list_cache = [g(x) for x in list if h(x)]
88 krock_list_cache_by_name = {}
89 for x in krock_list_cache:
90 if not x["name"] in krock_list_cache_by_name:
91 krock_list_cache_by_name[x["name"]] = []
93 krock_list_cache_by_name[x["name"]].append(x)
95 return krock_list_cache, krock_list_cache_by_name
97 def findModInfo(author, name, link):
98 _, lookup = getKrockList()
101 if len(lookup[name]) == 1:
102 return lookup[name][0]
104 for x in lookup[name]:
105 if x["author"] == author:
111 def parseConf(string):
113 for line in string.split("\n"):
116 key = line[:idx-1].strip()
117 value = line[idx+1:].strip()
124 def getMeta(urlstr, author):
125 url = urlparse(urlstr)
128 if url.netloc == "github.com":
129 urlmaker = GithubURLMaker(url)
131 raise TaskError("Unsupported repo")
133 if not urlmaker.isValid():
134 raise TaskError("Error! Url maker not valid")
138 result["repo"] = urlmaker.getRepoURL()
139 result["issueTracker"] = urlmaker.getIssueTrackerURL()
142 contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
143 conf = parseConf(contents)
144 for key in ["name", "description", "title"]:
146 result[key] = conf[key]
150 print("mod.conf does not exist")
153 result["title"] = result["name"].replace("_", " ").title()
155 if not "description" in result:
157 contents = urllib.request.urlopen(urlmaker.getDescURL()).read().decode("utf-8")
158 result["description"] = contents.strip()
160 print("description.txt does not exist!")
162 if "description" in result:
163 desc = result["description"]
164 idx = desc.find(".") + 1
165 cutIdx = min(len(desc), 200 if idx < 5 else idx)
166 result["short_description"] = desc[:cutIdx]
168 info = findModInfo(author, result["name"], result["repo"])
170 result["forumId"] = info["topicId"]
175 def makeVCSRelease(id, branch):
176 release = PackageRelease.query.get(id)
178 raise TaskError("No such release!")
180 if release.package is None:
181 raise TaskError("No package attached to release")
183 url = urlparse(release.package.repo)
186 if url.netloc == "github.com":
187 urlmaker = GithubURLMaker(url)
189 raise TaskError("Unsupported repo")
191 if not urlmaker.isValid():
192 raise TaskError("Invalid github repo URL")
194 contents = urllib.request.urlopen(urlmaker.getCommitsURL(branch)).read().decode("utf-8")
195 commits = json.loads(contents)
197 if len(commits) == 0:
198 raise TaskError("No commits found")
200 release.url = urlmaker.getCommitDownload(commits[0]["sha"])
201 release.task_id = None