forked from Podshot/MCEdit-Filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate Filters.py
90 lines (86 loc) · 4.04 KB
/
Update Filters.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
# Update Filters by Podshot
# WIP Filter Updating ability by jgierer12
# This updates all filters that declare an UPDATE_URL and VERSION
import glob, urllib2, urllib, json, os, webbrowser, time, shutil
displayName = "Update Filters"
METHOD = "[Update Filters]"
inputs = (
("Remove Old Filters?", True),
("View Change Logs", False),
("Include WIP versions", False),
)
def perform(level, box, options):
doRemove = options["Remove Old Filters?"]
changeLog = options["View Change Logs"]
includeWIPs = options["Include WIP versions"]
filterDir = str(os.path.dirname(os.path.abspath(__file__)))
# Gets the directory of where the "filters" is located
try:
os.mkdir(filterDir + "/updates")
except OSError:
pass
filters = glob.glob("filters/*.py")
# Search the "filters" folder for all files that have an extension of ".py"
for filt in filters:
filterUpdateURL = None
try:
pyfile = str(filt[8:])
# Removes the "filters/" from the filter path
newName = pyfile.split(".")
# __import__() does not like file extensions, so I remove the .py extension from the file name
name = str(newName[0])
py = __import__(name)
# I use __import__() to import a filter from a string
filterUpdateURL = str(py.UPDATE_URL)
# Grabs the declared variable name "UPDATE_URL"
filterVersion = str(py.VERSION)
# Grabs the declared variable name "VERSION"
if includeWIPs:
filterWIP = None
try:
filterWIPURL = str(py.WIP_URL)
# Grabs the declared variable name "WIP_URL"
filterWIP = str(py.WIP)
# Grabs the declared variable name "WIP"
except:
pass
try:
if filterWIP == "True" or includeWIPs:
filterUpdateURL = filterWIPURL
except:
pass
site = urllib2.urlopen(filterUpdateURL)
# Opens up a site connection the the filter's update page or WIP update page
response = site.read()
# Converts the page into a string
jsonRaw = json.loads(response)
# Loads the page string into JSON format
if filterVersion != jsonRaw["Version"]:
# Checks to make sure the two versions don't match
urllib.urlretrieve(jsonRaw["Download-URL"], filterDir + "/updates/" + jsonRaw["Name"])
# Downloads the new filter to a file name determined by the update site (Used if the author like to version in the file name also)
print '%s: Updated "%s" from version %s to version %s' % (METHOD, jsonRaw["Name"], py.VERSION, str(jsonRaw["Version"]))
if doRemove:
# Removes the old filter if the user wants to
os.remove(filt)
if changeLog:
# If the user wants to open the Change Log and the author has included one
if "ChangeLog" in jsonRaw:
log = str(jsonRaw["ChangeLog"])
webbrowser.open_new_tab(log)
# Opens a new tab in the default webbrowser
else:
print '%s: Filter "%s" did not have a Change Log' % (METHOD, jsonRaw["Name"])
else:
print '%s: %s\'s version matched update the site\'s version' % (METHOD, jsonRaw["Name"])
except:
pass
files = glob.glob(filterDir + "/updates/*.py")
# Finds all .py like before, but in the "updates" subfolder
for f in files:
shutil.copy(f, filterDir)
# Copies all the new filters from the "updates" folder to the parent("filters") folder
time.sleep(2)
# I want to wait just in case the disk is a little slow
shutil.rmtree(filterDir + "/updates")
# Removes the directory tree of the "updates" folder