-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUpdate Filters.py
93 lines (89 loc) · 4.03 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
91
92
93
# 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 = (
("View Change Logs", False),
("Include WIP versions", False),
)
def perform(level, box, options):
NUM_UPDATED = 0
NUM_TOTAL = 0
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("filters/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:
NUM_TOTAL = NUM_TOTAL + 1
filterUpdateURL = ""
try:
pyfile = 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 = newName[0]
update = __import__(name)
# I use __import__() to import a filter from a string
filterUpdateURL = update.UPDATE_URL
# Grabs the declared variable name "UPDATE_URL"
filterVersion = update.VERSION
print '%s: %s:%s' % (METHOD, name, update.VERSION)
# Grabs the declared variable name "VERSION"
if includeWIPs:
filterWIP = None
try:
filterWIPURL = str(update.WIP_URL)
# Grabs the declared variable name "WIP_URL"
filterWIP = str(update.WIP)
# Grabs the declared variable name "WIP"
except:
pass
if filterWIP == "True" or includeWIPs:
filterUpdateURL = filterWIPURL
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 str(filterVersion) != str(jsonRaw["Version"]):
# Checks to make sure the two versions don't match
if "Name" in jsonRaw:
urllib.urlretrieve(jsonRaw["Download-URL"], "filters/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"], update.VERSION, str(jsonRaw["Version"]))
else:
urllib.urlretrieve(jsonRaw["Download-URL"], "filters/updates/" + pyfile)
# 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, pyfile, update.VERSION, str(jsonRaw["Version"]))
NUM_UPDATED = NUM_UPDATED + 1
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:
continue
files = glob.glob("filters/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
raise Exception("Updated " + str(NUM_UPDATED) + " filters out of " + str(NUM_TOTAL) + " filters.")