2013-08-18 18:22:36 +00:00
|
|
|
#!/usr/bin/env python
|
2013-09-15 07:39:57 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
# Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
#
|
|
|
|
# Author: Soonho Kong
|
|
|
|
#
|
|
|
|
|
2015-01-18 16:00:47 +00:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-08-18 18:22:36 +00:00
|
|
|
import dropbox
|
|
|
|
import os
|
2013-09-15 07:39:57 +00:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--dropbox-token', type=str, help='Dropbox token for authentication', required=True)
|
|
|
|
parser.add_argument('--destpath', type=str, help='Destination in Dropbox', required=True)
|
|
|
|
parser.add_argument('--srcpath', type=str, help='Local directory to copy')
|
|
|
|
parser.add_argument('--copylist', type=str, help='File containing a list of files to copy')
|
|
|
|
parser.add_argument('--deletelist', type=str, help='File containing a list of files to delete')
|
|
|
|
args = parser.parse_args()
|
2013-08-18 18:22:36 +00:00
|
|
|
|
2013-09-15 07:39:57 +00:00
|
|
|
if not args.srcpath and not args.copylist and not args.deletelist:
|
2015-01-18 16:00:47 +00:00
|
|
|
print("You need to specify one of the following options:")
|
|
|
|
print(" --srcpath,")
|
|
|
|
print(" --copylist,")
|
|
|
|
print(" --deletelist")
|
2013-09-15 07:39:57 +00:00
|
|
|
exit(1)
|
2013-08-18 18:22:36 +00:00
|
|
|
|
2013-09-15 07:39:57 +00:00
|
|
|
access_token = args.dropbox_token
|
|
|
|
server_path = args.destpath
|
2013-08-18 18:22:36 +00:00
|
|
|
|
|
|
|
# Connect DROPBOX
|
2013-09-15 07:39:57 +00:00
|
|
|
try:
|
|
|
|
client = dropbox.client.DropboxClient(access_token)
|
|
|
|
client.account_info()
|
|
|
|
except:
|
2015-01-18 16:00:47 +00:00
|
|
|
print("Failed to connect to Dropbox. Please check the access token.")
|
2013-09-15 07:39:57 +00:00
|
|
|
exit(1)
|
2013-08-18 18:22:36 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
def copy_file_with_retry(fullpath, targetpath, max_try):
|
|
|
|
global count
|
|
|
|
print("==> " + targetpath)
|
|
|
|
try:
|
|
|
|
handle = open(fullpath)
|
2013-09-15 07:39:57 +00:00
|
|
|
except:
|
2013-09-16 03:15:03 +00:00
|
|
|
print("FAILED: " + fullpath + " not found")
|
2013-09-15 07:39:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2013-08-19 03:21:25 +00:00
|
|
|
response = client.put_file(targetpath, handle, True)
|
2013-08-18 18:22:36 +00:00
|
|
|
except:
|
|
|
|
handle.close()
|
|
|
|
print("FAILED: " + targetpath)
|
|
|
|
if count < max_try:
|
|
|
|
count = count + 1
|
|
|
|
copy_file_with_retry(fullpath, targetpath, max_try)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
count = 0
|
|
|
|
handle.close()
|
|
|
|
|
|
|
|
def upload_dir_to_dropbox(localDir, serverDir):
|
|
|
|
localDir = os.path.expanduser(localDir)
|
|
|
|
for dirName, subdirList, fileList in os.walk(localDir):
|
|
|
|
for fname in fileList:
|
|
|
|
fullpath = os.path.normpath(dirName + "/" + fname)
|
|
|
|
targetpath = os.path.normpath(serverDir + "/" + fullpath[len(localDir):])
|
|
|
|
copy_file_with_retry(fullpath, targetpath, 5)
|
|
|
|
|
2013-09-15 07:39:57 +00:00
|
|
|
def remove_from_dropbox(p):
|
2013-08-18 18:22:36 +00:00
|
|
|
try:
|
2013-09-15 07:39:57 +00:00
|
|
|
client.file_delete(p)
|
|
|
|
print (p + " deleted from Dropbox")
|
2013-08-18 18:22:36 +00:00
|
|
|
except dropbox.rest.ErrorResponse:
|
2013-09-15 07:39:57 +00:00
|
|
|
print (p + " not exists")
|
2013-08-18 18:22:36 +00:00
|
|
|
|
2013-09-15 07:39:57 +00:00
|
|
|
if args.srcpath:
|
|
|
|
local_path = args.srcpath
|
|
|
|
print ("Copy: " + local_path + " ==> " + server_path)
|
|
|
|
upload_dir_to_dropbox(local_path, server_path)
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if args.copylist:
|
|
|
|
copylist = args.copylist
|
|
|
|
print ("Copy files in " + copylist + " ==> " + server_path)
|
|
|
|
try:
|
|
|
|
copylist_handle = open(copylist, "r")
|
|
|
|
except IOError:
|
2015-01-18 16:00:47 +00:00
|
|
|
print('Failed to open ' + copylist)
|
2013-09-15 07:39:57 +00:00
|
|
|
for line in copylist_handle:
|
|
|
|
fullpath = os.path.normpath(line.strip())
|
|
|
|
copy_file_with_retry(fullpath, os.path.normpath(server_path + "/" + fullpath), 5)
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if args.deletelist:
|
|
|
|
deletelist = args.deletelist
|
|
|
|
print ("Delete files in " + deletelist + " from Dropbox " + server_path)
|
|
|
|
try:
|
|
|
|
deletelist_handle = open(deletelist, "r")
|
|
|
|
except IOError:
|
2015-01-18 16:00:47 +00:00
|
|
|
print('Failed to open ' + deletelist)
|
2013-09-15 07:39:57 +00:00
|
|
|
deletelist_handle = open(deletelist, "r")
|
|
|
|
for line in deletelist_handle:
|
|
|
|
fullpath = os.path.normpath(line.strip())
|
|
|
|
remove_from_dropbox(os.path.normpath(server_path + "/" + fullpath))
|
|
|
|
exit(0)
|