Пример #1
0
class DropBoxStorage(Storage):
    """DropBox Storage class for Django pluggable storage system."""
    def generate_url(self, name):
        url = self.client.share(name, name)
        url = url['url']
        f = request.urlopen(url)
        path = f.geturl()
        return re.sub('www.dropbox.com','dl.dropboxusercontent.com', path)

    def url(self, name):
        return name #

    def __init__(self, oauth2_access_token=setting('DROPBOX_OAUTH2_TOKEN')):
        if oauth2_access_token is None:
            raise ImproperlyConfigured("You must configure a token auth at"
                                       "'settings.DROPBOX_OAUTH2_TOKEN'.")
        self.client = DropboxClient(oauth2_access_token)

    def delete(self, name):
        self.client.file_delete(name)

    def exists(self, name):
        response = self.client.search('/', name, file_limit=1)
        return bool(response)

    def listdir(self, path):
        directories, files = [], []
        metadata = self.client.metadata(path)
        for entry in metadata['contents']:
            if entry['is_dir']:
                directories.append(entry['path'])
            else:
                files.append(entry['path'])
        return directories, files

    def size(self, name):
        metadata = self.client.metadata(name)
        return metadata['bytes']

    def modified_time(self, name):
        metadata = self.client.metadata(name)
        mod_time = datetime.strptime(metadata['modified'], DATE_FORMAT)
        return mod_time

    def accessed_time(self, name):
        metadata = self.client.metadata(name)
        acc_time = datetime.strptime(metadata['client_mtime'], DATE_FORMAT)
        return acc_time

    def _open(self, name, mode='rb'):
        remote_file = DropBoxFile(name, self)
        return remote_file

    def _save(self, name, content):
        self.client.put_file(name, content)
        return name

    def _read(self, name, num_bytes=None):
        data = self.client.get_file(name)
        return data.read(num_bytes)
Пример #2
0
class DropboxStorage(Storage):

    def __init__(self):
        flow = DropboxOAuth2FlowNoRedirect(settings.APP_KEY,
                                           settings.APP_SECRET)

        #authorize_url = flow.start()
        #access_token, user_id = flow.finish(settings.ACCESS_TOKEN)

        self.client = DropboxClient(settings.ACCESS_TOKEN)

    def _open(self, name, mode='rb'):
        return File(name, mode=mode)

    def _save(self, name, content):
        response = self.client.put_file(name, content, overwrite=True)
        return response['path']

    def delete(self, name):
        self.client.file_delete(name)

    def exists(self, name):
        try:
            self.client.metadata(name)
        except ErrorResponse as e:
            if e.status == 404:
                return False
            raise e
        return True

    def size(self, name):
        return self.client.metadata(name)['bytes']

    def url(self, name):
        return self.client.metadata(name)
Пример #3
0
class DropBoxStorage(Storage):
    """DropBox Storage class for Django pluggable storage system."""

    def __init__(self, oauth2_access_token=setting('DROPBOX_OAUTH2_TOKEN')):
        if oauth2_access_token is None:
            raise ImproperlyConfigured("You must configure a token auth at"
                                       "'settings.DROPBOX_OAUTH2_TOKEN'.")
        self.client = DropboxClient(oauth2_access_token)

    def delete(self, name):
        self.client.file_delete(name)

    def exists(self, name):
        try:
            return bool(self.client.metadata(name))
        except ErrorResponse:
            return False

    def listdir(self, path):
        directories, files = [], []
        metadata = self.client.metadata(path)
        for entry in metadata['contents']:
            if entry['is_dir']:
                directories.append(entry['path'])
            else:
                files.append(entry['path'])
        return directories, files

    def size(self, name):
        metadata = self.client.metadata(name)
        return metadata['bytes']

    def modified_time(self, name):
        metadata = self.client.metadata(name)
        mod_time = datetime.strptime(metadata['modified'], DATE_FORMAT)
        return mod_time

    def accessed_time(self, name):
        metadata = self.client.metadata(name)
        acc_time = datetime.strptime(metadata['client_mtime'], DATE_FORMAT)
        return acc_time

    def url(self, name):
        media = self.client.media(name)
        return media['url']

    def _open(self, name, mode='rb'):
        remote_file = DropBoxFile(name, self)
        return remote_file

    def _save(self, name, content):
        self.client.put_file(name, content)
        return name

    def _read(self, name, num_bytes=None):
        data = self.client.get_file(name)
        return data.read(num_bytes)
Пример #4
0
 def get(self):
     client = DropboxClient(self.get_secure_cookie("access_token"))
     try:
         client.file_delete('/blog.db')
         client.file_delete('/settings.py')
     except:
         print("Can't find any backup")
     finally:
         with open(os.path.join(os.path.abspath(os.path.dirname("__file__")), 'blog.db'), 'rb') as f:
             response = client.put_file('/blog.db', f)
         with open(os.path.join(os.path.abspath(os.path.dirname("__file__")), 'settings.py'), 'rb') as f:
             response = client.put_file('/settings.py', f)
         self.redirect("/admin/dropbox?message=Backup successfully")
Пример #5
0
 def _rotate(self, client: DropboxClient, files: list):
     """  Rotate Dropbox in order to save storage.
         """
     total_size = sum(item['size'] for item in files)
     files_history = sorted(files, key=itemgetter('modified'))
     for file in files_history:
         if total_size < self.max_size:
             break
         try:
             client.file_delete(file['file'])
             print("{} was deleted from Dropbox.".format(file['file']))
             total_size -= file['size']
         except (MaxRetryError, ErrorResponse):
             pass
Пример #6
0
 def get(self):
     client = DropboxClient(self.get_secure_cookie("access_token"))
     try:
         client.file_delete('/blog.db')
         client.file_delete('/settings.py')
     except:
         print("Can't find any backup")
     finally:
         with open(
                 os.path.join(os.path.abspath(os.path.dirname("__file__")),
                              'blog.db'), 'rb') as f:
             response = client.put_file('/blog.db', f)
         with open(
                 os.path.join(os.path.abspath(os.path.dirname("__file__")),
                              'settings.py'), 'rb') as f:
             response = client.put_file('/settings.py', f)
         self.redirect("/admin/dropbox?message=Backup successfully")
Пример #7
0
 def post(self):
     session = SessionData.get(self.request.get('session_key'))
     conference_data = ConferenceData.get(self.request.get('conf_key'))
     db_path = self.request.get('db_path')
     try:
         client = DropboxClient(conference_data.dbox_access_token, "en_US", rest_client=None)
         logging.info('DB client created %s' % client)
     except:
         logging.info("DB Client was not created, access token is %s"% conference_data.dbox_access_token)
         return None
     try:
         client.file_delete(session.dbox_path)
         logging.info('File %s was deleted' % session.dbox_path)
     except:
         logging.error('File %s not deleted'% session.dbox_path)
         return
     session.dbox_path = None
     data_cache.set('%s-sessions'% session.module, None)
     return
