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, TaskError
10 def __init__(self, url):
13 m = re.search("^\/([^\/]+)\/([^\/]+)\/?$", url.path)
19 self.baseUrl = "https://raw.githubusercontent.com/{}/{}/master" \
20 .format(user, repo.replace(".git", ""))
25 return self.baseUrl is not None
28 return "https://github.com/{}/{}".format(self.user, self.repo)
30 def getIssueTrackerURL(self):
31 return "https://github.com/{}/{}/issues/".format(self.user, self.repo)
33 def getModConfURL(self):
34 return self.baseUrl + "/mod.conf"
37 return self.baseUrl + "/description.txt"
39 def getScreenshotURL(self):
40 return self.baseUrl + "/placeholder.png"
42 def getCommitsURL(self, branch):
43 return "https://api.github.com/repos/{}/{}/commits?sha={}" \
44 .format(self.user, self.repo, urllib.parse.quote_plus(branch))
46 def getCommitDownload(self, commit):
47 return "https://github.com/{}/{}/archive/{}.zip" \
48 .format(self.user, self.repo, commit)
51 krock_list_cache = None
52 krock_list_cache_by_name = None
54 global krock_list_cache
55 global krock_list_cache_by_name
57 if krock_list_cache is None:
58 contents = urllib.request.urlopen("http://krock-works.16mb.com/MTstuff/modList.php").read().decode("utf-8")
59 list = json.loads(contents)
62 if not ("title" in x and "author" in x and \
63 "topicId" in x and "link" in x and x["link"] != ""):
67 m = re.search("\[([A-Za-z0-9_]+)\]", x["title"])
71 x["name"] = m.group(1)
77 "author": x["author"],
79 "topicId": x["topicId"],
83 krock_list_cache = [g(x) for x in list if h(x)]
84 krock_list_cache_by_name = {}
85 for x in krock_list_cache:
86 if not x["name"] in krock_list_cache_by_name:
87 krock_list_cache_by_name[x["name"]] = []
89 krock_list_cache_by_name[x["name"]].append(x)
91 return krock_list_cache, krock_list_cache_by_name
93 def findModInfo(author, name, link):
94 list, lookup = getKrockList()
96 if name is not None and name in lookup:
97 if len(lookup[name]) == 1:
98 return lookup[name][0]
100 for x in lookup[name]:
101 if x["author"] == author:
104 if link is not None and len(link) > 15:
106 if link in x["link"]:
112 def parseConf(string):
114 for line in string.split("\n"):
117 key = line[:idx].strip()
118 value = line[idx+1:].strip()
125 def getMeta(urlstr, author):
126 url = urlparse(urlstr)
129 if url.netloc == "github.com":
130 urlmaker = GithubURLMaker(url)
132 raise TaskError("Unsupported repo")
134 if not urlmaker.isValid():
135 raise TaskError("Error! Url maker not valid")
139 result["repo"] = urlmaker.getRepoURL()
140 result["issueTracker"] = urlmaker.getIssueTrackerURL()
143 contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
144 conf = parseConf(contents)
145 for key in ["name", "description", "title"]:
147 result[key] = conf[key]
151 print("mod.conf does not exist")
154 result["title"] = result["name"].replace("_", " ").title()
156 if not "description" in result:
158 contents = urllib.request.urlopen(urlmaker.getDescURL()).read().decode("utf-8")
159 result["description"] = contents.strip()
161 print("description.txt does not exist!")
163 if "description" in result:
164 desc = result["description"]
165 idx = desc.find(".") + 1
166 cutIdx = min(len(desc), 200 if idx < 5 else idx)
167 result["short_description"] = desc[:cutIdx]
169 info = findModInfo(author, result.get("name"), result["repo"])
171 result["forumId"] = info.get("topicId")
176 def makeVCSRelease(id, branch):
177 release = PackageRelease.query.get(id)
180 raise TaskError("No such release!")
182 if release.package is None:
183 raise TaskError("No package attached to release")
185 url = urlparse(release.package.repo)
188 if url.netloc == "github.com":
189 urlmaker = GithubURLMaker(url)
191 raise TaskError("Unsupported repo")
193 if not urlmaker.isValid():
194 raise TaskError("Invalid github repo URL")
196 contents = urllib.request.urlopen(urlmaker.getCommitsURL(branch)).read().decode("utf-8")
197 commits = json.loads(contents)
199 if len(commits) == 0:
200 raise TaskError("No commits found")
202 release.url = urlmaker.getCommitDownload(commits[0]["sha"])
203 release.task_id = None