Esempio n. 1
0
        def on_created(self, event):
            super(LoggingEventHandler, self).on_created(event)

            if os.name <> "nt":
                what = 'directory' if event.is_directory else 'file'
                if what == 'file':
                    par = event.src_path
                    while True:
                        size1 = os.path.getsize(par)
                        time.sleep(0.5)
                        size2 = os.path.getsize(par)
                        if size1 == size2:
                            break
                    path = par[len(dropboxPath) + 1:]

                    tmp = ['put', dropboxPath + '/' + path, path, 'add']
                    queue.put(orphiliaclient.client.client(tmp))
                else:
                    par = event.src_path
                    path = par[len(dropboxPath) + 1:]
                    if os.name == "nt":
                        path = path_rewrite.rewritepath('posix', path)
                    tmp = ['mkdir', path]
                    queue.put(orphiliaclient.client.client(tmp))
                logging.info(" > Created %s: %s", what, event.src_path)
            else:
                what = 'directory' if event.is_directory else 'file'
                if what == 'directory':
                    par = event.src_path
                    path = par[len(dropboxPath) + 1:]
                    if os.name == "nt":
                        path = path_rewrite.rewritepath('posix', path)
                    tmp = ['mkdir', path]
                    queue.put(orphiliaclient.client.client(tmp))
                    logging.info(" > Created %s: %s", what, event.src_path)
Esempio n. 2
0
 def on_created(self, event):
     super(LoggingEventHandler, self).on_created(event)
     if os.name <> "nt":
         what = "directory" if event.is_directory else "file"
         if what == "file":
             par = event.src_path
             while True:
                 size1 = os.path.getsize(par)
                 time.sleep(0.5)
                 size2 = os.path.getsize(par)
                 if size1 == size2:
                     break
             path = par[len(droppath) + 1 :]
             tmp = ["put", droppath + path, path2, "add"]
             queue.put(orphilia.client.client_new(tmp))
         else:
             par = event.src_path
             path = par[len(droppath) + 1 :]
             if os.name == "nt":
                 path = path_rewrite.rewritepath("posix", path)
             tmp = ["mkdir", path]
             queue.put(orphilia.client.client_new(tmp))
         logging.info("Created %s: %s", what, event.src_path)
     else:
         what = "directory" if event.is_directory else "file"
         if what == "directory":
             par = event.src_path
             path = par[len(droppath) + 1 :]
             if os.name == "nt":
                 path = path_rewrite.rewritepath("posix", path)
             tmp = ["mkdir", path]
             queue.put(orphilia.client.client_new(tmp))
             logging.info("Created %s: %s", what, event.src_path)
Esempio n. 3
0
		def on_created(self, event):
			super(LoggingEventHandler, self).on_created(event)
			
			if os.name <> "nt":
				what = 'directory' if event.is_directory else 'file'
				if what == 'file':
						par = event.src_path
						while True:
							size1 = os.path.getsize(par)
							time.sleep(0.5)
							size2 = os.path.getsize(par)
							if size1 == size2:
								break
						path = par[len(dropboxPath)+1:]
						
						tmp = [ 'put', dropboxPath + '/' + path, path, 'add' ]
						queue.put(orphiliaclient.client.client(tmp))
				else:
						par = event.src_path
						path = par[len(dropboxPath)+1:]
						if os.name == "nt":
							path = path_rewrite.rewritepath('posix',path)
						tmp = [ 'mkdir', path ]
						queue.put(orphiliaclient.client.client(tmp))
				logging.info(" > Created %s: %s", what, event.src_path)
			else:
				what = 'directory' if event.is_directory else 'file'
				if what == 'directory':
					par = event.src_path
					path = par[len(dropboxPath)+1:]
					if os.name == "nt":
						path = path_rewrite.rewritepath('posix',path)
					tmp = [ 'mkdir', path ]
					queue.put(orphiliaclient.client.client(tmp))
					logging.info(" > Created %s: %s", what, event.src_path)