class DropboxSyncClient:
    def __init__(self, oauth2_access_token):
        self.access_token = oauth2_access_token
        self._client = DropboxClient(oauth2_access_token) 

    def upload_file(self, dropbox_file_path, local_file_path, replace=False):
        f = open(local_file_path, 'rb')
        response = self._client.put_file(dropbox_file_path, f, replace)
        return 1, response['path']

    def generate_public_url(self, dropbox_file_path):
        return self._client.share(dropbox_file_path)['url']

    def delete_file(self, dropbox_file_path):
        self._client.file_delete(dropbox_file_path)
        return 1, None

    def update_local_to_cloud(self, dropbox_file_path, local_file_path):
        return 1, self.upload_file(dropbox_file_path, local_file_path, replace=True)

    def update_cloud_to_local(self, dropbox_file_path, local_file_path):
        try:
            try:
                os.makedirs(os.path.dirname(local_file_path))
            except Exception as e:
                pass

            open(local_file_path, 'wb').write(self._client.get_file(dropbox_file_path).read())
            return 1, None
        except Exception as e:
            print e
            return 1, None

    def get_file_list(self, dropbox_folder_path):
        folder_metadata = self._client.metadata(dropbox_folder_path)
        return [content['path'] for content in folder_metadata['contents']]

    def set_access_token(self, access_token):
        self._client = DropboxClient(access_token)

    def get_remaining_space(self):
        quota_info = self._client.account_info()['quota_info']
        return quota_info['total'] - (quota_info['shared'] + quota_info['normal'])
Пример #9
0
def oauth_callback():
    '''Callback function for when the user returns from OAuth.'''

    access_token, uid, extras = get_flow().finish(request.args)
    session['uid'] = uid
 
    # Extract and store the access token for this user
    redis_client.hset('tokens', uid, access_token)

    # Create folder tree, put random easter eggs in it.

    # OAuth token for the user
    token = redis_client.hget('tokens', uid)

    client = DropboxClient(token)

    # Set up the Easter hunt area!
    try:
        client.file_delete('/Yard')
    except Exception, e:
        pass
Пример #10
0
class DropBox(clouddrive.CloudDrive):
    name = "DropBox"

    def __init__(self, access_token):
        self.client = DropboxClient(access_token)
        self.access_token = access_token
        clouddrive.CloudDrive.__init__(self, access_token)

    @staticmethod
    def auth():
        #app_key = 'knbyx2adg14kkn5'
        #app_secret = 'kh3ulgqry8jffqp'
        app_key = 'eif0l7bgnpb06di'
        app_secret = 'qa02jhdo4jrwaid'
        
        global Auth_DROPMytoken
        global Auth_Drop_Running_true

        def keep_running():
            global Auth_Drop_Running_true
            return Auth_Drop_Running_true

        class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler):
            def do_HEAD(s):
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()

            def do_GET(s):
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()
                if s.path.find("code=") != -1:
                    global Auth_DROPMytoken
                    global Auth_Drop_Running_true
                    Auth_DROPMytoken = s.path
                    Auth_Drop_Running_true = False
                    s.wfile.write("ok see command line")
                    return

        class MYrequest:

            def __init__(self, url):
                self.url = url

            def get(self, mystring):
                if self.url.find(mystring) == -1:
                    return
                item = self.url.split(mystring + "=")[1]
                if item.find("&") != -1:
                    item = item.split("&")[0]
                return urllib.unquote(item).decode()


        redirect_url = "http://localhost:8787"
        my_session = {}
        Auth_Drop_Running_true = True
        flow = DropboxOAuth2Flow(app_key, app_secret, redirect_url, my_session, "dropbox-auth-csrf-token")

        authorize_url = flow.start()
        webbrowser.open(authorize_url)

        try:
            httpd = BaseHTTPServer.HTTPServer(("", 8787), AuthHandler)
            while keep_running():
                httpd.handle_request()
        except KeyboardInterrupt:
            pass
        httpd.server_close()

        token, user_id, url_state = flow.finish(MYrequest(Auth_DROPMytoken))
        clouddrive.CloudDrive.access_token = token
        return token


    def read(self, filename):
        try:
            data=[]
            with self.client.get_file(filename) as f:
                data+=f.read()
        except:
            return -1
        return ''.join(data)

    def write(self, filename, data):
        try:
            response = self.client.put_file(filename, data,True)
        except:
            return -1
        return 1    

    def mkdir(self, dirname):
        try:
            self.client.file_create_folder(dirname)
        except:
            return -1
        return 1

    def delete(self, filename):
        try:
            self.client.file_delete(filename)
        except:
            return -1
        return 1

    def capacity(self):
        try:
            a=self.client.account_info()["quota_info"]
        except:
	        return -1
        return a["quota"]-a["shared"]-a["normal"]

    def listing (self, path="/") :

        lists=self.client.metadata(path)
        filelist=[]
        for x in lists["contents"] :
            filelist.append((x['path'],x['is_dir']))
        return filelist 
Пример #11
0
class DropboxStorage(Storage):
    """
    A storage class providing access to resources in a Dropbox Public folder.
    """

    def __init__(self, location='/Public'):
        session = DropboxSession(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TYPE, locale=None)
        session.set_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        self.client = DropboxClient(session)
        self.account_info = self.client.account_info()
        self.location = location
        self.base_url = 'http://dl.dropbox.com/u/{uid}/'.format(**self.account_info)
            
    def _client_metadata(self, path):
        """
        Get client metadata from cache based on the DROPBOX_CACHE setting.
        """
        if DROPBOX_CACHE and isinstance(DROPBOX_CACHE, int):
            cache_key = _cache_key(path)
            meta = cache.get(cache_key, None)
            if not meta:
                meta = self.client.metadata(path)
                cache.set(cache_key, meta, DROPBOX_CACHE)
            return meta
        return self.client.metadata(path)

    def _get_abs_path(self, name):
        return os.path.realpath(os.path.join(self.location, name))

    def _open(self, name, mode='rb'):
        name = self._get_abs_path(name)
        remote_file = DropboxFile(name, self, mode=mode)
        return remote_file

    def _save(self, name, content):
        name = self._get_abs_path(name)
        directory = os.path.dirname(name)
        if not self.exists(directory) and directory:
             self.client.file_create_folder(directory)
        response = self._client_metadata(directory)
        if not response['is_dir']:
             raise IOError("%s exists and is not a directory." % directory)
        abs_name = os.path.realpath(os.path.join(self.location, name))
        self.client.put_file(abs_name, content)
        _clear_cache(abs_name)
        return name

    def delete(self, name):
        name = self._get_abs_path(name)
        self.client.file_delete(name)
        _clear_cache(name)

    def exists(self, name):
        name = self._get_abs_path(name)
        try:
            metadata = self._client_metadata(name)
            if metadata.get('is_deleted'):
                return False
        except ErrorResponse as e:
            if e.status == 404: # not found
                return False
            raise e 
        return True

    def listdir(self, path):
        path = self._get_abs_path(path)
        response = self._client_metadata(path)
        directories = []
        files = []
        for entry in response.get('contents', []):
            if entry['is_dir']:
                directories.append(os.path.basename(entry['path']))
            else:
                files.append(os.path.basename(entry['path']))
        return directories, files

    def size(self, name):
        path = os.path.realpath(os.path.join(self.location, name))
        return self._client_metadata(path)['bytes']

    def url(self, name):
        if name.startswith(self.location):
            name = name[len(self.location) + 1:]
        if self.base_url is None:
            raise ValueError("This file is not accessible via a URL.")
        return urlparse.urljoin(self.base_url, filepath_to_uri(name))

    def get_available_name(self, name):
        """
        Returns a filename that's free on the target storage system, and
        available for new content to be written to.
        """
        name = self._get_abs_path(name)
        dir_name, file_name = os.path.split(name)
        file_root, file_ext = os.path.splitext(file_name)
        # If the filename already exists, add an underscore and a number (before
        # the file extension, if one exists) to the filename until the generated
        # filename doesn't exist.
        count = itertools.count(1)
        while self.exists(name):
            # file_ext includes the dot.
            name = os.path.join(dir_name, "%s_%s%s" % (file_root, count.next(), file_ext))

        return name
