예제 #1
0
    def _test_metadata(self, expected_exception, status, reason,
                       how_many_messages=0):
        from dropbox.rest import ErrorResponse

        class client(object):

            def __init__(self, status, reason):
                self.status = status
                self.reason = reason

            def metadata(self, path):
                class http_resp(object):
                    status = self.status
                    reason = self.reason

                    @staticmethod
                    def read():
                        return ''
                raise ErrorResponse(http_resp)

        user = create_user()
        service = self._getOUT(user)
        service.client = client(status, reason)
        request = testing.DummyRequest()
        self.setUpPyramid(request=request)

        with self.assertRaises(expected_exception):
            service.list('/')
        sent_messages = self.mail_stub.get_sent_messages(to=user.email)
        self.assertEqual(len(sent_messages), how_many_messages)
예제 #2
0
def apply_delta(root, e):
    queue = Queue.Queue(0)
    path, metadata = e
    branch, leaf = split_path(path.encode("utf-8"))

    if metadata is not None:
        # Traverse down the tree until we find the parent folder of the entry
        # we want to add. Create any missing folders along the way.
        children = root
        for part in branch:
            node = get_or_create_child(children, part)
            # If there's no folder here, make an empty one.
            if not node.is_folder():
                node.content = {}
            children = node.content

        # Create the file/folder.
        node = get_or_create_child(children, leaf)
        node.path = metadata['path']  # Save the un-lower-cased path.
        if metadata['is_dir']:
            print(' + ' + metadata['path'])

            # Only create an empty folder if there isn't one there already.
            if not node.is_folder():
                node.content = {}
            if delta_switch == 0:
                try:
                    os.makedirs(dropboxPath + "/" + metadata['path'])
                    print(" > Directory created.")
                except:
                    pass
        else:
            node.content = metadata['size'], metadata['modified']
            tmp = ['sync', node.path]
            directory = normpath(join(dropboxPath, dirname(node.path)))

            print(' + ' + node.path)

            if delta_switch == 0:
                try:
                    if not exists(directory): os.makedirs(directory)
                    queue.put(client(tmp))
                except Exception, err:
                    print(" x Something went wrong. Unable to get file.")
                    traceback.print_exc()
예제 #3
0
def apply_delta(root, e):
    queue = Queue.Queue(0)
    path, metadata = e
    branch, leaf = split_path(path.encode("utf-8"))

    if metadata is not None:
        # Traverse down the tree until we find the parent folder of the entry
        # we want to add. Create any missing folders along the way.
        children = root
        for part in branch:
            node = get_or_create_child(children, part)
            # If there's no folder here, make an empty one.
            if not node.is_folder():
                node.content = {}
            children = node.content

        # Create the file/folder.
        node = get_or_create_child(children, leaf)
        node.path = metadata['path']  # Save the un-lower-cased path.
        if metadata['is_dir']:
            print(' + ' + metadata['path'])

            # Only create an empty folder if there isn't one there already.
            if not node.is_folder():
                node.content = {}
            if delta_switch == 0:
                try:
                    os.makedirs(dropboxPath + "/" + metadata['path'])
                    print(" > Directory created.")
                except:
                    pass
        else:
            node.content = metadata['size'], metadata['modified']
            tmp = ['sync', node.path]
            directory = normpath(join(dropboxPath, dirname(node.path)))

            print(' + ' + node.path)

            if delta_switch == 0:
                try:
                    if not exists(directory): os.makedirs(directory)
                    queue.put(client(tmp))
                except Exception, err:
                    print(" x Something went wrong. Unable to get file.")
                    traceback.print_exc()
