forked from NoxArt/SublimeText2-FTPSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpsyncfiles.py
474 lines (362 loc) · 11.3 KB
/
ftpsyncfiles.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# -*- coding: utf-8 -*-
# Copyright (c) 2012 Jiri "NoxArt" Petruzelka
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# @author Jiri "NoxArt" Petruzelka | [email protected] | @NoxArt
# @copyright (c) 2012 Jiri "NoxArt" Petruzelka
# @link https://github.com/NoxArt/SublimeText2-FTPSync
# ==== Libraries ===========================================================================
# Python's built-in libraries
import datetime
import fnmatch
import os
import re
import sys
import tempfile
# FTPSync libraries
if sys.version < '3':
from ftpsynccommon import Types
else:
from FTPSync.ftpsynccommon import Types
# ==== Initialization and optimization =====================================================
# limit for breaking down a filepath structure when looking for config files
nestingLimit = 30
# permission triples
triples = {
'---': 0,
'--x': 1,
'--s': 1,
'--t': 1,
'-w-': 2,
'-wx': 3,
'-ws': 3,
'-wt': 3,
'r--': 4,
'r-x': 5,
'r-s': 5,
'r-t': 5,
'rw-': 6,
'rwx': 7,
'rws': 7,
'rwt': 7,
}
# ==== Content =============================================================================
# Returns whether the variable is some form os string
def isString(var):
var_type = type(var)
if sys.version[0] == '3':
return var_type is str or var_type is bytes
else:
return var_type is str or var_type is unicode
# A file representation with helper methods
class Metafile:
def __init__(self, name, isDir, lastModified, filesize, path=None, permissions=None):
self.name = name
self.isDir = bool(isDir)
self.lastModified = lastModified
if self.lastModified is not None:
self.lastModified = float(self.lastModified)
self.filesize = filesize
if self.filesize is not None:
self.filesize = float(self.filesize)
self.path = path
self.permissions = permissions
def getName(self):
return self.name
def getPath(self):
return self.path
def getPermissions(self):
return self.permissions
def getPermissionsNumeric(self):
symbolic = self.permissions
numeric = "0"
numeric += str(triples[symbolic[0:3]])
numeric += str(triples[symbolic[3:6]])
numeric += str(triples[symbolic[6:9]])
return numeric
def isDirectory(self):
return self.isDir
def getLastModified(self):
return self.lastModified
def getLastModifiedFormatted(self, format='%Y-%m-%d %H:%M'):
return formatTimestamp(self.lastModified, format)
def getFilesize(self):
return self.filesize
def isSameFilepath(self, filepath):
return os.path.realpath(self.getPath()) == os.path.realpath(filepath)
def isNewerThan(self, compared_file):
if self.lastModified is None:
return False
if isString(compared_file):
if os.path.exists(compared_file) is False:
return False
lastModified = os.path.getmtime(compared_file)
elif isinstance(compared_file, Metafile):
lastModified = compared_file.getLastModified()
else:
raise TypeError("Compared_file must be either string (file_path) or Metafile instance")
return self.lastModified > lastModified
def isDifferentSizeThan(self, compared_file):
if self.filesize is None:
return False
if isString(compared_file):
if os.path.exists(compared_file) is False:
return False
lastModified = os.path.getsize(compared_file)
elif isinstance(compared_file, Metafile):
lastModified = compared_file.getLastModified()
else:
raise TypeError("Compared_file must be either string (file_path) or Metafile instance")
return self.filesize != os.path.getsize(compared_file)
# Detects if object is a string and if so converts to unicode, if not already
#
# @source http://farmdev.com/talks/unicode/
# @author Ivan Krstić
def to_unicode_or_bust(obj, encoding='utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj
# Converts file_path to Metafile
#
# @type file_path: string
#
# @return Metafile
def fileToMetafile(file_path):
if sys.version[0] < '3' and type(file_path) is str:
file_path = file_path.decode('utf-8')
elif type(file_path) is bytes:
file_path = file_path.decode('utf-8')
name = os.path.basename(file_path)
path = file_path
isDir = os.path.isdir(file_path)
lastModified = os.path.getmtime(file_path)
filesize = os.path.getsize(file_path)
return Metafile(name, isDir, lastModified, filesize, path)
# Returns a timestamp formatted for humans
#
# @type timestamp: int|float
# @type format: string
# @param format: see http://docs.python.org/library/time.html#time.strftime
#
# @return string
def formatTimestamp(timestamp, format='%Y-%m-%d %H:%M'):
if timestamp is None:
return "-"
return datetime.datetime.fromtimestamp(int(timestamp)).strftime(format)
# Get all folders paths from given path upwards
#
# @type file_path: string
# @param file_path: absolute file path to return the paths from
#
# @return list<string> of file paths
#
# @global nestingLimit
def getFolders(file_path):
if file_path is None:
return []
folders = [file_path]
limit = nestingLimit
while True:
split = os.path.split(file_path)
# nothing found
if len(split) == 0:
break
# get filepath
file_path = split[0]
limit -= 1
# nothing else remains
if len(split[1]) == 0 or limit < 0:
break
folders.append(split[0])
return folders
# Finds a real file path among given folder paths
# and returns the path or None
#
# @type folders: list<string>
# @param folders: list of paths to folders to look into
# @type file_name: string
# @param file_name: file name to search
#
# @return string file path or None
def findFile(folders, file_name):
if folders is None:
return None
for folder in folders:
if isString(folder) is False:
folder = folder.decode('utf-8')
if os.path.exists(os.path.join(folder, file_name)) is True:
return folder
return None
# Returns unique list of file paths with corresponding config
#
# @type folders: list<string>
# @param folders: list of paths to folders to filter
# @type getConfigFile: callback<file_path:string>
#
# @return list<string> of file paths
def getFiles(paths, getConfigFile):
if paths is None:
return []
files = []
fileNames = []
for target in paths:
if target not in fileNames:
fileNames.append(target)
files.append([target.encode('utf-8'), getConfigFile(target.encode('utf-8'))])
return files
# Goes through paths using glob and returns list of Metafiles
#
# @type pattern: string
# @param pattern: glob-like filename pattern
# @type root: string
# @param root: top searched directory
#
# @return list<Metafiles>
def gatherMetafiles(pattern, root):
if pattern is None:
return []
result = {}
file_names = []
for subroot, dirnames, filenames in os.walk(root):
for filename in fnmatch.filter(filenames, pattern):
target = os.path.join(subroot, filename).encode('utf-8')
if target not in file_names:
file_names.append(target)
result[target] = fileToMetafile(target)
for folder in dirnames:
result.update(gatherMetafiles(pattern, os.path.join(root, folder)).items())
return result
# Returns difference using lastModified between file dicts
#
# @type metafilesBefore: dict
# @type metafilesAfter: dict
#
# @return list<Metafiles>
def getChangedFiles(metafilesBefore, metafilesAfter):
changed = []
for file_path in metafilesAfter:
#file_path = Types.u(file_path)
if file_path in metafilesBefore and metafilesAfter[file_path].isNewerThan(metafilesBefore[file_path]):
changed.append(metafilesAfter[file_path])
return changed
# Abstraction of os.rename for replacing cases
#
# @type source: string
# @param source: source file path
# @type destination: string
# @param destination: destination file path
def replace(source, destination):
destinationTemp = destination + '.ftpsync.bak'
try:
os.rename(source, destination)
except OSError:
os.rename(destination, destinationTemp)
try:
os.rename(source, destination)
os.unlink(destinationTemp)
except OSError as e:
os.rename(destinationTemp, destination)
raise
# Performing operation on temporary file and replacing it back
#
# @type operation: callback(file)
# @param operation: operation performed on temporary file
# @type permissions: int (octal)
# @type mode: string
# @param mode: file opening mode
def viaTempfile(file_path, operation, permissions, mode):
if permissions is None:
permissions = '0755'
exceptionOccured = None
directory = os.path.dirname(file_path)
if os.path.exists(directory) is False:
os.makedirs(directory, int(permissions, 8))
temp = tempfile.NamedTemporaryFile(mode, suffix = '.ftpsync.temp', dir = directory, delete = False)
try:
operation(temp)
except Exception as exp:
exceptionOccured = exp
finally:
temp.flush()
temp.close()
if exceptionOccured is None:
if os.path.exists(file_path) is False:
created = open(file_path, 'w+')
created.close()
replace(temp.name, file_path)
if os.path.exists(temp.name):
os.unlink(temp.name)
if exceptionOccured is not None:
raise exceptionOccured
# Guesses whether given file is textual or not
#
# @type file_path: string
# @type asciiWhitelist: list<string>
#
# @return boolean whether it's likely textual or binary
def isTextFile(file_path, asciiWhitelist):
fileName, fileExtension = os.path.splitext(file_path)
if fileExtension and fileExtension[1:] in asciiWhitelist:
return True
return False
# Adds . and .. entries if missing in the collection
#
# @type contents: list<Metadata>
#
# @return list<metadata>
def addLinks(contents):
hasSelf = False
hasUp = False
single = None
for entry in contents:
if entry.getName() == '.':
hasSelf = True
elif entry.getName() == '..':
hasUp = True
if hasSelf and hasUp:
return contents
else:
single = entry
if single is not None:
if hasSelf == False:
entrySelf = Metafile('.', True, None, None, single.getPath(), None)
contents.append(entrySelf)
if hasUp == False:
entryUp = Metafile('..', True, None, None, single.getPath(), None)
contents.append(entryUp)
return contents
# Return a relative filepath to path either from the current directory or from an optional start directory
#
# Contains a fix for a bug #5117 not fixed in a version used by ST2
#
# @type path: string
# @param path: destination path
# @type path: string
# @param path: starting (root) path
#
# @return string relative path
def relpath(path, start):
relpath = os.path.relpath(path, start)
if start == '/' and relpath[0:2] == '..':
relpath = relpath[3:]
return relpath