2 # Copyright (C) 2018 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 import flask, json, os, git, tempfile, shutil, gitdb
19 from git import GitCommandError
20 from git_archive_all import GitArchiver
21 from flask_sqlalchemy import SQLAlchemy
22 from urllib.error import HTTPError
24 from urllib.parse import urlparse, quote_plus, urlsplit
25 from zipfile import ZipFile
28 from app.models import *
29 from app.tasks import celery, TaskError
30 from app.utils import randomString
31 from .minetestcheck import build_tree, MinetestCheckError, ContentType
32 from .minetestcheck.config import parse_conf
35 def __init__(self, url):
42 m = re.search("^\/([^\/]+)\/([^\/]+)\/?$", url.path)
47 repo = m.group(2).replace(".git", "")
48 self.baseUrl = "https://raw.githubusercontent.com/{}/{}/master" \
54 return self.baseUrl is not None
57 return "https://github.com/{}/{}".format(self.user, self.repo)
59 def getScreenshotURL(self):
60 return self.baseUrl + "/screenshot.png"
62 def getModConfURL(self):
63 return self.baseUrl + "/mod.conf"
65 def getCommitsURL(self, branch):
66 return "https://api.github.com/repos/{}/{}/commits?sha={}" \
67 .format(self.user, self.repo, urllib.parse.quote_plus(branch))
69 def getCommitDownload(self, commit):
70 return "https://github.com/{}/{}/archive/{}.zip" \
71 .format(self.user, self.repo, commit)
73 krock_list_cache = None
74 krock_list_cache_by_name = None
76 global krock_list_cache
77 global krock_list_cache_by_name
79 if krock_list_cache is None:
80 contents = urllib.request.urlopen("https://krock-works.uk.to/minetest/modList.php").read().decode("utf-8")
81 list = json.loads(contents)
84 if not ("title" in x and "author" in x and \
85 "topicId" in x and "link" in x and x["link"] != ""):
89 m = re.search("\[([A-Za-z0-9_]+)\]", x["title"])
93 x["name"] = m.group(1)
99 "author": x["author"],
101 "topicId": x["topicId"],
105 krock_list_cache = [g(x) for x in list if h(x)]
106 krock_list_cache_by_name = {}
107 for x in krock_list_cache:
108 if not x["name"] in krock_list_cache_by_name:
109 krock_list_cache_by_name[x["name"]] = []
111 krock_list_cache_by_name[x["name"]].append(x)
113 return krock_list_cache, krock_list_cache_by_name
115 def findModInfo(author, name, link):
116 list, lookup = getKrockList()
118 if name is not None and name in lookup:
119 if len(lookup[name]) == 1:
120 return lookup[name][0]
122 for x in lookup[name]:
123 if x["author"] == author:
126 if link is not None and len(link) > 15:
128 if link in x["link"]:
133 def generateGitURL(urlstr):
134 scheme, netloc, path, query, frag = urlsplit(urlstr)
136 return "http://:@" + netloc + path + query
140 return os.path.join(tempfile.gettempdir(), randomString(10))
143 # Clones a repo from an unvalidated URL.
144 # Returns a tuple of path and repo on sucess.
145 # Throws `TaskError` on failure.
146 # Caller is responsible for deleting returned directory.
147 def cloneRepo(urlstr, ref=None, recursive=False):
148 gitDir = getTempDir()
152 gitUrl = generateGitURL(urlstr)
153 print("Cloning from " + gitUrl)
156 repo = git.Repo.clone_from(gitUrl, gitDir, \
157 progress=None, env=None, depth=1, recursive=recursive, kill_after_timeout=15)
159 repo = git.Repo.init(gitDir)
160 origin = repo.create_remote("origin", url=gitUrl)
161 assert origin.exists()
165 for submodule in repo.submodules:
166 submodule.update(init=True)
170 except GitCommandError as e:
171 # This is needed to stop the backtrace being weird
174 except gitdb.exc.BadName as e:
175 err = "Unable to find the reference " + (ref or "?") + "\n" + e.stderr
177 raise TaskError(err.replace("stderr: ", "") \
178 .replace("Cloning into '" + gitDir + "'...", "") \
182 def getMeta(urlstr, author):
183 gitDir, _ = cloneRepo(urlstr, recursive=True)
186 tree = build_tree(gitDir, author=author, repo=urlstr)
187 except MinetestCheckError as err:
188 raise TaskError(str(err))
190 shutil.rmtree(gitDir)
193 result["name"] = tree.name
194 result["provides"] = tree.fold("name")
195 result["type"] = tree.type.name
197 for key in ["depends", "optional_depends"]:
198 result[key] = tree.fold("meta", key)
200 for key in ["title", "repo", "issueTracker", "forumId", "description", "short_description"]:
201 result[key] = tree.get(key)
203 for mod in result["provides"]:
204 result["depends"].discard(mod)
205 result["optional_depends"].discard(mod)
207 for key, value in result.items():
208 if isinstance(value, set):
209 result[key] = list(value)
214 def makeVCSReleaseFromGithub(id, branch, release, url):
215 urlmaker = GithubURLMaker(url)
216 if not urlmaker.isValid():
217 raise TaskError("Invalid github repo URL")
219 commitsURL = urlmaker.getCommitsURL(branch)
221 contents = urllib.request.urlopen(commitsURL).read().decode("utf-8")
222 commits = json.loads(contents)
224 raise TaskError("Unable to get commits for Github repository. Either the repository or reference doesn't exist.")
226 if len(commits) == 0 or not "sha" in commits[0]:
227 raise TaskError("No commits found")
229 release.url = urlmaker.getCommitDownload(commits[0]["sha"])
230 release.task_id = None
231 release.commit_hash = commits[0]["sha"]
232 release.approve(release.package.author)
238 @celery.task(bind=True)
239 def checkZipRelease(self, id, path):
240 release = PackageRelease.query.get(id)
242 raise TaskError("No such release!")
243 elif release.package is None:
244 raise TaskError("No package attached to release")
248 with ZipFile(path, 'r') as zip_ref:
249 zip_ref.extractall(temp)
252 tree = build_tree(temp, expected_type=ContentType[release.package.type.name], \
253 author=release.package.author.username, name=release.package.name)
254 except MinetestCheckError as err:
255 if "Fails validation" not in release.title:
256 release.title += " (Fails validation)"
258 release.task_id = self.request.id
259 release.approved = False
262 raise TaskError(str(err))
264 release.task_id = None
265 release.approve(release.package.author)
273 def makeVCSRelease(id, branch):
274 release = PackageRelease.query.get(id)
276 raise TaskError("No such release!")
277 elif release.package is None:
278 raise TaskError("No package attached to release")
280 # url = urlparse(release.package.repo)
281 # if url.netloc == "github.com":
282 # return makeVCSReleaseFromGithub(id, branch, release, url)
284 gitDir, repo = cloneRepo(release.package.repo, ref=branch, recursive=True)
287 tree = build_tree(gitDir, expected_type=ContentType[release.package.type.name], \
288 author=release.package.author.username, name=release.package.name)
289 except MinetestCheckError as err:
290 raise TaskError(str(err))
293 filename = randomString(10) + ".zip"
294 destPath = os.path.join(app.config["UPLOAD_DIR"], filename)
296 assert(not os.path.isfile(destPath))
297 archiver = GitArchiver(force_sub=True, main_repo_abspath=gitDir)
298 archiver.create(destPath)
299 assert(os.path.isfile(destPath))
301 release.url = "/uploads/" + filename
302 release.task_id = None
303 release.commit_hash = repo.head.object.hexsha
304 release.approve(release.package.author)
310 shutil.rmtree(gitDir)
313 def importRepoScreenshot(id):
314 package = Package.query.get(id)
315 if package is None or package.soft_deleted:
316 raise Exception("Unexpected none package")
320 gitDir, _ = cloneRepo(package.repo)
321 except TaskError as e:
322 # ignore download errors
326 # Find and import screenshot
328 for ext in ["png", "jpg", "jpeg"]:
329 sourcePath = gitDir + "/screenshot." + ext
330 if os.path.isfile(sourcePath):
331 filename = randomString(10) + "." + ext
332 destPath = os.path.join(app.config["UPLOAD_DIR"], filename)
333 shutil.copyfile(sourcePath, destPath)
335 ss = PackageScreenshot()
338 ss.title = "screenshot.png"
339 ss.url = "/uploads/" + filename
343 return "/uploads/" + filename
345 shutil.rmtree(gitDir)
347 print("screenshot.png does not exist")
352 def getDepends(package):
353 url = urlparse(package.repo)
355 if url.netloc == "github.com":
356 urlmaker = GithubURLMaker(url)
361 if not urlmaker.isValid():
365 # Try getting depends on mod.conf
368 contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
369 conf = parse_conf(contents)
370 for key in ["depends", "optional_depends"]:
372 result[key] = conf[key]
377 print("mod.conf does not exist")
379 if "depends" in result or "optional_depends" in result:
387 pattern = re.compile("^([a-z0-9_]+)\??$")
389 contents = urllib.request.urlopen(urlmaker.getDependsURL()).read().decode("utf-8")
392 for line in contents.split("\n"):
394 if pattern.match(line):
395 if line[len(line) - 1] == "?":
396 soft.append( line[:-1])
400 result["depends"] = ",".join(hard)
401 result["optional_depends"] = ",".join(soft)
403 print("depends.txt does not exist")
408 def importDependencies(package, mpackage_cache):
409 if Dependency.query.filter_by(depender=package).count() != 0:
412 result = getDepends(package)
414 if "depends" in result:
415 deps = Dependency.SpecToList(package, result["depends"], mpackage_cache)
416 print("{} hard: {}".format(len(deps), result["depends"]))
421 if "optional_depends" in result:
422 deps = Dependency.SpecToList(package, result["optional_depends"], mpackage_cache)
423 print("{} soft: {}".format(len(deps), result["optional_depends"]))
429 def importAllDependencies():
430 Dependency.query.delete()
432 packages = Package.query.filter_by(type=PackageType.MOD).all()
433 for i, p in enumerate(packages):
434 print("============= {} ({}/{}) =============".format(p.name, i, len(packages)))
435 importDependencies(p, mpackage_cache)