예제 #4
0
def client(parameters):
    cmd = parameters[0]

    if cmd == "ls":
        path = parameters[1]
        to_file = parameters[2]

        resp = api_client.metadata(path)
        file = open(to_file, "w")

        if 'contents' in resp:
            for f in resp['contents']:
                name = os.path.basename(f['path'])
                encoding = locale.getdefaultlocale()[1]
                file.write(('%s\n' % name).encode(encoding))
        file.close()

    if cmd == "share":
        param = parameters[1]
        f = api_client.share(param)
        url = str(f['url'])
        print(" > Generated link: " + url)

    elif cmd == "delta":
        state = load_state()
        cursor = state.get('cursor')
        tree = state['tree']
        page = 0
        changed = False
        page_limit = 15

        try:
            delta_switch = parameter[1]
        except:
            delta_switch = 0

        while (page_limit is None) or (page < page_limit):
            # Make an int for progress/total
            progress = 0
            total = 0
            # Get /delta results from Dropbox
            result = api_client.delta(cursor)
            page += 1
            if result['reset'] == True:
                sys.stdout.write('reset\n')
                changed = True
                tree = {}
            cursor = result['cursor']
            # Apply the entries one by one to our cached tree.
            for delta_entry in result['entries']:
                total = total + 1
            for delta_entry in result['entries']:
                changed = True
                progress = progress + 1
                print("Current entry: " + str(progress) + "/" + str(total))
                apply_delta(tree, delta_entry)
                cursor = result['cursor']
                #if not result['has_more']: break

        if not changed:
            sys.stdout.write('No updates.\n')
        else:
            # Save state
            state['cursor'] = cursor
            state['tree'] = tree
            save_state(state)

    elif cmd == "put":
        from_path = parameters[1]
        to_path = parameters[2]
        notify = parameters[3]  # it can be 'add' or 'upd'

        from_file = open(os.path.expanduser(from_path), 'rb')
        # try:
        api_client.put_file("/" + to_path, from_file)
        # except:
        #	print(" x Unable to upload file. ")
        common.orphiliaNotify(notify, from_path)

    elif cmd == "unlink":
        sess.unlink()
        print(" > Unlinked :C")

    elif cmd == "cat":
        f = api_client.get_file("/" + path)
        stdout.write(f.read())
        stdout.write("\n")

    elif cmd == "mkdir":
        path = parameters[1]
        try:
            api_client.file_create_folder("/" + path)
        except:
            print(" x Unable to make directory " + path)
        print(" > Directory \'" + path + "\' created")

    elif cmd == "rm":
        path = path_rewrite.rewritepath('posix', parameters[1])
        try:
            api_client.file_delete("/" + path)
            common.orphiliaNotify('rm', path)
        except:
            print(" x Unable to remove file " + path)

    elif cmd == "mv":
        from_path = parameters[1]
        to_path = parameters[2]
        try:
            api_client.file_move("/" + from_path, "/" + to_path)
        except:
            print(" x Unable to move file " + from_path + " to " + to_path)

    elif cmd == "account_info":
        f = api_client.account_info()
        pprint.PrettyPrinter(indent=2).pprint(f)

    elif cmd == "uid":
        f = api_client.account_info()
        uid = str(f['uid'])
        return uid

    elif cmd == "get":
        from_path = parameters[1]
        to_path = parameters[2]

        resp = api_client.metadata(from_path)
        modified = resp['client_mtime']

        try:
            open(os.path.expanduser(to_path), 'rb')
        except:
            pass

        date1 = time.mktime(parse(modified).timetuple())
        f = api_client.get_file("/" + from_path)
        dirname = os.path.dirname(os.path.abspath(to_path))

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        file = open(to_path, "wb")
        try:
            file.write(f.read())
            file.close()
            os.utime(os.path.normpath(to_path), (date1, date1))
        except:
            print(" x Unable to save file.")

    elif cmd == "sync":
        """
        Command which syncs files.
            - if both file exists, compare the dates:
                - local is older? download from server
                - local is newer? remove from server and upload local
            - if only local exists, upload
            - if exists only on the server, download
        """
        # get path that was sent to client
        path = parameters[1]
        localPath = os.path.normpath(dropboxPath + '/' + path)
        # assume that both files actually exist
        change = 'upd'

        # check if file exists on the server, try to get response data
        try:
            resp = api_client.metadata(path)
            modified = resp['client_mtime']
            dropboxTime = parse(modified)
        except:
            # ok, it doesn't, so we're going to upload that file
            dropboxTime = 0
            change = 'add'

        # check if local file exists and try to get it's modified date
        try:
            localTime = datetime.datetime.fromtimestamp(
                os.path.getmtime(localPath), tz=tz.tzutc())
        except:
            change = 'get'
            localTime = 0

        # uhm, this was not supposed to happen...
        if dropboxTime == 0 and localTime == 0:
            print(
                " x WTF?! It looks like there is no file on the server nor locally..."
            )
            exit()
        else:
            if change == "upd":
                # both file exists, decide what's next
                if localTime < dropboxTime:
                    # local file is older, download from server
                    change = "get"
                else:
                    # local file is newer, remove from server
                    tmp = ['rm', path]
                    client(tmp)
                    # and reupload
                    change = "add"

            # push tasks to client
            if change == "get":
                tmp = ['get', path, localPath]
                client(tmp)
            elif change == "add":
                tmp = ['put', localPath, path, change]
                client(tmp)

    print(" > Command '" + parameters[0] + "' executed")
