forked from aicpp/cloudsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudsync.py
executable file
·78 lines (60 loc) · 2.14 KB
/
cloudsync.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
#!/usr/bin/python
import os
import sys, locale
import dropboxsync
import logging
import logger as lgr
import argparse
import filters as ftr
def isCronMode():
return not os.isatty(sys.stdin.fileno())
#configure logger
def createLogger():
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
if not isCronMode():
logger.setLevel(logging.DEBUG)
logger.addHandler(lgr.createConsoleHandler())
logFilePath = lgr.defaultLogFilePath(__file__)
logger.addHandler(lgr.createLogFileHandler(logFilePath))
return logger
def createParser():
# parse command line arguments
parser = argparse.ArgumentParser(description='Sync files between local directory and Dropbox (in both directions)')
parser.add_argument('--dropboxdir', required=True,
help='Directory in your Dropbox')
parser.add_argument('--localdir', required=True,
help='Local directory')
parser.add_argument('--direction', required=True, choices=['todropbox','tolocal'],
help='Direction to sync')
parser.add_argument('--token', required=True,
help='Access token (see https://www.dropbox.com/developers/apps)')
# additional filtration
parser.add_argument('--match-days', nargs='?', type=int,
help='Copy only newer files (file modification time is newer last N-days)')
return parser
def main():
parser = createParser()
args = parser.parse_args()
logger = createLogger()
# logger.debug("isCron:%s" % isCronMode())
# logger.debug('vars:%s' % vars(args))
# sys.exit(0)
# parse token
if not args.token:
logger.error('--token is mandatory')
sys.exit(2)
try:
#
dbSync = dropboxsync.DropboxSync(vars(args))
dbSync.setLogger(logger)
dbSync.prepare()
filters = []
filters.append(ftr.FileFilterDays(matchDays=dbSync.args['match_days']))
filters.append(ftr.FileFilterMask())
dbSync.filterSourceFiles(filters)
dbSync.synchronize()
except:
logger.exception('')
if __name__ == '__main__':
main()