Пример #12
0
class DropboxAPI(StorageAPI, AppendOnlyLog):
    "dropbox@auth : dropbox.com account with auth info"

    def __init__(self):
        from params import AUTH_DIR
        authdir = AUTH_DIR
        self.auth_file = os.path.join(authdir, 'dropbox.auth')
        try:
            with open(self.auth_file, 'r') as file:
                ACCESS_TOKEN = file.readline().rstrip()
                USER_ID = file.readline().rstrip()
        except IOError:
            ACCESS_TOKEN, USER_ID = self._authorize()

        self.client = DropboxClient(ACCESS_TOKEN)

    def sid(self):
        return util.md5("dropbox") % 10000

    def copy(self):
        return DropboxAPI()

    def _authorize(self):
        dbg.info('Request access token from Dropbox')
        flow = DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
        authorize_url = flow.start()
        # print 'Open auth url:', authorize_url
        #browser = webdriver.PhantomJS(service_log_path=os.path.join(tempfile.gettempdir(), 'ghostdriver.log'))
        #browser = webdriver.PhantomJS(service_log_path=os.path.join(tempfile.gettempdir(), 'ghostdriver.log'), service_args=['--ignore-ssl-errors=true', '--ssl-protocol=tlsv1'])
        # Change to rely on browser
        print(
            "We need to authorize access to Dropbox. Please visit the following URL and authorize the access:"
        )
        print(authorize_url)
        print("")
        code = raw_input("Input the code you got: ").strip()
        #code = #raw_input("Enter the authorization code here: ").strip()
        access_token, user_id = flow.finish(code)
        with open(self.auth_file, 'w') as file:
            file.write(access_token + "\n")
            file.write(user_id + "\n")

        dbg.info('Authentication successful')

        return (access_token, user_id)

    # return: list of file paths
    def listdir(self, path):
        dic = self.client.metadata(path)
        lst = map(lambda x: x["path"], dic["contents"])
        lst = map(lambda x: x.split("/")[-1], lst)
        return lst

    def exists(self, path):
        try:
            dic = self.client.metadata(path)
            if (dic.has_key("is_deleted") and dic["is_deleted"]): return False
            return True
        except:
            return False

    def get(self, path):
        """Get the file content

    Args:
      path: string

    Returns:
      content: string
    """

        conn = self.client.get_file(path)
        content = conn.read()
        conn.close()
        return content

    def get_file_rev(self, path, rev):
        # get file of a previous version with rev hash_id
        content = None
        try:
            conn = self.client.get_file(path, rev=rev)
            content = conn.read()
            conn.close()
        except ErrorResponse as detail:
            #print "[get_file_rev] File doesn't exist", detail
            return None
        return content

    def put(self, path, content):
        """Upload the file

    Args:
      path: string
      content: string, size <= 4MB

    Returns: None
    """
        from dropbox.rest import ErrorResponse
        strobj = StringIO(content)

        try:
            metadata = self.client.put_file(path,
                                            strobj,
                                            overwrite=False,
                                            autorename=False)
        except ErrorResponse as e:
            if e.status == 409:
                raise ItemAlreadyExists(e.status, e.reason)
            else:
                raise APIError(e.status, e.reason)
        return True

    def putdir(self, path):
        self.client.file_create_folder(path)

    def update(self, path, content):
        """Update the file
    Args and returns same as put
    """
        strobj = StringIO(content)
        metadata = self.client.put_file(path, strobj, overwrite=True)
        return True

    def rm(self, path):
        """Delete the file

    Args:
      path: string
    """
        self.client.file_delete(path)

    def rmdir(self, path):
        self.client.file_delete(path)

    def metadata(self, path):
        # only for file, not dir
        _md = self.client.metadata(path)
        md = {}
        md['size'] = _md['bytes']
        md['mtime'] = util.convert_time(_md['modified'])
        return md

    def delta(self, path=None, cursor=None):
        resp = self.client.delta(cursor=cursor, path_prefix=path)
        cursor = resp['cursor']
        changes = []

        for entry in resp['entries']:
            event = {}
            if entry[1]:
                # we don't care about delete event
                event['path'] = entry[0]
                if entry[1]['is_dir']:
                    event['type'] = 'folder'
                else:
                    event['type'] = 'file'
                changes.append(event)

        return cursor, changes

    def poll(self, path=None, cursor=None, timeout=30):
        # timeout max 480
        import requests
        import time

        from error import PollError

        beg_time = time.time()
        end_time = beg_time + timeout
        curr_time = beg_time

        url = 'https://api-notify.dropbox.com/1/longpoll_delta'
        params = {}
        changes = []
        if path:
            path = util.format_path(path)

        if not cursor:
            cursor, _ = self.delta(path)
            curr_time = time.time()

        while True:
            params['cursor'] = cursor
            params['timeout'] = max(30, int(end_time -
                                            curr_time))  # minimum 30 second

            resp = requests.request('GET', url, params=params)
            obj = resp.json()
            if 'error' in obj:
                raise PollError(resp.status_code, resp.text)

            if obj['changes']:
                cursor, _delta = self.delta(path, cursor)
                changes.extend(_delta)

            if changes:
                break
            curr_time = time.time()
            if curr_time > end_time:
                break

        return cursor, changes

    def init_log(self, path):
        if not self.exists(path):
            self.put(path, '')

    def reset_log(self, path):
        if self.exists(path):
            self.rm(path)

    def append(self, path, msg):
        self.update(path, msg)

    def get_logs(self, path, last_clock):

        length = 5
        # latest revision comes first
        revisions = self.client.revisions(path, rev_limit=length)
        if not revisions:
            return [], None

        new_logs = []
        new_clock = revisions[0]['rev']
        end = False  # if reach to end

        while True:
            for metadata in revisions:
                if last_clock and metadata['rev'] == last_clock:
                    end = True
                    break
            if end: break
            if len(revisions) < length: break
            # still have logs unread, double the length
            length *= 2
            revisions = self.client.revisions(path, rev_limit=length)

        # download the content of unseen rev
        for metadata in revisions:
            if last_clock and metadata['rev'] == last_clock:
                break
            if 'is_deleted' in metadata and metadata['is_deleted']: continue
            msg = self.get_file_rev(path, metadata['rev'])
            if len(msg) > 0:
                new_logs.insert(0, msg)

        return new_logs, new_clock

    def __msg_index(self, fn):
        return eval(fn[3:])

    def init_log2(self, path):
        if not self.exists(path):
            self.putdir(path)

    def append2(self, path, msg):
        path = util.format_path(path)
        lst = sorted(self.listdir(path))
        if lst:
            index = self.__msg_index(lst[-1]) + 1
        else:
            index = 0

        while True:
            fn = 'msg%d' % index
            fpath = path + '/' + fn
            try:
                self.put(fpath, msg)
            except ItemAlreadyExists:
                index += 1
            else:
                break

    def get_logs2(self, path, last_clock):
        path = util.format_path(path)
        lst = self.listdir(path)
        if not lst:
            return [], None

        srt = {}
        for fn in lst:
            srt[self.__msg_index(fn)] = fn
        lst = [srt[i] for i in sorted(srt.keys(), reverse=True)]
        new_logs = []
        new_clock = self.__msg_index(lst[0])

        for fn in lst:
            if last_clock == None and self.__msg_index(fn) == last_clock: break
            msg = self.get(path + '/' + fn)
            new_logs.insert(0, msg)

        return new_logs, new_clock

    def share(self, path, target_email):
        url = "https://www.dropbox.com/"
        print 'Get access token from Dropbox'
        print 'Open auth url:', url
        browser = webdriver.PhantomJS(
            service_log_path=os.path.join(tempfile.gettempdir(),
                                          'ghostdriver.log'),
            service_args=['--ignore-ssl-errors=true', '--ssl-protocol=tlsv1'])
        browser.get(url)
        try:
            wait = WebDriverWait(browser, 30)
            btn = wait.until(
                EC.element_to_be_clickable(
                    (By.XPATH, "//div[@id='sign-in']/a")))
            btn.click()
            email = wait.until(
                EC.element_to_be_clickable(
                    (By.XPATH, "//input[@id='login_email']")))
            email.send_keys(raw_input("Enter your Dropbox email:"))
            pwd = browser.find_element_by_xpath(
                "//input[@id='login_password']")
            pwd.send_keys(getpass.getpass("Enter your Dropbox password:"******"//a[text()='%s']" % path)))
            target_folder.click()
            wait.until(EC.title_contains("%s" % path))
            share_btn = browser.find_element_by_xpath(
                "//a[@id='global_share_button']")
            share_btn.click()
            target = wait.until(
                EC.element_to_be_clickable((
                    By.XPATH,
                    "//form[@class='invite-more-form']//input[@spellcheck][@type='text']"
                )))
            target.send_keys(target_email)
            confirm_btn = browser.find_element_by_xpath(
                "//form[@class='invite-more-form']//input[@type='button'][1]")
            confirm_btn.click()
        except:
            print(browser.title)
            assert False
            # print(browser.current_url)
            # print(browser.page_source)
            pass