예제 #5
0
def client(parameters):
	cmd = parameters[0]
	
	if cmd == "ls":
		path = parameters[1]
		to_file = parameters[2]
			
		resp = api_client.metadata(path)
		file = open(to_file,"w")
		
		if 'contents' in resp:
			for f in resp['contents']:
				name = os.path.basename(f['path'])
				encoding = locale.getdefaultlocale()[1]
				file.write(('%s\n' % name).encode(encoding))
		file.close()
		
	if cmd == "share":
		param = parameters[1]
		f = api_client.share(param)
		url = str(f['url'])
		print(" > Generated link: " + url)
		
	elif cmd == "delta":
		state = load_state()
		cursor = state.get('cursor')
		tree = state['tree']
		page = 0
		changed = False
		page_limit = 15
		
		try:
			delta_switch = parameter[1]
		except:
			delta_switch = 0
		
		while (page_limit is None) or (page < page_limit):
			# Make an int for progress/total
			progress = 0
			total = 0
			# Get /delta results from Dropbox
			result = api_client.delta(cursor)
			page += 1
			if result['reset'] == True:
				sys.stdout.write('reset\n')
				changed = True
				tree = {}
			cursor = result['cursor']
			# Apply the entries one by one to our cached tree.
			for delta_entry in result['entries']:
				total = total+1
			for delta_entry in result['entries']:
				changed = True
				progress = progress +1
				print("Current entry: "+str(progress)+"/"+str(total))
				apply_delta(tree, delta_entry)
				cursor = result['cursor']
				if not result['has_more']: break

		if not changed:
			sys.stdout.write('No updates.\n')
		else:
		# Save state
			state['cursor'] = cursor
			state['tree'] = tree
			save_state(state)
		
	elif cmd == "put":
		from_path = parameters[1]
		to_path = parameters[2]
		notify = parameters[3] # it can be 'add' or 'upd'
		
		from_file = open(os.path.expanduser(from_path), 'rb')
		#try:
		api_client.put_file("/" + to_path, from_file)
		#except:
		#	print(" x Unable to upload file. ")
		common.orphiliaNotify(notify,from_path)
		
	elif cmd == "unlink":
			sess.unlink()
			print(" > Unlinked :C")

	elif cmd == "cat":
		f = api_client.get_file("/" + path)
		stdout.write(f.read())
		stdout.write("\n")

	elif cmd == "mkdir":
		path = parameters[1]
		try:
			api_client.file_create_folder("/" + path)
		except:
			print(" x Unable to make directory " + path)
		print(" > Directory \'" + path + "\' created")
	
	elif cmd == "rm":
		path = path_rewrite.rewritepath('posix',parameters[1])
		try:
			api_client.file_delete("/" + path)
			common.orphiliaNotify('rm',path)
		except:
			print(" x Unable to remove file " + path)

	elif cmd == "mv":
		from_path = parameters[1]
		to_path = parameters[2]
		try:
			api_client.file_move("/" + from_path, "/" + to_path)
		except:
			print(" x Unable to move file " + from_path + " to " + to_path)
	
	elif cmd == "account_info":
		f = api_client.account_info()
		pprint.PrettyPrinter(indent=2).pprint(f)
		
	elif cmd == "uid":
		param = parameters[1]
		f = api_client.account_info()
		uid = str(f['uid'])
		try:
			common.putIn(uid,param,'rewrite')
		except:
			print(" x Unable to save file.")
		print(" > UID updated")
		
	elif cmd == "get":
		from_path = parameters[1]
		to_path = parameters[2]
		
		resp = api_client.metadata(from_path)
		modified = resp['modified']
		
		try:
			open(os.path.expanduser(to_path), 'rb')
		except:
			pass
			
		date1 = time.mktime(datetime.datetime.strptime(modified, "%a, %d %b %Y %H:%M:%S +0000").timetuple())
		f = api_client.get_file("/" + from_path)
		file = open(to_path,"w+")
		try:
			file.write(f.read())
		except:
			print(" x Unable to save file.")
		file.close()
		os.utime(os.path.normpath(to_path),(date1,date1))

	elif cmd == "sync":
		path = parameters[1]
		localPath = os.path.normpath(dropboxPath + '/' + path)
		change = 'upd'
		
		try:
			resp = api_client.metadata(path)
			modified = resp['client_mtime']
		except:
			change = 'add'
		
		try:
			localTime = os.path.getmtime(localPath)
		except:
			localTime = 0
		
		if (change != 'add'):
			dropboxTime = time.mktime(datetime.datetime.strptime(modified, "%a, %d %b %Y %H:%M:%S +0000").timetuple())
		
		if ((change != 'add') and (localTime < dropboxTime)):
			tmp = ['get',path,localPath]
			client(tmp)
		elif ((change != 'add') and (localTime == dropboxTime)):
			print(" > No need to update. localTime: " + str(localTime) + " = dropboxTime: " + str(dropboxTime))
		else:
			if (change != 'add'):
				tmp = ['rm', path]
				client(tmp)
			tmp = ['put',localPath,path,change]
			client(tmp)
		
	print(" > Command '" + parameters[0] + "' executed")