Esempio n. 4
0
 def on_deleted(self, event):
     super(LoggingEventHandler, self).on_deleted(event)
     par = event.src_path
     path = par[len(droppath) + 1 :]
     if os.name == "nt":
         path = path_rewrite.rewritepath("posix", path)
     what = "directory" if event.is_directory else "file"
     tmp = ["rm", path]
     queue.put(orphilia.client.client_new(tmp))
     logging.info("Deleted %s: %s", what, event.src_path)
Esempio n. 5
0
def public(parameters):
	read_details = open(os.path.normpath(configurationdir+'/dropbox-path'), 'r')
	DROPPATH = read_details.read()
	read_details.close()
	read_details2 = open(os.path.normpath(configurationdir+'/dropbox-id'), 'r')
	DROPID = read_details2.read()
	read_details2.close()
	par = parameters[1]
	link = 'http://dl.dropbox.com/u/' + DROPID + '/' + path_rewrite.rewritepath('url',par[len(os.path.normpath(DROPPATH + "/Public"))+1:])
	orphilia_notify('link',link)
Esempio n. 6
0
		def on_deleted(self, event):
			super(LoggingEventHandler, self).on_deleted(event)
			par = event.src_path
			path = par[len(dropboxPath)+1:]
			if os.name == "nt":
				path = path_rewrite.rewritepath('posix',path)
			what = 'directory' if event.is_directory else 'file'
			tmp = [ 'rm', path ]
			queue.put(orphiliaclient.client.client(tmp))
			logging.info(" > Deleted %s: %s", what, event.src_path)
Esempio n. 7
0
 def on_deleted(self, event):
     super(LoggingEventHandler, self).on_deleted(event)
     par = event.src_path
     path = par[len(dropboxPath) + 1:]
     if os.name == "nt":
         path = path_rewrite.rewritepath('posix', path)
     what = 'directory' if event.is_directory else 'file'
     tmp = ['rm', path]
     queue.put(orphiliaclient.client.client(tmp))
     logging.info(" > Deleted %s: %s", what, event.src_path)
Esempio n. 8
0
        def on_moved(self, event):
            super(LoggingEventHandler, self).on_moved(event)
            par = event.src_path
            path = par[len(droppath) + 1 :]
            par2 = event.dest_path
            path2 = par2[len(droppath) + 1 :]

            if os.name == "nt":
                path = path_rewrite.rewritepath("posix", path)
                path2 = path_rewrite.rewritepath("posix", path2)

            what = "directory" if event.is_directory else "file"
            if what == "file":
                tmp = ["mv", path, path2]
                queue.put(orphilia.client.client_new(tmp))
            else:
                tmp = ["mkdir", path2]
                queue.put(orphilia.client.client_new(tmp))
                tmp = ["rm", path]
                queue.put(orphilia.client.client_new(tmp))
            logging.info("Moved %s: from %s to %s", what, event.src_path, event.dest_path)
Esempio n. 9
0
		def on_moved(self, event):
			super(LoggingEventHandler, self).on_moved(event)
			par = event.src_path
			path = par[len(dropboxPath)+1:]
			par2 = event.dest_path
			path2 = par2[len(dropboxPath)+1:]
			
			if os.name == "nt":
				path = path_rewrite.rewritepath('posix',path)
				path2 = path_rewrite.rewritepath('posix',path2)

			what = 'directory' if event.is_directory else 'file'
			if what == "file":
				tmp = [ 'mv', path, path2]
				queue.put(orphiliaclient.client.client(tmp))
			else:
				tmp = [ 'mkdir', path2 ]
				queue.put(orphiliaclient.client.client(tmp))
				tmp = [ 'rm', path ]
				queue.put(orphiliaclient.client.client(tmp))
			logging.info(" > Moved %s: from %s to %s", what, event.src_path, event.dest_path)
