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
/
ftpsyncwrapper.py
564 lines (454 loc) · 16.8 KB
/
ftpsyncwrapper.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# -*- 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 ftplib
import os
import re
import time
# FTPSync libraries
from ftpsyncfiles import Metafile, isTextFile
# ==== Initialization and optimization =====================================================
# to extract data from FTP LIST http://stackoverflow.com/questions/2443007/ftp-list-format
re_ftpListParse = re.compile("^([d-])[rxws-]{9}\s+\d+\s+\S+\s+\S+\s+(\d+)\s+(\w{1,3}\s+\d+\s+(?:\d+:\d+|\d{2,4}))\s+(.*?)$", re.M | re.I | re.U | re.L)
# error code - first 3-digit number https://tools.ietf.org/html/rfc959#page-39
re_errorCode = re.compile("[1-5]\d\d")
# For FTP LIST entries with {last modified} timestamp earlier than 6 months, see http://stackoverflow.com/questions/2443007/ftp-list-format
currentYear = int(time.strftime("%Y", time.gmtime()))
# List of FTP errors of interest
ftpError = {
'fileNotAllowed': 553,
'fileUnavailible': 550,
'pendingInformation': 350,
'ok': 200,
'passive': 227
}
ftpErrors = {
'noFileOrDirectory': 'No such file or directory',
'cwdNoFileOrDirectory': 'No such file or directory',
'fileNotExist': 'Sorry, but that file doesn\'t exist',
'permissionDenied': 'Permission denied',
'rnfrExists': 'RNFR accepted - file exists, ready for destination',
'disconnected': 'An established connection was aborted by the software in your host machine',
'timeout': 'timed out',
'typeIsNow': 'TYPE is now'
}
# SSL issue
sslErrors = {
'badWrite': 'error:1409F07F:SSL routines:SSL3_WRITE_PENDING:bad write retry',
}
# Default permissions for newly created folder
defaultFolderPermissions = "755"
# ==== Exceptions ==========================================================================
class ConnectionClosedException(Exception):
pass
class TargetAlreadyExists(Exception):
pass
# ==== Content =============================================================================
# Factory function - returns and instance of a proper class based on the configuration
# currently differs between FTP(S) and SFTP
#
# SFTP currently not implemented
#
# @type config: dict
# @type name: string
# @param name: user-defined name of a connection
#
# @return AbstractConnection
def CreateConnection(config, name):
if 'private_key' in config and config['private_key'] is not None:
raise NotImplementedError
else:
return FTPSConnection(config['connections'][name], config, name)
# Base class for all connection classes
class AbstractConnection:
# Return server path for the uploaded file relative to specified path
#
# @example:
# file_path: /user/home/NoxArt/web/index.php
# config['path']: /www/
# config['file_path']: /user/home/NoxArt/
#
# result: /www/web/index.php
#
# @type self: AbstractConnection
# @type file_path: string
#
# @return string remote file path
def _getMappedPath(self, file_path):
config = os.path.dirname(self.config['file_path'])
fragment = os.path.relpath(file_path, config)
return self._postprocessPath(os.path.join(self.config['path'], fragment))
# Tweaks a remote path before using it
#
# @type self: AbstractConnection
# @type file_path: string
#
# @return string remote file path
def _postprocessPath(self, path):
path = path.replace('\\\\', '\\')
path = path.replace('\\', '/')
path = path.replace('//','/')
return path
# FTP(S) connection
#
# uses Python's ftplib
#
# because of FTP_TLS added in v2.7 FTPSync uses imported library from v2.7.1
# shipped with the plugin
class FTPSConnection(AbstractConnection):
# Constructor
#
# @type self: FTPSConnection
# @type config: dict
# @param config: only the connection part of config
# @type name: string
# @param name: connection name from config
def __init__(self, config, generic_config, name):
self.config = config
self.generic_config = generic_config
self.name = name
self.isClosed = False
if self.config['tls'] is True:
self.connection = ftplib.FTP_TLS()
else:
self.connection = ftplib.FTP()
# Destructor, closes connection
#
# @type self: FTPSConnection
def __del__(self):
if hasattr(self, 'connection'):
self.close()
# Connects to remote server
#
# @type self: FTPSConnection
def connect(self):
self.connection.connect(self.config['host'], int(self.config['port']), int(self.config['timeout']))
self.connection.set_pasv(self.config['passive'])
# Authenticates if necessary
#
# @type self: FTPSConnection
#
# @return bool whether the authentication happened or not
def authenticate(self):
if self.config['tls'] is True:
self.connection.auth()
self.connection.prot_p()
return True
return False
# Logs into the remote server
#
# @type self: FTPSConnection
def login(self):
self.connection.login(self.config['username'], self.config['password'])
# Send an empty/keep-alive message to server
#
# @type self: FTPSConnection
def keepAlive(self):
self.connection.voidcmd("NOOP")
# Returns whether the connection is active
#
# @type self: FTPSConnection
#
# @return bool
def isAlive(self):
return self.isClosed is False and self.connection.sock is not None and self.connection.file is not None
# Uploads a file to remote server
#
# @type self: FTPSConnection
# @type file_path: string
def put(self, file_path, new_name = None, failed=False):
def action():
remote_file = file_path
if new_name is not None:
remote_file = self._postprocessPath(os.path.join(os.path.split(file_path)[0], new_name))
path = self._getMappedPath(remote_file)
if os.path.isdir(file_path):
return self.__ensurePath(path, True)
command = "STOR " + path
uploaded = open(file_path, "rb")
try:
self.connection.storbinary(command, uploaded)
except Exception, e:
if self.__isErrorCode(e, ['ok', 'passive']) is True:
self.connection.storbinary(command, uploaded)
elif self.__isErrorCode(e, 'fileNotAllowed') and failed is False:
self.__ensurePath(path)
self.put(file_path, new_name, True)
else:
raise
finally:
uploaded.close()
return self.__execute(action)
# Downloads a file from remote server
#
# @type self: FTPSConnection
# @type file_path: string
def get(self, file_path):
def action():
path = self._getMappedPath(file_path)
command = "RETR " + path
downloaded = open(file_path, "wb")
downloaded = open(file_path, "wb")
try:
self.connection.retrbinary(command, lambda data: downloaded.write(data))
except Exception, e:
if self.__isErrorCode(e, ['ok', 'passive']):
self.connection.retrbinary(command, lambda data: downloaded.write(data))
else:
raise
finally:
downloaded.close()
return self.__execute(action)
# Renames a file on remote server
#
# @type self: FTPSConnection
# @type file_path: string
# @type new_name: string
#
# @global ftpErrors
def rename(self, file_path, new_name, forced=False):
def action():
is_dir = os.path.isdir(file_path)
dirname = os.path.dirname(file_path)
path = self._getMappedPath(dirname)
base = os.path.basename(file_path)
try:
self.cwd(path)
except Exception, e:
if self.__isErrorCode(e, 'fileUnavailible'):
self.__ensurePath(path)
else:
raise
if not forced:
try:
self.connection.voidcmd("LIST " + new_name)
raise TargetAlreadyExists("Remote target {" + new_name + "} already exists")
except Exception, e:
if self.__isErrorCode(e, 'fileUnavailible'):
pass
else:
raise
try:
self.connection.voidcmd("RNFR " + base)
except Exception, e:
if self.__isError(e, 'rnfrExists'):
self.connection.voidcmd("RNTO " + new_name)
return
elif self.__isError(e, 'cwdNoFileOrDirectory') or self.__isError(e, 'fileNotExist'):
if is_dir:
self.__ensurePath( path + '/' + new_name, True )
else:
self.put(file_path, new_name)
return
else:
raise
try:
self.connection.voidcmd("RNFR " + base)
except Exception, e:
if self.__isError(e, 'rnfrExists') and str(e).find('Aborting previous'):
self.connection.voidcmd("RNTO " + new_name)
return
else:
raise
self.connection.voidcmd("RNTO " + new_name)
return self.__execute(action)
# Changes a current path on remote server
#
# @type self: FTPSConnection
# @type path: string
def cwd(self, path):
self.connection.cwd(path)
# Returns a list of content of a given path
#
# @type self: FTPSConnection
# @type file_path: string
#
# @return list<Metafile>
def list(self, file_path):
def action():
path = self._getMappedPath(file_path)
contents = []
result = []
try:
self.connection.dir(path, lambda data: contents.append(data))
except Exception, e:
if self.__isErrorCode(e, ['ok', 'passive']):
self.connection.dir(path, lambda data: contents.append(data))
else:
raise
for content in contents:
try:
if self.config['debug_extras']['print_list_result'] is True:
print "FTPSync <debug> LIST line: " + str(content)
except KeyError:
pass
split = re_ftpListParse.search(content)
if split is None:
continue
isDir = split.group(1) == 'd'
filesize = split.group(2)
lastModified = split.group(3)
name = split.group(4)
data = Metafile(name, isDir, self.__parseTime(lastModified) + self.config['time_offset'], filesize)
if name != "." and name != "..":
result.append(data)
return result
return self.__execute(action)
# Closes a connection
#
# @type self: FTPSConnection
# @type connections: dict<hash => list<connection>
# @type hash: string
def close(self, connections=[], hash=None):
try:
self.connection.quit()
except:
self.connection.close()
finally:
self.isClosed = True
if len(connections) > 0 and hash is not None:
try:
connections[hash].remove(self)
except ValueError:
return
# Changes permissions for a remote file
#
# @type self: FTPSConnection
# @type filename: string
# @type permissions: string
def chmod(self, filename, permissions):
command = "SITE CHMOD " + str(permissions) + " " + str(filename)
self.connection.voidcmd(command)
# Executes an action while handling common errors
#
# @type self: FTPSConnection
# @type callback: callback
#
# @return unknown
def __execute(self, callback):
try:
return callback()
except Exception, e:
# bad write - repeat command
if str(e).find(sslErrors['badWrite']) is True:
return callback()
# disconnected - close itself to be refreshed
elif self.__isError(e, 'disconnected') is True:
self.close()
raise
# timeout - retry
elif self.__isError(e, 'timeout') is True:
return callback()
# other exception
else:
raise
# Throws exception if closed
#
# @type self: FTPSConnection
def __checkClosed(self):
if self.isClosed is True:
raise ConnectionClosedException
# Parses string time
#
# @see http://stackoverflow.com/questions/2443007/ftp-list-format
#
# @type self: FTPSConnection
# @type: time_val: string
#
# @return unix timestamp
def __parseTime(self, time_val):
if time_val.find(':') is -1:
struct = time.strptime(time_val + str(" 00:00"), "%b %d %Y %H:%M")
else:
struct = time.strptime(str(currentYear) + " " + time_val, "%Y %b %d %H:%M")
return time.mktime(struct)
# Integer code error comparison
#
# @type self: FTPSConnection
# @type exception: Exception
# @type error: string
# @param error: key of ftpError dict
#
# @return boolean
#
# @global ftpError
# @global re_errorCode
def __isErrorCode(self, exception, error):
code = re_errorCode.search(str(exception))
if code is None:
return False
if type(error) is list:
for err in error:
if int(code.group(0)) == ftpError[err]:
return True
return False
else:
return int(code.group(0)) == ftpError[error]
# Textual error comparison
#
# @type self: FTPSConnection
# @type exception: Exception
# @type error: string
# @param error: key of ftpErrors dict
#
# @return boolean
#
# @global ftpErrors
def __isError(self, exception, error):
return str(exception).find(ftpErrors[error]) != -1
# Ensures the given path is existing and accessible
#
# @type self: FTPSConnection
# @type path: string
def __ensurePath(self, path, isFolder=False):
self.connection.cwd(self.config['path'])
relative = os.path.relpath(path, self.config['path'])
folders = self._postprocessPath(relative)
folders = folders.split("/")
index = 0
for folder in folders:
index += 1
try:
if index < len(folders) or (isFolder and index <= len(folders)):
self.connection.cwd(folder)
except Exception, e:
if self.__isErrorCode(e, 'fileUnavailible'):
try:
# create folder
self.connection.mkd(folder)
except Exception, e:
if self.__isErrorCode(e, 'fileUnavailible'):
# exists, but not proper permissions
self.chmod(folder, self.config['default_folder_permissions'])
else:
raise
# move down
self.connection.cwd(folder)
else:
raise
#class SSHConnection():