Пример #13
0
    logging.info(__INFOFILE__.format(cnt_add = str(len(file_to_be_added)), cnt_delete = str(len(file_to_be_removed))))
    
    """ Is there any faster way to get multiple files from the FTP Server? """
    for f in file_to_be_added:
        try:
            fp = cStringIO.StringIO()
            nasclient.getFile(f,fp)

            dropboxclient.put_file(path.join(__DROPBOXDIR__,f).replace("\\","/"),fp)
            fp.close()
            logging.info(__FILESUCCMSG__.format(filename = f, action = "ADD"))
        except Exception as e:
            logging.info(__ERRMSG3__.format(filename = f,device="Dropbox",raisemsg = str(e)))
            anyerror = True
            continue

    for f in file_to_be_removed:
        try:
            dropboxclient.file_delete(path.join(__DROPBOXDIR__,f).replace("\\","/"))
            logging.info(__FILESUCCMSG__.format(filename = f, action = "REMOVE"))
        except Exception as e:
            logging.info(__ERRMSG4__.format(filename = f,device="Dropbox",raisemsg = str(e)))
            anyerror = True
            continue

if anyerror:
    logging.info(__SUCCWARNMSG__)
else:
    logging.info(__SUCCMSG__)
Пример #14
0
class DropboxAPI(StorageAPI, AppendOnlyLog):
  "dropbox@auth : dropbox.com account with auth info"

  def __init__(self):
    from params import AUTH_DIR
    authdir = AUTH_DIR 
    self.auth_file = os.path.join(authdir, 'dropbox.auth')
    try:
      with open(self.auth_file, 'r') as file:
        ACCESS_TOKEN = file.readline().rstrip()
        USER_ID = file.readline().rstrip()
    except IOError:
      ACCESS_TOKEN, USER_ID = self._authorize()

    self.client = DropboxClient(ACCESS_TOKEN)

  def sid(self):
    return util.md5("dropbox") % 10000

  def copy(self):
    return DropboxAPI()


  def _authorize(self):
    dbg.info('Request access token from Dropbox')
    flow = DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
    authorize_url = flow.start()
    # print 'Open auth url:', authorize_url
    #browser = webdriver.PhantomJS(service_log_path=os.path.join(tempfile.gettempdir(), 'ghostdriver.log'))
    #browser = webdriver.PhantomJS(service_log_path=os.path.join(tempfile.gettempdir(), 'ghostdriver.log'), service_args=['--ignore-ssl-errors=true', '--ssl-protocol=tlsv1'])
    # Change to rely on browser
    print("We need to authorize access to Dropbox. Please visit the following URL and authorize the access:")
    print(authorize_url)
    print("")
    code = raw_input("Input the code you got: ").strip()
    #code = #raw_input("Enter the authorization code here: ").strip()
    access_token, user_id = flow.finish(code)
    with open(self.auth_file, 'w') as file:
      file.write(access_token + "\n")
      file.write(user_id + "\n")

    dbg.info('Authentication successful')

    return (access_token, user_id)

  # return: list of file paths
  def listdir(self, path):
    dic = self.client.metadata(path)
    lst = map(lambda x:x["path"], dic["contents"])
    lst = map(lambda x:x.split("/")[-1], lst)
    return lst

  def exists(self, path):
    try:
      dic = self.client.metadata(path)
      if(dic.has_key("is_deleted") and dic["is_deleted"]): return False
      return True
    except:
      return False

  def get(self, path):
    """Get the file content

    Args:
      path: string

    Returns:
      content: string
    """

    conn = self.client.get_file(path)
    content = conn.read()
    conn.close()
    return content

  def get_file_rev(self, path, rev):
    # get file of a previous version with rev hash_id
    content = None
    try:
      conn = self.client.get_file(path, rev=rev)
      content = conn.read()
      conn.close()
    except ErrorResponse as detail:
      #print "[get_file_rev] File doesn't exist", detail
      return None
    return content

  def put(self, path, content):
    """Upload the file

    Args:
      path: string
      content: string, size <= 4MB

    Returns: None
    """
    from dropbox.rest import ErrorResponse
    strobj = StringIO(content)

    try:
      metadata = self.client.put_file(path, strobj, overwrite=False, autorename=False)
    except ErrorResponse as e:
      if e.status == 409:
        raise ItemAlreadyExists(e.status, e.reason)
      else:
        raise APIError(e.status, e.reason)
    return True

  def putdir(self, path):
    self.client.file_create_folder(path)

  def update(self, path, content):
    """Update the file
    Args and returns same as put
    """
    strobj = StringIO(content)
    metadata = self.client.put_file(path, strobj, overwrite=True)
    return True

  def rm(self, path):
    """Delete the file

    Args:
      path: string
    """
    self.client.file_delete(path)

  def rmdir(self, path):
    self.client.file_delete(path)

  def metadata(self, path):
    # only for file, not dir
    _md = self.client.metadata(path)
    md = {}
    md['size'] = _md['bytes']
    md['mtime'] = util.convert_time(_md['modified'])
    return md

  def delta(self, path=None, cursor=None):
    resp = self.client.delta(cursor=cursor, path_prefix=path)
    cursor = resp['cursor']
    changes = []

    for entry in resp['entries']:
      event = {}
      if entry[1]:
        # we don't care about delete event
        event['path'] = entry[0]
        if entry[1]['is_dir']:
          event['type'] = 'folder'
        else:
          event['type'] = 'file'
        changes.append(event)

    return cursor, changes

  def poll(self, path=None, cursor=None, timeout=30):
    # timeout max 480
    import requests
    import time

    from error import PollError

    beg_time = time.time()
    end_time = beg_time + timeout
    curr_time = beg_time

    url = 'https://api-notify.dropbox.com/1/longpoll_delta'
    params = {}
    changes = []
    if path:
      path = util.format_path(path)

    if not cursor:
      cursor, _ = self.delta(path)
      curr_time = time.time()

    while True:
      params['cursor'] = cursor
      params['timeout'] = max(30, int(end_time - curr_time)) # minimum 30 second

      resp = requests.request('GET', url, params=params)
      obj = resp.json()
      if 'error' in obj:
        raise PollError(resp.status_code, resp.text)

      if obj['changes']:
        cursor, _delta = self.delta(path, cursor)
        changes.extend(_delta)
      
      if changes:
        break
      curr_time = time.time()
      if curr_time > end_time:
        break

    return cursor, changes

  def init_log(self, path):
    if not self.exists(path):
      self.put(path, '')

  def reset_log(self, path):
    if self.exists(path):
      self.rm(path)

  def append(self, path, msg):
    self.update(path, msg)

  def get_logs(self, path, last_clock):

    length = 5
    # latest revision comes first
    revisions = self.client.revisions(path, rev_limit=length)
    if not revisions:
      return [], None

    new_logs = []
    new_clock = revisions[0]['rev']
    end = False # if reach to end

    while True:
      for metadata in revisions:
        if last_clock and metadata['rev'] == last_clock:
          end = True
          break
      if end: break
      if len(revisions) < length: break
      # still have logs unread, double the length
      length *= 2
      revisions = self.client.revisions(path, rev_limit=length)

    # download the content of unseen rev
    for metadata in revisions:
      if last_clock and metadata['rev'] == last_clock:
        break
      if 'is_deleted' in metadata and metadata['is_deleted']: continue
      msg = self.get_file_rev(path, metadata['rev'])
      if len(msg) > 0:
        new_logs.insert(0, msg)

    return new_logs, new_clock

  def __msg_index(self, fn):
    return eval(fn[3:])

  def init_log2(self, path):
    if not self.exists(path):
      self.putdir(path)

  def append2(self, path, msg):
    path = util.format_path(path)
    lst = sorted(self.listdir(path))
    if lst:
      index = self.__msg_index(lst[-1]) + 1
    else:
      index = 0
    
    while True:
      fn = 'msg%d' % index
      fpath = path + '/' + fn
      try:
        self.put(fpath, msg)
      except ItemAlreadyExists:
        index += 1
      else:
        break

  def get_logs2(self, path, last_clock):
    path = util.format_path(path)
    lst = self.listdir(path)
    if not lst:
      return [], None

    srt = {}
    for fn in lst:
      srt[self.__msg_index(fn)] = fn
    lst = [srt[i] for i in sorted(srt.keys(), reverse=True)]
    new_logs = []
    new_clock = self.__msg_index(lst[0])

    for fn in lst:
      if last_clock == None and self.__msg_index(fn) == last_clock: break
      msg = self.get(path + '/' + fn)
      new_logs.insert(0, msg)

    return new_logs, new_clock

  def share(self, path, target_email):
    url = "https://www.dropbox.com/"
    print 'Get access token from Dropbox'
    print 'Open auth url:', url
    browser = webdriver.PhantomJS(service_log_path=os.path.join(tempfile.gettempdir(), 'ghostdriver.log'), service_args=['--ignore-ssl-errors=true', '--ssl-protocol=tlsv1'])
    browser.get(url)
    try:
      wait = WebDriverWait(browser, 30)
      btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='sign-in']/a")))
      btn.click()
      email = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='login_email']")))
      email.send_keys(raw_input("Enter your Dropbox email:"))
      pwd = browser.find_element_by_xpath("//input[@id='login_password']") 
      pwd.send_keys(getpass.getpass("Enter your Dropbox password:"******"//a[text()='%s']" % path)))
      target_folder.click()
      wait.until(EC.title_contains("%s" % path))
      share_btn = browser.find_element_by_xpath("//a[@id='global_share_button']")
      share_btn.click()
      target = wait.until(EC.element_to_be_clickable((By.XPATH, "//form[@class='invite-more-form']//input[@spellcheck][@type='text']")))
      target.send_keys(target_email)
      confirm_btn = browser.find_element_by_xpath("//form[@class='invite-more-form']//input[@type='button'][1]")
      confirm_btn.click()
    except:
      print(browser.title)
      assert False
      # print(browser.current_url)
      # print(browser.page_source)    
      pass