Esempio n. 10
0
		def on_moved(self, event):
			super(LoggingEventHandler, self).on_moved(event)
			par = event.src_path
			path = par[len(dropboxPath)+1:]
			par2 = event.dest_path
			path2 = par2[len(dropboxPath)+1:]
			
			if os.name == "nt":
				path = path_rewrite.rewritepath('posix',path)
				path2 = path_rewrite.rewritepath('posix',path2)

			what = 'directory' if event.is_directory else 'file'
			if what == "file":
				tmp = [ 'mv', path, path2]
				queue.put(orphiliaclient.client.client(tmp))
			else:
				tmp = [ 'mkdir', path2 ]
				queue.put(orphiliaclient.client.client(tmp))
				tmp = [ 'rm', path ]
				queue.put(orphiliaclient.client.client(tmp))
			logging.info(" > Moved %s: from %s to %s", what, event.src_path, event.dest_path)
Esempio n. 11
0
        def on_modified(self, event):
            super(LoggingEventHandler, self).on_modified(event)

            what = 'directory' if event.is_directory else 'file'
            if what == "file":
                par = event.src_path
                path = par[len(dropboxPath) + 1:]
                if os.name == "nt":
                    path = path_rewrite.rewritepath('posix', path)
                if os.name <> "nt":
                    tmp = ['rm', path]
                    queue.put(orphiliaclient.client.client(tmp))
                while True:
                    size1 = os.path.getsize(par)
                    time.sleep(0.2)
                    size2 = os.path.getsize(par)
                    if size1 == size2:
                        break
                tmp = ['put', dropboxPath + '/' + path, path, 'upd']
                queue.put(orphiliaclient.client.client(tmp))
            logging.info(" > Modified %s: %s", what, event.src_path)
Esempio n. 12
0
		def on_modified(self, event):
			super(LoggingEventHandler, self).on_modified(event)

			what = 'directory' if event.is_directory else 'file'
			if what == "file":
				par = event.src_path
				path = par[len(dropboxPath)+1:]
				if os.name == "nt":
					path = path_rewrite.rewritepath('posix',path)
				if os.name <> "nt":
					tmp = [ 'rm', path ]
					queue.put(orphiliaclient.client.client(tmp))
				while True:
					size1 = os.path.getsize(par)
					time.sleep(0.2)
					size2 = os.path.getsize(par)
					if size1 == size2:
						break
				tmp = [ 'put', dropboxPath + '/' + path, path, 'upd']
				queue.put(orphiliaclient.client.client(tmp))
			logging.info(" > Modified %s: %s", what, event.src_path)
Esempio n. 13
0
        def on_modified(self, event):
            super(LoggingEventHandler, self).on_modified(event)

            what = "directory" if event.is_directory else "file"
            if what == "file":
                par = event.src_path
                path = par[len(droppath) + 1 :]
                if os.name == "nt":
                    path = path_rewrite.rewritepath("posix", path)
                if os.name <> "nt":
                    tmp = ["rm", path]
                    queue.put(orphilia.client.client_new(tmp))
                while True:
                    size1 = os.path.getsize(par)
                    time.sleep(0.2)
                    size2 = os.path.getsize(par)
                    if size1 == size2:
                        break
                tmp = ["put", droppath + "/" + path, path, "upd"]
                queue.put(orphilia.client.client_new(tmp))
            logging.info("Modified %s: %s", what, event.src_path)
Esempio n. 14
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")
Esempio n. 15
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()

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

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

        while (page_limit is None) or (page < page_limit):
            # 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']:
                changed = True
                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]

        try:
            open(os.path.expanduser(to_path), 'rb')
        except:
            resp = api_client.metadata(from_path)
            modified = resp['modified']
            date1 = modified[5:]
            date1 = date_rewrite.generate_modifytime(date1)
            f = api_client.get_file("/" + from_path)
            file = open(to_path, "wb")
            try:
                file.write(f.read())
            except:
                print(" x Unable to save file.")
            file.close()
            if sys.platform[:5] != "win32":
                os.system("touch -d \"" + date1 + "\" \"" + to_path +
                          "\"")  # this solution won't work on Windows

    elif cmd == "sync_folder":
        path = parameters[1]

        resp = api_client.metadata(
            path)  # gets list of files in directory on Dropbox
        dirlist = os.listdir(
            dropboxPath + "/" +
            path)  # gets list of files in directory on local computer
        rand1 = random.random()  # generates random integer

        queue = Queue.Queue(0)

        if 'contents' in resp:  # begin comparing both lists
            for f in resp['contents']:
                name = os.path.basename(f['path'])
                encoding = locale.getdefaultlocale()[1]
                if ('%s' % name).encode(
                        encoding
                ) not in dirlist:  # found Dropbox file, which isn't present on local computer
                    print('%s' % name).encode(encoding) + " not found."
                    if not os.path.isfile(('%s' % name).encode(encoding)):
                        dir = f['is_dir']
                        if not dir:
                            tmp = [
                                'get', path + "/" + name,
                                dropboxPath + "/" + path + name
                            ]
                            queue.put(client_new(tmp))
                        if dir:
                            os.mkdir(dropboxPath + "/" + path +
                                     ('%s' % name).encode(encoding))
                else:  # found Dropbox file which is present on local computer, check this out, bro!
                    name = os.path.basename(f['path'])
                    encoding = locale.getdefaultlocale()[1]
                    print(('%s' % name).encode(encoding) +
                          " found. Checking...")

    print(" > Command '" + parameters[0] + "' executed")