예제 #6
0
def apply_delta(root, e):
    queue = Queue.Queue(0)
    path, metadata = e
    branch, leaf = split_path(path)

    if metadata is not None:
        print(' + ' + path.encode("utf-8"))
        # Traverse down the tree until we find the parent folder of the entry
        # we want to add. Create any missing folders along the way.
        children = root
        for part in branch:
            node = get_or_create_child(children, part)
            # If there's no folder here, make an empty one.
            if not node.is_folder():
                node.content = {}
            children = node.content

        # Create the file/folder.
        node = get_or_create_child(children, leaf)
        node.path = metadata['path']  # Save the un-lower-cased path.
        if metadata['is_dir']:
            # Only create an empty folder if there isn't one there already.
            if not node.is_folder():
                node.content = {}
            if delta_switch == 0:
                try:
                    os.mkdir(dropboxPath + "/" + metadata['path'])
                except:
                    pass
        else:
            node.content = metadata['size'], metadata['modified']
            tmp = ['get', node.path, dropboxPath + "/" + node.path]
            if delta_switch == 0:
                try:
                    queue.put(client(tmp))
                except:
                    print(" x Something went wrong. Unable to get file.")
    else:
        print(' - ' + path.encode("utf-8"))
        if delta_switch == 0:
            try:
                queue.put(os.remove(dropboxPath + '/' + path))
            except:
                print(' x Something went wrong. Unable to remove element.')

        # Traverse down the tree until we find the parent of the entry we
        # want to delete.
        children = root
        for part in branch:
            node = children.get(part)
            # If one of the parent folders is missing, then we're done.
            if node is None or not node.is_folder():
                try:
                    queue.put(shutil.rmtree(dropboxPath + path))
                except:
                    print(' x Something went wrong. Unable to remove path')
                break
            children = node.content
        else:
            # If we made it all the way, delete the file/folder (if it exists).
            if leaf in children:
                del children[leaf]
