-
Notifications
You must be signed in to change notification settings - Fork 0
/
NEMP_Tester.py
92 lines (73 loc) · 3.02 KB
/
NEMP_Tester.py
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
# Config options
mods = ["ChickenChunks", "EnderTech-Drayshak", "Veinminer", "WarpBook"] # A list of mods to test
caching = True # Setting this to True rewrites the fetch_page function of NEMP_Class to cache web requests
# Stuff bellow is to make all web requests cached.
# Most of it is "stolen" from the NotEnoughMods_Tools.py
# and NotEnoughMods_Polling.py files in the NotEnoughMods repo
import traceback
import codecs
import os
import json
from string import ascii_letters, digits
from commands.NEMP import NEMP_Class
def cached_fetch_page(self, url, timeout=10, decode_json=False):
self.cacheDir = os.path.join("nemcache")
try:
fname = normalize_filename(url)
filepath = os.path.join(self.cacheDir, fname)
if os.path.exists(filepath):
print "Loading from cache,",filepath
with codecs.open(filepath, encoding='utf-8', mode='r') as f:
if decode_json:
return json.loads(f.read())
else:
return f.read()
request = self.requests_session.get(url, timeout=timeout)
print "Writing to cache,",filepath
with codecs.open(filepath, encoding='utf-8', mode='w') as f:
if decode_json:
f.write(request.text)
return request.json()
else:
f.write(request.text)
return request.text
except:
traceback.print_exc()
pass
# most likely a timeout
def normalize_filename(name):
return ''.join(c for c in name if c in "-_.() %s%s" % (ascii_letters, digits))
if caching:
NEMP_Class.NotEnoughClasses.fetch_page = cached_fetch_page
NEM = NEMP_Class.NotEnoughClasses()
for mod in mods:
try:
result = getattr(NEM, NEM.mods[mod]["function"])(mod)
real_name = NEM.mods[mod].get('name', mod)
if 'mc' in result:
version = result['mc']
else:
version = NEM.mods[mod]["mc"]
print "\n" "<--------------------------->"
if not result:
print(mod + "Didn't get a reply from the parser. (got " + repr(result) + ")")
print "<--------------------------->" "\n"
break
print(mod + ": {}".format(result))
if "mc" in result:
if version != result["mc"]:
print("Expected MC version {}, got {}".format(version, result["mc"]))
else:
print("Did not receive MC version from parser.")
if "version" in result:
print("!lmod {0} {1} {2}".format(version, real_name, unicode(result["version"])))
if "dev" in result:
print("!ldev {0} {1} {2}".format(version, real_name, unicode(result["dev"])))
if "change" in result:
print(" * " + result["change"])
print "<--------------------------->" "\n"
except Exception as error:
print "\n" "<--------------------------->"
print(mod + ": " + str(error))
print(mod + " failed to be polled")
print "<--------------------------->" "\n"