aboutsummaryrefslogtreecommitdiff
path: root/app/tasks/importtasks.py
blob: 7ccd36c560e5ab5f171db0e267e3f04b32f1b1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Content DB
# Copyright (C) 2018  rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


import flask, json, os
from flask.ext.sqlalchemy import SQLAlchemy
from urllib.error import HTTPError
import urllib.request
from urllib.parse import urlparse, quote_plus
from app import app
from app.models import *
from app.tasks import celery, TaskError
from app.utils import randomString

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).replace(".git", "")
		self.baseUrl = "https://raw.githubusercontent.com/{}/{}/master" \
				.format(user, repo)
		self.user = user
		self.repo = repo

	def isValid(self):
		return self.baseUrl is not None

	def getRepoURL(self):
		return "https://github.com/{}/{}".format(self.user, self.repo)

	def getIssueTrackerURL(self):
		return "https://github.com/{}/{}/issues/".format(self.user, self.repo)

	def getModConfURL(self):
		return self.baseUrl + "/mod.conf"

	def getDescURL(self):
		return self.baseUrl + "/description.txt"

	def getDependsURL(self):
		return self.baseUrl + "/depends.txt"

	def getScreenshotURL(self):
		return self.baseUrl + "/screenshot.png"

	def getCommitsURL(self, branch):
		return "https://api.github.com/repos/{}/{}/commits?sha={}" \
				.format(self.user, self.repo, urllib.parse.quote_plus(branch))

	def getCommitDownload(self, commit):
		return "https://github.com/{}/{}/archive/{}.zip" \
				.format(self.user, self.repo, commit)


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):
	list, lookup = getKrockList()

	if name is not None and name in lookup:
		if len(lookup[name]) == 1:
			return lookup[name][0]

		for x in lookup[name]:
			if x["author"] == author:
				return x

	if link is not None and len(link) > 15:
		for x in list:
			if link in x["link"]:
				return x

	return None


def parseConf(string):
	retval = {}
	for line in string.split("\n"):
		idx = line.find("=")
		if idx > 0:
			key   = line[:idx].strip()
			value = line[idx+1:].strip()
			retval[key] = value

	return retval


@celery.task()
def getMeta(urlstr, author):
	url = urlparse(urlstr)

	urlmaker = None
	if url.netloc == "github.com":
		urlmaker = GithubURLMaker(url)
	else:
		raise TaskError("Unsupported repo")

	if not urlmaker.isValid():
		raise TaskError("Error! Url maker not valid")

	result = {}

	result["repo"] = urlmaker.getRepoURL()
	result["issueTracker"] = urlmaker.getIssueTrackerURL()

	try:
		contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
		conf = parseConf(contents)
		for key in ["name", "description", "title", "depends", "optional_depends"]:
			try:
				result[key] = conf[key]
			except KeyError:
				pass
	except HTTPError:
		print("mod.conf does not exist")

	if "name" in result:
		result["title"] = result["name"].replace("_", " ").title()

	if not "description" in result:
		try:
			contents = urllib.request.urlopen(urlmaker.getDescURL()).read().decode("utf-8")
			result["description"] = contents.strip()
		except HTTPError:
			print("description.txt does not exist!")

	import re
	pattern = re.compile("^([a-z0-9_]+)\??$")
	if not "depends" in result and not "optional_depends" in result:
		try:
			contents = urllib.request.urlopen(urlmaker.getDependsURL()).read().decode("utf-8")
			soft = []
			hard = []
			for line in contents.split("\n"):
				line = line.strip()
				if pattern.match(line):
					if line[len(line) - 1] == "?":
						soft.append( line[:-1])
					else:
						hard.append(line)

			result["depends"] = ",".join(hard)
			result["optional_depends"] = ",".join(soft)


		except HTTPError:
			print("depends.txt does not exist!")

	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]


	info = findModInfo(author, result.get("name"), result["repo"])
	if info is not None:
		result["forumId"] = info.get("topicId")

	return result