예제 #7
0
def apply_delta(root, e):
	queue = Queue.Queue(0)
	path, metadata = e
	branch, leaf = split_path(path)

	if metadata is not None:
		print(' + ' + path.encode("utf-8"))
		# Traverse down the tree until we find the parent folder of the entry
		# we want to add. Create any missing folders along the way.
		children = root
		for part in branch:
			node = get_or_create_child(children, part)
			# If there's no folder here, make an empty one.
			if not node.is_folder():
				node.content = {}
			children = node.content

		# Create the file/folder.
		node = get_or_create_child(children, leaf)
		node.path = metadata['path']  # Save the un-lower-cased path.
		if metadata['is_dir']:
			# Only create an empty folder if there isn't one there already.
			if not node.is_folder():
				node.content = {}
			if delta_switch == 0:
				try:
					os.mkdir(dropboxPath + "/" + metadata['path'])
				except:
					pass
		else:
			node.content = metadata['size'], metadata['modified']
			tmp = [ 'get', node.path, dropboxPath + "/" + node.path]
			if delta_switch == 0:
				try:
					queue.put(client(tmp))
				except:
					print(" x Something went wrong. Unable to get file.")
	else:
		print(' - ' + path.encode("utf-8"))
		if delta_switch == 0:
			try:
				queue.put(os.remove(dropboxPath + '/' + path))
			except:
				print(' x Something went wrong. Unable to remove element.')
	
		# Traverse down the tree until we find the parent of the entry we
		# want to delete.
		children = root
		for part in branch:
			node = children.get(part)
			# If one of the parent folders is missing, then we're done.
			if node is None or not node.is_folder(): 
				try:
					queue.put(shutil.rmtree(dropboxPath+path)) 
				except:
					print(' x Something went wrong. Unable to remove path')
				break
			children = node.content
		else:
			# If we made it all the way, delete the file/folder (if it exists).
			if leaf in children:
				del children[leaf]
