-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
95 lines (63 loc) · 2.18 KB
/
update.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
94
95
"""
Kristoffer Langeland Knudsen
"""
import os
import sys
import time
import urllib
import urllib2
import zipfile
import StringIO
import os.path
from distutils.version import StrictVersion
import cache
def update(configtree):
cache.resetUpdateTimer('core', time.time() + float(configtree.findtext('.//updates/core/timer')))
# Download the version file, check if we need an update.
if checkVersion(configtree.findtext(".//updates/core/version")):
print("")
print(" Update available.")
fetchUpdate(configtree)
else:
print("")
print(" No update available")
## --------------------------------------------------------------- ##
def checkVersion(versionURL):
localVersion = open('version')
remoteVersion = urllib.urlopen(versionURL)
res = StrictVersion(remoteVersion.read()) > StrictVersion(localVersion.read())
localVersion.close()
remoteVersion.close()
return res
## --------------------------------------------------------------- ##
def fetchUpdate(configtree):
print("")
print("\tRequesting update.")
response = urllib2.urlopen(configtree.findtext(".//updates/core/url"))
print("\tResponse: " + str(response.getcode()))
archive = StringIO.StringIO(response.read())
zip = zipfile.ZipFile(archive)
zip.extractall()
print("\tReplacing files...")
print("")
for name in zip.namelist():
(path, file) = os.path.split(name)
if not file: continue
if file.endswith('.xml') and not file == 'config.xml':
# We need to be somewhat special about config files.
if os.path.exists(file):
# Does the config already exist?
# Just delete the file in the zip archive.
os.remove(name)
continue
# Delete the old file.
if os.path.exists(file): os.remove(file)
# Move the new file into place.
os.rename(name, file)
# Clean up, and pretend nothing ever happened.
os.rmdir(path)
print("")
print("\tUpdate complete.")
print("")
sys.exit()