-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageSetup.py
173 lines (145 loc) · 4.11 KB
/
PackageSetup.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
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
#!python
from __future__ import with_statement
import glob
import os.path
import shutil
import zipfile
def isFileEntry(s):
return s[-1] != '/'
def readFile(fname):
with open(fname, 'rb') as f:
return f.read()
def writeFile(fname, data):
with open(fname, 'wb') as fo:
fo.write(data)
def mkdirs(path):
try:
os.makedirs(path)
except os.error:
pass
def upgradeArchive(src, dst, pkgdir, backupdir, inhibitOverwrite):
mkdirs(pkgdir)
mkdirs(os.path.dirname(dst))
try:
newar = zipfile.ZipFile(src)
except (zipfile.BadZipfile):
print 'PackageSetup: bad zip file at %s' % src
return
newfiles = set(filter(isFileEntry, newar.namelist()))
oldar = None
oldfiles = set()
try:
oldar = zipfile.ZipFile(dst)
oldfiles = set(filter(isFileEntry, oldar.namelist()))
except (zipfile.error, IOError):
pass
# delete any orphaned files
if not inhibitOverwrite:
orphanedFiles = oldfiles - newfiles
for f in orphanedFiles:
# backup the file, if the user has modified it
try:
orig = oldar.read(f)
user = readFile(os.path.join(pkgdir, f))
if user != orig:
backupFile = os.path.join(backupdir, f)
mkdirs(os.path.dirname(backupFile))
writeFile(backupFile, user)
except (os.error, IOError):
pass
try:
os.remove(os.path.join(pkgdir, f))
except (os.error, IOError):
pass
# extract any new files
for f in newfiles - oldfiles:
try:
if not isinstance(f, unicode):
f = unicode(f, 'utf-8')
fname = os.path.join(pkgdir, f)
except (UnicodeDecodeError):
fname = os.path.join(pkgdir, unicode(f, 'cp1252', 'replace'))
mkdirs(os.path.dirname(fname))
try:
user = readFile(fname)
if user:
if inhibitOverwrite:
continue;
else:
# Backup the old file
backupFile = os.path.join(backupdir, f)
mkdirs(os.path.dirname(backupFile))
writeFile(backupFile, user)
except (os.error, IOError):
pass
writeFile(fname, newar.read(f))
# upgrade each file
if not inhibitOverwrite:
for f in oldfiles & newfiles:
fname = os.path.join(pkgdir, f)
orig = oldar.read(f)
new = newar.read(f)
if new != orig:
# backup the file, if the user has modified it
try:
user = readFile(fname)
if user != orig:
backupFile = os.path.join(backupdir, f)
mkdirs(os.path.dirname(backupFile))
writeFile(backupFile, user)
except (os.error, IOError):
pass
writeFile(fname, newar.read(f))
# copy the zip
shutil.copy(src, dst)
def removeArchive(dst, pkgdir):
oldar = None
oldfiles = set()
try:
oldar = zipfile.ZipFile(dst)
oldfiles = set(filter(isFileEntry, oldar.namelist()))
oldar.close()
except (zipfile.error, IOError):
pass
# delete any orphaned files
orphanedFiles = oldfiles
for f in orphanedFiles:
try:
os.remove(os.path.join(pkgdir, f))
except (os.error, IOError):
pass
# delete the archive and destination
try:
os.remove(dst)
except (os.error, IOError):
pass
try:
os.rmdir(pkgdir)
except (os.error, IOError):
pass
def srcNewer(src, dst):
try:
return (os.path.getmtime(src) > os.path.getmtime(dst)
or os.path.getsize(src) != os.path.getsize(dst))
except (os.error, IOError):
return True
def upgradePackage(pkg, pristinedir, datadir, backupdir):
pristinePkg = os.path.join(pristinedir, os.path.basename(pkg))
# if the zip is different from the one in pristinedir
if srcNewer(pkg, pristinePkg):
(base, ext) = os.path.splitext(os.path.basename(pkg))
inhibitOverwrite = (base == "User")
upgradeArchive(pkg, pristinePkg, os.path.join(datadir, base),
os.path.join(backupdir, base), inhibitOverwrite)
def upgrade(appdir, userdir, pristinedir, datadir, backupdir):
packages = (glob.glob(appdir + "/*.sublime-package") +
glob.glob(userdir + "/*.sublime-package"))
for pkg in packages:
upgradePackage(pkg, pristinedir, datadir, backupdir)
# Delete any packages that are no longer around
depzips = (set([os.path.basename(x) for x in glob.glob(pristinedir + "/*.sublime-package")])
- set([os.path.basename(x) for x in packages]))
for dz in depzips:
pz = os.path.join(pristinedir, dz)
(base, ext) = os.path.splitext(dz)
removeArchive(pz, os.path.join(datadir, base))