forked from aicpp/cloudsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropboxsync.py
302 lines (266 loc) · 11.3 KB
/
dropboxsync.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
#!/usr/bin/env python
import logging
import contextlib
import datetime
import dropbox
from dropbox.files import FileMetadata, FolderMetadata
import os
import time
import filters
import unicodedata
class DropboxSync(object):
"""
Class to help synchronize files to/from dropbox
use Dropbox API v2 (https://github.com/dropbox/dropbox-sdk-python)
"""
def __init__(self, args):
self.args=args
self.dbx = None
self.localDir = self.normalizeDir(args['localdir'])
self.dropboxDir = self.normalizeDir(args['dropboxdir'])
self.directionToDb = args['direction'] == 'todropbox'
self.timeoutSec = 2 * 60
self.logger = logging.getLogger(__name__)
self.logger.addHandler(logging.NullHandler())
self.dbList = []
self.locList = []
self.filterItems = []
self.sourceFilesMatched = []
def setLogger(self, logger):
self.logger = logger
# prepare
def prepare(self):
self.logger.info('--- Mode: %s' % self.args['direction'])
self.prepareDropboxAuth()
self.checkDropboxAuth()
self.checkDropboxDir()
self.checkLocalDir()
self.listDropboxFiles()
self.listLocalFiles()
self.listFilterItems()
def prepareDropboxAuth(self):
self.logger.debug('Connecting to dropbox using token...')
self.dbx = dropbox.Dropbox(self.args['token'])
self.logger.debug('Dropbox connected')
def checkLocalDir(self):
if not os.path.exists(self.localDir):
raise Exception('Local path is not exists:%s' % self.localDir)
if not os.path.isdir(self.localDir):
raise Exception('Local path is not directory:%s' % self.localDir)
def checkDropboxAuth(self):
"""
Checks Dropbox uploader is initialized to Dropbox account
"""
self.logger.debug('Getting info about dropbox account...')
acc = self.dbx.users_get_current_account()
self.logger.debug('Dropbox account: [%s_%s] mail:%s' % (acc.country, acc.locale, acc.email))
def checkDropboxDir(self):
# TODO: check dir exists
pass
def listLocalFiles(self):
self.logger.debug('Getting list of local files...')
locList = [unicodedata.normalize('NFC', f.decode('utf-8')) for f in os.listdir(self.localDir) if os.path.isfile(os.path.join(self.localDir,f))]
self.locList = [self.filterItemByLocal(f) for f in locList]
self.logger.debug('Local files:%s' % len(self.locList))
return True
def mtime(self, filePath):
mtime = os.path.getmtime(filePath)
return datetime.datetime(*time.gmtime(mtime)[:6])
# t = os.path.getmtime(filePath)
# return datetime.datetime.fromtimestamp(t)
def filterItemByLocal(self, fileName):
filePath = os.path.join(self.localDir, fileName)
return filters.FileFilterItem(
name=fileName,
mtime=self.mtime(filePath),
size=os.path.getsize(filePath))
def filterItemByDropbox(self, fileMd):
return filters.FileFilterItem(
name=fileMd.name,
mtime=fileMd.client_modified,
size=fileMd.size
)
def normalizeDir(self, directory):
result = directory.replace(os.path.sep, '/')
result = os.path.expanduser(result)
while '//' in result:
result = result.replace('//', '/')
result = result.rstrip('/')
result = unicodedata.normalize('NFC', result.decode('utf-8'))
return result
# filtration
def listFilterItems(self):
resItems = []
if self.directionToDb:
self.filterItems = self.locList
else:
self.filterItems = self.dbList
def filterSourceFiles(self, filters):
resFiles = self.filterItems
sourceCount = len(resFiles)
self.logger.debug('Source files:%s' % (len(resFiles)))
for fltr in filters:
prevCount = len(resFiles)
resFiles = fltr.filterFiles(resFiles)
resCount = len(resFiles)
if resCount != prevCount:
self.logger.debug('Filter \'%s\': %s -> %s' % (fltr.__class__.__name__, prevCount, resCount))
self.sourceFilesMatched = resFiles
self.logger.info('--- Filter source files: %d -> %d' % (sourceCount, len(resFiles)))
# synchronize
def synchronize(self):
# for debug
#self.fixLocalTimestamps()
if self.directionToDb:
self.deleteDropboxFiles()
self.syncToDropbox()
else:
self.deleteLocalFiles()
self.syncToLocal()
return True
def deleteLocalFiles(self):
# remove local
sourceNames = [fileItem.fileName for fileItem in self.sourceFilesMatched]
delList = [fileItem for fileItem in self.locList if fileItem.fileName not in sourceNames]
if not delList:
return
self.logger.debug('Local files to delete:%s' % len(delList))
for fileItem in delList:
os.remove(os.path.join(self.localDir, fileItem.fileName))
self.logger.info('--- Delete %d/%d local files' % (len(delList), len(self.locList)))
def syncToLocal(self):
countSuccess = 0
countSkip = 0
countFails = 0
for fileItem in self.sourceFilesMatched:
if fileItem in self.locList:
self.logger.debug('Skip existed:%s' % fileItem.fileName)
countSkip += 1
continue
if self.downloadFile(fileItem):
countSuccess += 1
else:
countFails += 1
# print stat
strSkip = ' Skip:%d' % countSkip if countSkip else ''
strFails = ' Fails:%d' % countFails if countFails else ''
self.logger.info('--- Download %d/%d%s%s' % (countSuccess, len(self.sourceFilesMatched), strSkip, strFails))
def deleteDropboxFiles(self):
""" Delete not matched files from Dropbox directory """
sourceNames = [fileItem.fileName for fileItem in self.sourceFilesMatched]
delList = [fileItem for fileItem in self.dbList if fileItem.fileName not in sourceNames]
if not delList:
return
self.logger.debug('Dropbox files to delete:%s' % len(delList))
for fileItem in delList:
self.deleteFile(fileItem)
self.logger.info('--- Success delete %d/%d dropbox files' % (len(delList), len(self.dbList)))
def syncToDropbox(self):
countSuccess = 0
countSkip = 0
countFails = 0
for fileItem in self.sourceFilesMatched:
if fileItem in self.dbList:
self.logger.debug('Skip existed:%s' % fileItem.fileName)
countSkip += 1
continue
if self.uploadFile(fileItem):
countSuccess += 1
else:
countFails += 1
# print stat
strSkip = ' Skip:%d' % countSkip if countSkip else ''
strFails = ' Fails:%d' % countFails if countFails else ''
self.logger.info('--- Success upload %d/%d%s%s' % (countSuccess, len(self.sourceFilesMatched), strSkip, strFails))
# dropbox helpers
def listDropboxFiles(self):
"""List a folder.
Return an array of filter items
"""
path = self.dropboxDir
self.logger.debug('Downloading dropbox list files...')
try:
with self.stopwatch(__name__):
res = self.dbx.files_list_folder(path)
except dropbox.exceptions.ApiError as err:
self.dbList = []
raise Exception('Folder listing failed for %s -- assumed empty:%s' % (path, err))
else:
self.logger.debug('Dropbox files:%s' % len(res.entries))
self.dbList = [self.filterItemByDropbox(fileMd) for fileMd in res.entries]
def downloadFile(self, fileItem):
"""Download a file.
Return True when success, or False if error occurs.
"""
fileName = fileItem.fileName
dbItem = next((f for f in self.dbList if f.fileName == fileName), None)
dbPath = os.path.join(self.dropboxDir, fileName)
locPath = os.path.join(self.localDir, fileName)
self.logger.debug('Downloading %s (%d bytes) ...' % (fileName, dbItem.fileSize))
with self.stopwatch('downloading'):
try:
md = self.dbx.files_download_to_file(locPath, dbPath)
except dropbox.exceptions.ApiError as err:
raise Exception('%s - API error:%s' % (fileName, err))
self.logger.debug('Success download - %s (%d bytes)' % (fileName, dbItem.fileSize))
return True
def uploadFile(self, fileItem):
"""Upload a file.
Return the request response, or None in case of error.
"""
fileName = fileItem.fileName
dbPath = os.path.join(self.dropboxDir, fileName)
locPath = os.path.join(self.localDir, fileName)
mode = dropbox.files.WriteMode.overwrite
# mtime0 = os.path.getmtime(locPath)
# mtime = datetime.datetime(*time.gmtime(mtime0)[:6])
mtime = self.mtime(locPath)
with open(locPath, 'rb') as f:
data = f.read()
self.logger.debug('Uploading %s (%d bytes) ...' % (fileName, len(data)))
# self.logger.debug('mtime %d %d ...' % (mtime, fileItem.fileModifyTime))
with self.stopwatch('uploading'):
try:
res = self.dbx.files_upload(
data, dbPath, mode,
client_modified=mtime,
autorename=False,
mute=True)
except dropbox.exceptions.ApiError as err:
raise Exception('%s - API error:%s' % (fileName, err))
# self.logger.debug('Success upload - res:%s' % (res))
self.logger.debug('Success upload - %s (%s bytes)' % (fileName, len(data)))
return True
def deleteFile(self, fileItem):
self.logger.debug('Deleting - \'%s\'' % (fileItem.fileName))
with self.stopwatch('deleting'):
try:
md = self.dbx.files_delete(os.path.join(self.dropboxDir, fileItem.fileName))
except dropbox.exceptions.ApiError as err:
raise Exception('%s - API error:%s' % (fileItem.fileName, err))
self.logger.debug('Success delete - %s' % fileItem.fileName)
# process helpers
def fixLocalTimestamps(self):
for fileName in self.locList:
self._debugFixLocalTimestamp(fileName)
self.logger.debug('Timestamps fixed in local files:%s' % len(self.locList))
def _debugFixLocalTimestamp(self, fileName):
from datetime import datetime
basename = os.path.splitext(fileName)[0]
newTime0 = None
if "." in basename:
newTime0 = datetime.strptime(basename, "%Y-%m-%d %H.%M.%S")
else:
newTime0 = datetime.strptime(basename, "%Y-%m-%d %H-%M-%S")
newTime = time.mktime(newTime0.timetuple())
# self.logger.debug('%s - %s -> %s' % (fileName, newTime0, newTime))
os.utime(os.path.join(self.localDir, fileName), (newTime, newTime))
@contextlib.contextmanager
def stopwatch(self, message):
"""Context manager to print how long a block of code took."""
t0 = time.time()
try:
yield
finally:
t1 = time.time()
self.logger.debug('Total elapsed time for %s: %.3f' % (message, t1 - t0))