예제 #8
0
def client(parameters):
    cmd = parameters[0]

    if cmd == "ls":
        path = parameters[1]
        to_file = parameters[2]

        resp = api_client.metadata(path)
        file = open(to_file, "w")

        if 'contents' in resp:
            for f in resp['contents']:
                name = os.path.basename(f['path'])
                encoding = locale.getdefaultlocale()[1]
                file.write(('%s\n' % name).encode(encoding))
        file.close()

    if cmd == "share":
        param = parameters[1]
        f = api_client.share(param)
        url = str(f['url'])
        print(" > Generated link: " + url)

    elif cmd == "delta":
        state = load_state()
        cursor = state.get('cursor')
        tree = state['tree']
        page = 0
        changed = False
        page_limit = 15

        try:
            delta_switch = parameter[1]
        except:
            delta_switch = 0

        while (page_limit is None) or (page < page_limit):
            # Make an int for progress/total
            progress = 0
            total = 0
            # Get /delta results from Dropbox
            result = api_client.delta(cursor)
            page += 1
            if result['reset'] == True:
                sys.stdout.write('reset\n')
                changed = True
                tree = {}
            cursor = result['cursor']
            # Apply the entries one by one to our cached tree.
            for delta_entry in result['entries']:
                total = total + 1
            for delta_entry in result['entries']:
                changed = True
                progress = progress + 1
                print("Current entry: " + str(progress) + "/" + str(total))
                apply_delta(tree, delta_entry)
                cursor = result['cursor']
                #if not result['has_more']: break

        if not changed:
            sys.stdout.write('No updates.\n')
        else:
            # Save state
            state['cursor'] = cursor
            state['tree'] = tree
            save_state(state)

    elif cmd == "put":
        from_path = parameters[1]
        to_path = parameters[2]
        notify = parameters[3]  # it can be 'add' or 'upd'

        from_file = open(os.path.expanduser(from_path), 'rb')
        # try:
        api_client.put_file("/" + to_path, from_file)
        # except:
        #	print(" x Unable to upload file. ")
        common.orphiliaNotify(notify, from_path)

    elif cmd == "unlink":
        sess.unlink()
        print(" > Unlinked :C")

    elif cmd == "cat":
        f = api_client.get_file("/" + path)
        stdout.write(f.read())
        stdout.write("\n")

    elif cmd == "mkdir":
        path = parameters[1]
        try:
            api_client.file_create_folder("/" + path)
        except:
            print(" x Unable to make directory " + path)
        print(" > Directory \'" + path + "\' created")

    elif cmd == "rm":
        path = path_rewrite.rewritepath('posix', parameters[1])
        try:
            api_client.file_delete("/" + path)
            common.orphiliaNotify('rm', path)
        except:
            print(" x Unable to remove file " + path)

    elif cmd == "mv":
        from_path = parameters[1]
        to_path = parameters[2]
        try:
            api_client.file_move("/" + from_path, "/" + to_path)
        except:
            print(" x Unable to move file " + from_path + " to " + to_path)

    elif cmd == "account_info":
        f = api_client.account_info()
        pprint.PrettyPrinter(indent=2).pprint(f)

    elif cmd == "uid":
        f = api_client.account_info()
        uid = str(f['uid'])
        return uid

    elif cmd == "get":
        from_path = parameters[1]
        to_path = parameters[2]

        resp = api_client.metadata(from_path)
        modified = resp['client_mtime']

        try:
            open(os.path.expanduser(to_path), 'rb')
        except:
            pass

        date1 = time.mktime(parse(modified).timetuple())
        f = api_client.get_file("/" + from_path)
        dirname = os.path.dirname(os.path.abspath(to_path))

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        file = open(to_path, "wb")
        try:
            file.write(f.read())
            file.close()
            os.utime(os.path.normpath(to_path), (date1, date1))
        except:
            print(" x Unable to save file.")

    elif cmd == "sync":
        """
        Command which syncs files.
            - if both file exists, compare the dates:
                - local is older? download from server
                - local is newer? remove from server and upload local
            - if only local exists, upload
            - if exists only on the server, download
        """
        # get path that was sent to client
        path = parameters[1]
        localPath = os.path.normpath(dropboxPath + '/' + path)
        # assume that both files actually exist
        change = 'upd'

        # check if file exists on the server, try to get response data
        try:
            resp = api_client.metadata(path)
            modified = resp['client_mtime']
            dropboxTime = parse(modified)
        except:
            # ok, it doesn't, so we're going to upload that file
            dropboxTime = 0
            change = 'add'

        # check if local file exists and try to get it's modified date
        try:
            localTime = datetime.datetime.fromtimestamp(os.path.getmtime(localPath), tz=tz.tzutc())
        except:
            change = 'get'
            localTime = 0

        # uhm, this was not supposed to happen...
        if dropboxTime == 0 and localTime == 0:
            print(" x WTF?! It looks like there is no file on the server nor locally...")
            exit()
        else:
            if change == "upd":
                # both file exists, decide what's next
                if localTime < dropboxTime:
                    # local file is older, download from server
                    change = "get"
                else:
                    # local file is newer, remove from server
                    tmp = ['rm', path]
                    client(tmp)
                    # and reupload
                    change = "add"

            # push tasks to client
            if change == "get":
                tmp = ['get', path, localPath]
                client(tmp)
            elif change == "add":
                tmp = ['put', localPath, path, change]
                client(tmp)

    print(" > Command '" + parameters[0] + "' executed")