Пример #15
0
class DropboxNoteProvider(RemoteNoteProvider):

    __DAY_ONE_EXTENSION= ".doentry"

    def __init__(self, accessToken, folder, proxyHost=None, proxyPort=None, proxyUser=None, proxyPassword=None):
        self.__token= accessToken
        self.__notesPath= folder + "/entries"
        self.__removedNotesPath= self.__notesPath + "/deleted"
        self.__photosPath= folder + "/photos"
        self.__removedPhotosPath= self.__photosPath + "/deleted"
        self.__client= DropboxClient(self.__token, rest_client=_restClient(proxyHost, proxyPort, proxyUser, proxyPassword))
        self.__notesCache= {}
        self.__dayOneFlavor= folder == SyncFolder.DayOne

    def requiresReverseUpdate(self):
        return True

    @online
    @expires
    def sync(self):
        notes= self.__list(self.__notesPath, False)
        removedNotes= self.__list(self.__removedNotesPath, True)
        #Clean inconsistent files
        for uuid in list(notes.keys()):
            if uuid in removedNotes:
                if removedNotes[uuid].lastModified >= notes[uuid].lastModified:
                    self.__client.file_delete(self.__notePath(uuid))
                    if notes[uuid].hasPhoto:
                        self.__client.file_delete(self.__photoPath(uuid))
                    del notes[uuid]
                else:
                    self.__client.file_delete(self.__removedNotePath(uuid))
                    del removedNotes[uuid]
        notes.update(removedNotes)
        self.__notesCache= notes
        return notes

    @online
    @expires
    def get(self, uuid):
        response, metadata= self.__client.get_file_and_metadata(self.__notePath(uuid))
        with response:
            note= unmarshalNote(response.read(), getLastModified(metadata))
        if uuid not in self.__notesCache or self.__notesCache[uuid].hasPhoto:
            try:
                with self.__client.get_file(self.__photoPath(uuid)) as response:
                    note.photo= response.read()
            except ErrorResponse as e:
                if e.status == 404: #Photo does not exist
                    pass
                else:
                    raise e
        return renderHtml(note)

    @online
    @expires
    def add(self, note):
        """
        The note lastModified time is updated from Dropbox when the operations succeeds (unfortunately there's no way to
        set the modified time in Dropbox).
        """
        uuid= note.uuid
        result= self.__client.put_file(self.__notePath(uuid), marshalNote(note))
        if len(result["path"]) != len(self.__notePath(uuid)):
            try:
                self.__client.file_delete(result["path"])
            except ErrorResponse:
                pass
            raise RuntimeError("Note[uuid=%s] already exists" % uuid)
        note.lastModified= getLastModified(result)

        if note.photo:
            self.__client.put_file(self.__photoPath(uuid), note.photo, overwrite=True)
        elif uuid in self.__notesCache and self.__notesCache[uuid].hasPhoto:
            try:
                self.__client.file_delete(self.__photoPath(uuid))
            except ErrorResponse:
                pass
        renderHtml(note)

        #Clean removed note if exists
        if uuid in self.__notesCache and self.__notesCache[uuid].removed:
            try:
                self.__client.file_delete(self.__removedNotePath(uuid))
            except ErrorResponse:
                pass

    @online
    @expires
    def update(self, note):
        """
        The note lastModified time is updated from Dropbox when the operations succeeds (unfortunately there's no way to
        set the modified time in Dropbox).
        """
        uuid= note.uuid
        #Check if note exists
        if self.__notesCache and (uuid not in self.__notesCache or self.__notesCache[uuid].removed):
            raise RuntimeError("Note[uuid=%s] does not exist" % uuid)

        result= self.__client.put_file(self.__notePath(uuid), marshalNote(note), overwrite=True)
        note.lastModified= getLastModified(result)

        if note.photo:
            self.__client.put_file(self.__photoPath(uuid), note.photo, overwrite=True)
        elif uuid not in self.__notesCache or self.__notesCache[uuid].hasPhoto:
            try:
                self.__client.file_delete(self.__photoPath(uuid))
            except ErrorResponse:
                pass
        renderHtml(note)

    @online
    @expires
    def remove(self, note):
        """
        The note lastModified time is updated from Dropbox when the operations succeeds (unfortunately there's no way to
        set the modified time in Dropbox).
        """
        uuid= note.uuid

        #Remove note if exists
        if uuid in self.__notesCache and not self.__notesCache[uuid].removed:
            try:
                self.__client.file_delete(self.__notePath(uuid))
            except ErrorResponse:
                pass

        #Remove photo if exists
        if uuid in self.__notesCache and self.__notesCache[uuid].hasPhoto:
            try:
                self.__client.file_delete(self.__photoPath(uuid))
            except ErrorResponse:
                pass

        result= self.__client.put_file(self.__removedNotePath(uuid), b"", overwrite=True)
        note.lastModified= getLastModified(result)

    def __list(self, path, removed):
        folder= self.__client.metadata(path)
        if not folder["is_dir"]:
            raise RuntimeError("Path is not a folder")

        notes= {}
        pos= len(path) + 1
        for file in folder["contents"]:
            if not file["is_dir"]:
                name= self.__fileUuid(file["path"][pos:])
                if isUuid(name):
                    notes[name]= NoteStatus(name, getLastModified(file), removed)

        if not removed:
            folder= self.__client.metadata(self.__photosPath)
            if not folder["is_dir"]:
                raise RuntimeError("Path is not a folder")

            pos= len(self.__photosPath) + 1
            for file in folder["contents"]:
                name= file["path"][pos:]
                if not file["is_dir"] and name.endswith(".jpg"):
                    name= name[:-4]
                    if name in notes:
                        notes[name].hasPhoto= True

        return notes

    def __fileUuid(self, filename):
        if self.__dayOneFlavor and filename.endswith(self.__DAY_ONE_EXTENSION):
            return filename[:-(len(self.__DAY_ONE_EXTENSION))]
        return filename

    def __buildNotePath(self, parentPath, uuid):
        path= parentPath + "/" + uuid
        if self.__dayOneFlavor:
            path+= self.__DAY_ONE_EXTENSION
        return path

    def __notePath(self, uuid):
        return self.__buildNotePath(self.__notesPath, uuid)

    def __removedNotePath(self, uuid):
        return self.__buildNotePath(self.__removedNotesPath, uuid)

    def __photoPath(self, uuid):
        return self.__photosPath + "/" + uuid + ".jpg"