Esempio n. 16
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()
		
	elif cmd == "delta":
		state = load_state()
		cursor = state.get('cursor')
		tree = state['tree']
		page = 0
		changed = False
		page_limit = 5
		
		try:
			delta_switch = parameter[1]
		except:
			delta_switch = 0
		
		while (page_limit is None) or (page < page_limit):
			# 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']:
				changed = True
				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]
		
		try:
			open(os.path.expanduser(to_path), 'rb')
		except:
			resp = api_client.metadata(from_path)
			modified = resp['modified']
			date1 = modified[5:]
			date1 = date_rewrite.generate_modifytime(date1)
			f = api_client.get_file("/" + from_path)
			file = open(to_path,"wb")
			try:
				file.write(f.read())
			except:
				print(" x Unable to save file.")
			file.close()
			if sys.platform[:5] != "win32":
				os.system("touch -d \"" + date1 + "\" \"" + to_path + "\"") # this solution won't work on Windows
		
	elif cmd == "sync_folder":
		path = parameters[1]
		
		resp = api_client.metadata(path) # gets list of files in directory on Dropbox
		dirlist = os.listdir(dropboxPath + "/" + path) # gets list of files in directory on local computer
		rand1 = random.random() # generates random integer
		
		queue = Queue.Queue(0)
	
		if 'contents' in resp: # begin comparing both lists
			for f in resp['contents']:
				name = os.path.basename(f['path'])
				encoding = locale.getdefaultlocale()[1]
				if ('%s' % name).encode(encoding) not in dirlist: # found Dropbox file, which isn't present on local computer
					print ('%s' % name).encode(encoding) + " not found."
					if not os.path.isfile(('%s' % name).encode(encoding)):
						dir = f['is_dir']
						if not dir:
							tmp = [ 'get', path + "/" + name, dropboxPath + "/" + path + name]
							queue.put(client_new(tmp))
						if dir:
							os.mkdir(dropboxPath + "/" + path +  ('%s' % name).encode(encoding))
				else: # found Dropbox file which is present on local computer, check this out, bro!
					name = os.path.basename(f['path'])
					encoding = locale.getdefaultlocale()[1]
					print(('%s' % name).encode(encoding) + " found. Checking...")

	print(" > Command '" + parameters[0] + "' executed")
Esempio n. 17
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")
Esempio n. 18
0
def getPublicLink(parameters):
	par = parameters[0]
	link = 'https://dl.dropboxusercontent.com/u/' + accountUID + '/' + path_rewrite.rewritepath('url',par[len(os.path.normpath(dropboxPath + "/Public"))+1:])
	common.orphiliaNotify('link',link)
	print(link)
Esempio n. 19
0
def getPublicLink(parameters):
    par = parameters[0]
    link = 'https://dl.dropboxusercontent.com/u/' + accountUID + '/' + path_rewrite.rewritepath(
        'url', par[len(os.path.normpath(dropboxPath + "/Public")) + 1:])
    common.orphiliaNotify('link', link)
    print(link)
Esempio n. 20
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")