1
1
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
3
# Copyright 2014 Håvard Gulldahl
3
# Copyright 2014 HAvard Gulldahl
5
5
# in part based on dpbxbackend.py:
6
6
# Copyright 2013 jno <jno@pisem.net>
35
35
def get_jotta_device(jfs):
37
37
for j in jfs.devices: # find Jotta/Shared folder
38
if j.name == u'Jotta':
43
43
def get_root_dir(jfs):
44
44
jottadev = get_jotta_device(jfs)
45
root_dir = jottadev.mountPoints['Archive']
45
root_dir = jottadev.mountPoints[u'Archive']
49
49
def set_jottalib_logging_level(log_level):
50
logger = logging.getLogger('jottalib')
50
logger = logging.getLogger(u'jottalib')
51
51
logger.setLevel(getattr(logging, log_level))
54
54
def set_jottalib_log_handlers(handlers):
55
logger = logging.getLogger('jottalib')
55
logger = logging.getLogger(u'jottalib')
56
56
for handler in handlers:
57
57
logger.addHandler(handler)
60
60
def get_duplicity_log_level():
61
""" Get the current duplicity log level as a stdlib-compatible logging level"""
61
u""" Get the current duplicity log level as a stdlib-compatible logging level"""
62
62
duplicity_log_level = log.LevelName(log.getverbosity())
64
64
# notice is a duplicity-specific logging level not supported by stdlib
65
if duplicity_log_level == 'NOTICE':
66
duplicity_log_level = 'INFO'
65
if duplicity_log_level == u'NOTICE':
66
duplicity_log_level = u'INFO'
68
68
return duplicity_log_level
71
71
class JottaCloudBackend(duplicity.backend.Backend):
72
"""Connect to remote store using JottaCloud API"""
72
u"""Connect to remote store using JottaCloud API"""
74
74
def __init__(self, parsed_url):
75
75
duplicity.backend.Backend.__init__(self, parsed_url)
79
79
from jottalib import JFS
80
80
from jottalib.JFS import JFSNotFoundError, JFSIncompleteFile
81
81
except ImportError:
82
raise BackendException('JottaCloud backend requires jottalib'
83
' (see https://pypi.python.org/pypi/jottalib).')
82
raise BackendException(u'JottaCloud backend requires jottalib'
83
u' (see https://pypi.python.org/pypi/jottalib).')
85
85
# Set jottalib loggers to the same verbosity as duplicity
86
86
duplicity_log_level = get_duplicity_log_level()
92
92
# Will fetch jottacloud auth from environment or .netrc
93
93
self.client = JFS.JFS()
95
self.folder = self.get_or_create_directory(parsed_url.path.lstrip('/'))
96
log.Debug("Jottacloud folder for duplicity: %r" % self.folder.path)
95
self.folder = self.get_or_create_directory(parsed_url.path.lstrip(u'/'))
96
log.Debug(u"Jottacloud folder for duplicity: %r" % self.folder.path)
98
98
def get_or_create_directory(self, directory_name):
99
99
root_directory = get_root_dir(self.client)
107
107
# - Upload one file
108
108
# - Retried if an exception is thrown
109
109
resp = self.folder.up(source_path.open(), remote_filename)
110
log.Debug('jottacloud.put(%s,%s): %s' % (source_path.name, remote_filename, resp))
110
log.Debug(u'jottacloud.put(%s,%s): %s' % (source_path.name, remote_filename, resp))
112
112
def _get(self, remote_filename, local_path):
114
114
# - Retried if an exception is thrown
115
115
remote_file = self.client.getObject(posixpath.join(self.folder.path, remote_filename))
116
log.Debug('jottacloud.get(%s,%s): %s' % (remote_filename, local_path.name, remote_file))
117
with open(local_path.name, 'wb') as to_file:
116
log.Debug(u'jottacloud.get(%s,%s): %s' % (remote_filename, local_path.name, remote_file))
117
with open(local_path.name, u'wb') as to_file:
118
118
for chunk in remote_file.stream():
119
119
to_file.write(chunk)
123
123
# - Return a list of filenames
124
124
# - Retried if an exception is thrown
125
125
return list([f.name for f in self.folder.files()
126
if not f.is_deleted() and f.state != 'INCOMPLETE'])
126
if not f.is_deleted() and f.state != u'INCOMPLETE'])
128
128
def _delete(self, filename):
129
129
# - Delete one file
130
130
# - Retried if an exception is thrown
131
131
remote_path = posixpath.join(self.folder.path, filename)
132
132
remote_file = self.client.getObject(remote_path)
133
log.Debug('jottacloud.delete deleting: %s (%s)' % (remote_file, type(remote_file)))
133
log.Debug(u'jottacloud.delete deleting: %s (%s)' % (remote_file, type(remote_file)))
134
134
remote_file.delete()
136
136
def _query(self, filename):
137
"""Get size of filename"""
137
u"""Get size of filename"""
138
138
# - Query metadata of one file
139
139
# - Return a dict with a 'size' key, and a file size value (-1 for not found)
140
140
# - Retried if an exception is thrown
141
log.Info('Querying size of %s' % filename)
141
log.Info(u'Querying size of %s' % filename)
142
142
remote_path = posixpath.join(self.folder.path, filename)
144
144
remote_file = self.client.getObject(remote_path)
145
145
except JFSNotFoundError:
148
'size': remote_file.size,
148
u'size': remote_file.size,
151
151
def _close(self):
156
duplicity.backend.register_backend("jottacloud", JottaCloudBackend)
157
""" jottacloud is a Norwegian backup company """
156
duplicity.backend.register_backend(u"jottacloud", JottaCloudBackend)
157
u""" jottacloud is a Norwegian backup company """