Пример #16
0
class DropboxStorage(Storage):
    """
    A storage class providing access to resources in a Dropbox Public folder.
    """
    def __init__(self, location='/Public'):
        session = DropboxSession(CONSUMER_KEY,
                                 CONSUMER_SECRET,
                                 ACCESS_TYPE,
                                 locale=None)
        session.set_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        self.client = DropboxClient(session)
        self.account_info = self.client.account_info()
        self.location = location
        self.base_url = 'http://dl.dropbox.com/u/{uid}/'.format(
            **self.account_info)

    def _get_abs_path(self, name):
        return os.path.realpath(os.path.join(self.location, name))

    def _open(self, name, mode='rb'):
        name = self._get_abs_path(name)
        remote_file = DropboxFile(name, self, mode=mode)
        return remote_file

    def _save(self, name, content):
        name = self._get_abs_path(name)
        directory = os.path.dirname(name)
        if not self.exists(directory) and directory:
            self.client.file_create_folder(directory)
        response = self.client.metadata(directory)
        if not response['is_dir']:
            raise IOError("%s exists and is not a directory." % directory)
        abs_name = os.path.realpath(os.path.join(self.location, name))
        self.client.put_file(abs_name, content)
        return name

    def delete(self, name):
        name = self._get_abs_path(name)
        self.client.file_delete(name)

    def exists(self, name):
        name = self._get_abs_path(name)
        try:
            metadata = self.client.metadata(name)
            if metadata.get('is_deleted'):
                return False
        except ErrorResponse as e:
            if e.status == 404:  # not found
                return False
            raise e
        return True

    def listdir(self, path):
        path = self._get_abs_path(path)
        response = self.client.metadata(path)
        directories = []
        files = []
        for entry in response.get('contents', []):
            if entry['is_dir']:
                directories.append(os.path.basename(entry['path']))
            else:
                files.append(os.path.basename(entry['path']))
        return directories, files

    def size(self, name):
        cache_key = 'django-dropbox-size:%s' % filepath_to_uri(name)
        size = cache.get(cache_key)

        if not size:
            size = self.client.metadata(filepath_to_uri(name))['bytes']
            cache.set(cache_key, size, CACHE_TIMEOUT)

        return size

    def url(self, name):
        cache_key = 'django-dropbox-url:%s' % filepath_to_uri(name)
        url = cache.get(cache_key)

        if not url:
            url = self.client.share(filepath_to_uri(name),
                                    short_url=False)['url'] + '?dl=1'
            cache.set(cache_key, url, CACHE_TIMEOUT)

        return url

    def get_available_name(self, name):
        """
        Returns a filename that's free on the target storage system, and
        available for new content to be written to.
        """
        name = self._get_abs_path(name)
        dir_name, file_name = os.path.split(name)
        file_root, file_ext = os.path.splitext(file_name)
        # If the filename already exists, add an underscore and a number (before
        # the file extension, if one exists) to the filename until the generated
        # filename doesn't exist.
        count = itertools.count(1)
        while self.exists(name):
            # file_ext includes the dot.
            name = os.path.join(
                dir_name, "%s_%s%s" % (file_root, count.next(), file_ext))

        return name
