diff --git a/duplicity/backends/dpbxbackend.py b/duplicity/backends/dpbxbackend.py index d7525b66..4b70e50f 100644 --- a/duplicity/backends/dpbxbackend.py +++ b/duplicity/backends/dpbxbackend.py @@ -98,7 +98,14 @@ def __init__(self, parsed_url): self.auth_flow = None self.login() - + def user_authenticated(self): + try: + account = self.api_client.users_get_current_account() + log.Debug("User authenticated as ,%s" % account) + return True + except: + log.Debug('User not authenticated') + return False def load_access_token(self): return os.environ.get('DPBX_ACCESS_TOKEN', None) @@ -196,10 +203,14 @@ def _put(self, source_path, remote_filename): (res_metadata.size, file_size)) def put_file_small(self, source_path, remote_path): + if not self.user_authenticated(): + self.login() + file_size = os.path.getsize(source_path.name) f = source_path.open('rb') try: log.Debug('dpbx,files_upload(%s, [%d bytes])' % (remote_path, file_size)) + res_metadata = self.api_client.files_upload(f, remote_path, mode=WriteMode.overwrite, autorename=False, @@ -212,6 +223,9 @@ def put_file_small(self, source_path, remote_path): f.close() def put_file_chunked(self, source_path, remote_path): + if not self.user_authenticated(): + self.login() + file_size = os.path.getsize(source_path.name) f = source_path.open('rb') try: @@ -295,6 +309,10 @@ def put_file_chunked(self, source_path, remote_path): log.Debug('dpbx,files_upload_session_append: %s' % e) retry_number -= 1 + + if not self.user_authenticated(): + self.login() + if retry_number == 0: raise @@ -320,6 +338,9 @@ def put_file_chunked(self, source_path, remote_path): @command() def _get(self, remote_filename, local_path): + if not self.user_authenticated(): + self.login() + remote_dir = urllib.unquote(self.parsed_url.path.lstrip('/')) remote_path = '/' + os.path.join(remote_dir, remote_filename).rstrip() @@ -353,6 +374,8 @@ def _get(self, remote_filename, local_path): @command() def _list(self): # Do a long listing to avoid connection reset + if not self.user_authenticated(): + self.login() remote_dir = '/' + urllib.unquote(self.parsed_url.path.lstrip('/')).rstrip() log.Debug('dpbx.files_list_folder(%s)' % remote_dir) @@ -373,6 +396,9 @@ def _list(self): @command() def _delete(self, filename): + if not self.user_authenticated(): + self.login() + remote_dir = urllib.unquote(self.parsed_url.path.lstrip('/')) remote_path = '/' + os.path.join(remote_dir, filename).rstrip() @@ -390,6 +416,8 @@ def _close(self): @command() def _query(self, filename): + if not self.user_authenticated(): + self.login() remote_dir = urllib.unquote(self.parsed_url.path.lstrip('/')) remote_path = '/' + os.path.join(remote_dir, filename).rstrip() @@ -399,6 +427,8 @@ def _query(self, filename): return {'size': info.size} def check_renamed_files(self, file_list): + if not self.user_authenticated(): + self.login() bad_list = [x for x in file_list if DPBX_AUTORENAMED_FILE_RE.search(x) is not None] if len(bad_list) == 0: return