@celery.task()
def makeVCSRelease(id, branch):
	release = PackageRelease.query.get(id)

	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")

	commitsURL = urlmaker.getCommitsURL(branch)
	contents = urllib.request.urlopen(commitsURL).read().decode("utf-8")
	commits = json.loads(contents)

	if len(commits) == 0 or not "sha" in commits[0]:
		raise TaskError("No commits found")

	release.url = urlmaker.getCommitDownload(commits[0]["sha"])
	print(release.url)
	release.task_id = None
	db.session.commit()

	return release.url


@celery.task()
def importRepoScreenshot(id):
	package = Package.query.get(id)
	if package is None or package.soft_deleted:
		raise Exception("Unexpected none package")

	# Get URL Maker
	url = urlparse(package.repo)
	urlmaker = None
	if url.netloc == "github.com":
		urlmaker = GithubURLMaker(url)
	else:
		raise TaskError("Unsupported repo")

	if not urlmaker.isValid():
		raise TaskError("Error! Url maker not valid")

	try:
		filename = randomString(10) + ".png"
		imagePath = os.path.join("app/public/uploads", filename)
		print(imagePath)
		urllib.request.urlretrieve(urlmaker.getScreenshotURL(), imagePath)

		ss = PackageScreenshot()
		ss.approved = True
		ss.package = package
		ss.title   = "screenshot.png"
		ss.url     = "/uploads/" + filename
		db.session.add(ss)
		db.session.commit()

		return "/uploads/" + filename
	except HTTPError:
		print("screenshot.png does not exist")

	return None



def getDepends(package):
	url = urlparse(package.repo)
	urlmaker = None
	if url.netloc == "github.com":
		urlmaker = GithubURLMaker(url)
	else:
		raise TaskError("Unsupported repo")

	result = {}
	if urlmaker.isValid():
		#
		# Try getting depends on mod.conf
		#
		try:
			contents = urllib.request.urlopen(urlmaker.getModConfURL()).read().decode("utf-8")
			conf = parseConf(contents)
			for key in ["depends", "optional_depends"]:
				try:
					result[key] = conf[key]
				except KeyError:
					pass

		except HTTPError:
			print("mod.conf does not exist")

		if "depends" in result or "optional_depends" in result:
			return result


		#
		# Try depends.txt
		#
		import re
		pattern = re.compile("^([a-z0-9_]+)\??$")
		try:
			contents = urllib.request.urlopen(urlmaker.getDependsURL()).read().decode("utf-8")
			soft = []
			hard = []
			for line in contents.split("\n"):
				line = line.strip()
				if pattern.match(line):
					if line[len(line) - 1] == "?":
						soft.append( line[:-1])
					else:
						hard.append(line)

			result["depends"] = ",".join(hard)
			result["optional_depends"] = ",".join(soft)
		except HTTPError:
			print("depends.txt does not exist")

		return result

	else:
		print(TaskError("non-github depends detector not implemented yet!"))
		return {}


def importDependencies(package, mpackage_cache):
	if Dependency.query.filter_by(depender=package).count() != 0:
		return

	result = getDepends(package)

	if "depends" in result:
		deps = Dependency.SpecToList(package, result["depends"], mpackage_cache)
		print("{} hard: {}".format(len(deps), result["depends"]))
		for dep in deps:
			dep.optional = False
			db.session.add(dep)

	if "optional_depends" in result:
		deps = Dependency.SpecToList(package, result["optional_depends"], mpackage_cache)
		print("{} soft: {}".format(len(deps), result["optional_depends"]))
		for dep in deps:
			dep.optional = True
			db.session.add(dep)

@celery.task()
def importAllDependencies():
	Dependency.query.delete()
	mpackage_cache = {}
	packages = Package.query.filter_by(type=PackageType.MOD).all()
	for i, p in enumerate(packages):
		print("============= {} ({}/{}) =============".format(p.name, i, len(packages)))
		importDependencies(p, mpackage_cache)

	db.session.commit()