Пример #17
0
class DropboxStorage(Storage):
   """
   A storage class providing access to resources in a Dropbox Public folder.
   """
   def __init__(self, location=None):
     session = DropboxSession(DROPBOX.app_key, DROPBOX.app_secret, DROPBOX.access_type, locale=None)
     session.set_token(DROPBOX.access_key, DROPBOX.access_secret)
     self.client = DropboxClient(session)
     self.overwrite_mode = DROPBOX.overwrite_mode
     self._location = location

   @prepend_path_with_attr("_location")
   def delete(self, name):
     self.client.file_delete(name)

   @prepend_path_with_attr("_location")
   def exists(self, name):
     try:
       metadata = self.client.metadata(name, list=False)
       return not metadata.get('is_deleted')
     except ErrorResponse as e:
       if 404 == e.status: # not found
         return False
       raise e 
     return True

   @prepend_path_with_attr("_location")
   def listdir(self, name, query_filter=None):
     if query_filter is None or len(query_filter) < 3:
       metadata = self.client.metadata(name)
     else:
       metadata = self.client.search(name, query_filter, file_limit=25000)
     directories = []
     files = []
     for entry in metadata.get('contents', []):
       if entry['is_dir']:
         directories.append(os.path.basename(entry['path']))
       else:
         files.append(os.path.basename(entry['path']))
     return directories, files

   @prepend_path_with_attr("_location")
   def open(self, name, mode='rb'):
     return DropboxFile(name, self, mode)

   @prepend_path_with_attr("_location")
   def save(self, name, content):
     metadata = self.client.put_file(name, content)
     return metadata['path']

   @prepend_path_with_attr("_location")
   def size(self, name):
     return self.client.metadata(name, list=False)['bytes']

   @prepend_path_with_attr("_location")
   def url(self, name):
     try:
       return self.client.share(name)['url']
     except ErrorResponse as e:
       if 404 == e.status: # not found
         return None
       raise e

   @prepend_path_with_attr("_location")
   def get_available_name(self, name):
     """
     Returns a filename that's free on the target storage system, and
     available for new content to be written to.
     """
     if self.overwrite_mode:
       return name

     if self.exists(name):
       dir_name, file_name = os.path.split(name)
       file_root, file_ext = os.path.splitext(file_name)
       
       # If the filename already exists, add an underscore and a number (before
       # the file extension, if one exists) to the filename until the generated
       # filename doesn't exist.
       dir_contents = self.listdir(dir_name, file_root)
       count = itertools.count(1)
       while True:
         # file_ext includes the dot.
         name = "%s_%s%s" % (file_root, count.next(), file_ext)
         if name not in dir_contents:
           return os.path.join(dir_name, name)
     else:
       return name

   @prepend_path_with_attr("_location")
   def get_valid_name(self, name):
     return name
Пример #18
0
    def delete_file_dropbox(self, user_profile):
        api_client = DropboxClient(user_profile.dropbox_profile.access_token["key"])

        result = api_client.file_delete(self.path)
        return True
Пример #19
0
 def historicalCloud(self):
     user = self.getData()
     while len(self.getAllBackupsByDate()) >= user['FileHistoricalNumberCloud']:
         cliente = DropboxClient(self.TOKEN)
         respuesta = cliente.file_delete(self.getAllBackupsByDate()[0]['path'])
Пример #20
0
class DropBox(clouddrive.CloudDrive):
    name = "DropBox"

    def __init__(self, access_token):
        self.client = DropboxClient(access_token)
        self.access_token = access_token
        clouddrive.CloudDrive.__init__(self, access_token)

    @staticmethod
    def auth():
        #app_key = 'knbyx2adg14kkn5'
        #app_secret = 'kh3ulgqry8jffqp'
        app_key = 'eif0l7bgnpb06di'
        app_secret = 'qa02jhdo4jrwaid'

        global Auth_DROPMytoken
        global Auth_Drop_Running_true

        def keep_running():
            global Auth_Drop_Running_true
            return Auth_Drop_Running_true

        class AuthHandler(BaseHTTPServer.BaseHTTPRequestHandler):
            def do_HEAD(s):
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()

            def do_GET(s):
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()
                if s.path.find("code=") != -1:
                    global Auth_DROPMytoken
                    global Auth_Drop_Running_true
                    Auth_DROPMytoken = s.path
                    Auth_Drop_Running_true = False
                    s.wfile.write("ok see command line")
                    return

        class MYrequest:
            def __init__(self, url):
                self.url = url

            def get(self, mystring):
                if self.url.find(mystring) == -1:
                    return
                item = self.url.split(mystring + "=")[1]
                if item.find("&") != -1:
                    item = item.split("&")[0]
                return urllib.unquote(item).decode()

        redirect_url = "http://localhost:8787"
        my_session = {}
        Auth_Drop_Running_true = True
        flow = DropboxOAuth2Flow(app_key, app_secret, redirect_url, my_session,
                                 "dropbox-auth-csrf-token")

        authorize_url = flow.start()
        webbrowser.open(authorize_url)

        try:
            httpd = BaseHTTPServer.HTTPServer(("", 8787), AuthHandler)
            while keep_running():
                httpd.handle_request()
        except KeyboardInterrupt:
            pass
        httpd.server_close()

        token, user_id, url_state = flow.finish(MYrequest(Auth_DROPMytoken))
        clouddrive.CloudDrive.access_token = token
        return token

    def read(self, filename):
        try:
            data = []
            with self.client.get_file(filename) as f:
                data += f.read()
        except:
            return -1
        return ''.join(data)

    def write(self, filename, data):
        try:
            response = self.client.put_file(filename, data, True)
        except:
            return -1
        return 1

    def mkdir(self, dirname):
        try:
            self.client.file_create_folder(dirname)
        except:
            return -1
        return 1

    def delete(self, filename):
        try:
            self.client.file_delete(filename)
        except:
            return -1
        return 1

    def capacity(self):
        try:
            a = self.client.account_info()["quota_info"]
        except:
            return -1
        return a["quota"] - a["shared"] - a["normal"]

    def listing(self, path="/"):

        lists = self.client.metadata(path)
        filelist = []
        for x in lists["contents"]:
            filelist.append((x['path'], x['is_dir']))
        return filelist
