This repository has been archived by the owner on May 13, 2020. It is now read-only.
forked from NoxArt/SublimeText2-FTPSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpsyncfiles.py
321 lines (247 loc) · 8.79 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
# -*- 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 | petruzelka@noxart.cz | @NoxArt
# @copyright (c) 2012 Jiri "NoxArt" Petruzelka
# @link https://github.com/NoxArt/SublimeText2-FTPSync
# ==== Libraries ===========================================================================
# Python's built-in libraries
import os
import datetime
import fnmatch
import re
# ==== Initialization and optimization =====================================================
# difference in time when comes to local vs remote {last modified} [s]
timeDifferenceTolerance = 1
# limit for breaking down a filepath structure when looking for config files
nestingLimit = 30
# bom marks (decimal)
bomMarks = {
'utf8': [239,187,191],
'utf16be': [254,255],
'utf16le': [255,254],
'utf32be': [0,0,254,255],
'utf32le': [255,254,0,0],
'utf7': [43,47,118,56,43,47,118,57,43,47,118,43,43,47,118,47],
'utf1': [247,100,76],
'utfebcdic': [221,115,102,115],
'scsu': [14,254,255],
'bocu-1': [251,238,40],
'gb18030': [132,49,149,51]
}
bomMaxLength = 16
# file_path[string] => textual[boolean]
isTextCache = {}
# ==== Content =============================================================================
# A file representation with helper methods
class Metafile:
def __init__(self, name, isDir, lastModified, filesize, path=None):
self.name = name
self.isDir = bool(isDir)
self.lastModified = float(lastModified)
self.filesize = float(filesize)
self.path = path
def getName(self):
return self.name
def getPath(self):
return self.path
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 isNewerThan(self, compared_file):
if type(compared_file) is str or type(compared_file) is unicode:
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 type(compared_file) is str or type(compared_file) is unicode:
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)
# Converts file_path to Metafile
#
# @type file_path: string
#
# @return Metafile
def fileToMetafile(file_path):
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'):
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 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, getConfigFile(target)])
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)
if target not in file_names:
file_names.append(target)
result[target] = fileToMetafile(target)
for folder in dirnames:
result = dict(result.items() + 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:
if file_path in metafilesBefore and metafilesAfter[file_path].isNewerThan(metafilesBefore[file_path]):
changed.append(metafilesAfter[file_path])
return changed
# Guesses whether given file is textual or not
#
# @type file_path: string
# @type asciiWhitelist: None|list<string>
# @type binaryWhitelist: None|list<string>
#
# @return boolean whether it's likely textual or binary
def isTextFile(file_path, asciiWhitelist=None, binaryWhitelist=None):
return False
# check cache
if file_path in isTextCache:
return isTextCache[file_path]
# check extension
extension = os.path.splitext(file_path)[1][1:]
if extension:
if type(asciiWhitelist) is list:
if extension in asciiWhitelist:
isTextCache[file_path] = True
return True
if type(binaryWhitelist) is list:
if extension in binaryWhitelist:
isTextCache[file_path] = False
return False
# check BOM
f = open(file_path, 'rb')
beginning = f.read(bomMaxLength)
f.close()
begin = []
for char in beginning:
begin.append(ord(char))
for encoding in bomMarks:
subarray = begin[0:len(bomMarks[encoding])]
if subarray == begin:
isTextCache[file_path] = True
return True
# is not
isTextCache[file_path] = False
return False