Пример #21
0
class DropboxStorage(Storage):
    """
    A storage class providing access to resources in a Dropbox Public folder.
    """

    def __init__(self, location='/Public'):
        session = DropboxSession(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TYPE, locale=None)
        session.set_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        self.client = DropboxClient(session)
        self.account_info = self.client.account_info()
        self.location = location
        self.base_url = 'http://dl.dropbox.com/u/{uid}/'.format(**self.account_info)

    def _get_abs_path(self, name):
        return os.path.realpath(os.path.join(self.location, name))

    def _open(self, name, mode='rb'):
        name = self._get_abs_path(name)
        remote_file = DropboxFile(name, self, mode=mode)
        return remote_file

    def _save(self, name, content):
        name = self._get_abs_path(name)
        directory = os.path.dirname(name)
        if not self.exists(directory) and directory:
             self.client.file_create_folder(directory)
        response = self.client.metadata(directory)
        if not response['is_dir']:
             raise IOError("%s exists and is not a directory." % directory)
        abs_name = os.path.realpath(os.path.join(self.location, name))
        self.client.put_file(abs_name, content)
        return name

    def delete(self, name):
        name = self._get_abs_path(name)
        self.client.file_delete(name)

    def exists(self, name):
        name = self._get_abs_path(name)
        try:
            metadata = self.client.metadata(name)
            if metadata.get('is_deleted'):
                return False
        except ErrorResponse as e:
            if e.status == 404: # not found
                return False
            raise e
        return True

    def listdir(self, path):
        path = self._get_abs_path(path)
        response = self.client.metadata(path)
        directories = []
        files = []
        for entry in response.get('contents', []):
            if entry['is_dir']:
                directories.append(os.path.basename(entry['path']))
            else:
                files.append(os.path.basename(entry['path']))
        return directories, files

    def size(self, name):
        cache_key = 'django-dropbox-size:%s' % filepath_to_uri(name)
        size = cache.get(cache_key)

        if not size:
            size = self.client.metadata(filepath_to_uri(name))['bytes']
            cache.set(cache_key, size, CACHE_TIMEOUT)

        return size

    def url(self, name):
        cache_key = 'django-dropbox-url:%s' % filepath_to_uri(name)
        url = cache.get(cache_key)

        if not url:
            url = self.client.share(filepath_to_uri(name), short_url=False)['url'] + '?dl=1'
            cache.set(cache_key, url, CACHE_TIMEOUT)

        return url

    def get_available_name(self, name):
        """
        Returns a filename that's free on the target storage system, and
        available for new content to be written to.
        """
        name = self._get_abs_path(name)
        dir_name, file_name = os.path.split(name)
        file_root, file_ext = os.path.splitext(file_name)
        # If the filename already exists, add an underscore and a number (before
        # the file extension, if one exists) to the filename until the generated
        # filename doesn't exist.
        count = itertools.count(1)
        while self.exists(name):
            # file_ext includes the dot.
            name = os.path.join(dir_name, "%s_%s%s" % (file_root, count.next(), file_ext))

        return name
Пример #22
0
class DropBoxStorage(Storage):
    """DropBox Storage class for Django pluggable storage system."""

    def __init__(self, oauth2_access_token=None, root_path=None):
        oauth2_access_token = oauth2_access_token or setting('DROPBOX_OAUTH2_TOKEN')
        self.root_path = root_path or setting('DROPBOX_ROOT_PATH', '/')
        if oauth2_access_token is None:
            raise ImproperlyConfigured("You must configure a token auth at"
                                       "'settings.DROPBOX_OAUTH2_TOKEN'.")
        self.client = DropboxClient(oauth2_access_token)

    def _full_path(self, name):
        if name == '/':
            name = ''
        return safe_join(self.root_path, name).replace('\\', '/')

    def delete(self, name):
        self.client.file_delete(self._full_path(name))

    def exists(self, name):
        try:
            return bool(self.client.metadata(self._full_path(name)))
        except ErrorResponse:
            return False

    def listdir(self, path):
        directories, files = [], []
        full_path = self._full_path(path)
        metadata = self.client.metadata(full_path)
        for entry in metadata['contents']:
            entry['path'] = entry['path'].replace(full_path, '', 1)
            entry['path'] = entry['path'].replace('/', '', 1)
            if entry['is_dir']:
                directories.append(entry['path'])
            else:
                files.append(entry['path'])
        return directories, files

    def size(self, name):
        metadata = self.client.metadata(self._full_path(name))
        return metadata['bytes']

    def modified_time(self, name):
        metadata = self.client.metadata(self._full_path(name))
        mod_time = datetime.strptime(metadata['modified'], DATE_FORMAT)
        return mod_time

    def accessed_time(self, name):
        metadata = self.client.metadata(self._full_path(name))
        acc_time = datetime.strptime(metadata['client_mtime'], DATE_FORMAT)
        return acc_time

    def url(self, name):
        media = self.client.media(self._full_path(name))
        return media['url']

    def _open(self, name, mode='rb'):
        remote_file = DropBoxFile(self._full_path(name), self)
        return remote_file

    def _save(self, name, content):
        self.client.put_file(self._full_path(name), content)
        return name
f = open(backup_file, 'rb')
response = dbx.put_file('/your_dropbox_app_folder/' + destination, f)

print "Response: ", response
# Retention
head = ''
total_backups = get_line_count('/root/backup_script/backup_history')
print "Checking retention"
print "There are %s backups | retention is %s" %(total_backups, retention)
diff = int(total_backups) - int(retention)
if diff > 0 :
    # Delete diff number of folders from the top of the file backup_history
    print "There are files to be deleted in the destination"
    with open("/root/backup_script/backup_history") as f:
        head = [next(f) for x in xrange(diff)]

# delete everything in head
if head:
	for folder in head:
	    os.system("sed -i '1d' /root/backup_script/backup_history")
	    print "Deleting ", folder
	    folder = folder.rstrip('\n')
	    dbx.file_delete('/your_dropbox_app_folder' + folder + '.tar.gz')
	    print "Deleted successfully"

#cleaning up
print "Cleaning up.."
os.system('rm -rf /tmp/databases/*')
os.system("rm -f %s" %(backup_file))
print "All done"