Skip to content

Commit

Permalink
Merge pull request #21 from mkouhei/devel
Browse files Browse the repository at this point in the history
release 0.8.3
  • Loading branch information
mkouhei committed Nov 15, 2014
2 parents 4382113 + 2252aa4 commit c6ffb8f
Show file tree
Hide file tree
Showing 24 changed files with 406 additions and 209 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
@@ -0,0 +1,3 @@
[run]
include =
backup2swift/*
12 changes: 12 additions & 0 deletions .gitignore
@@ -0,0 +1,12 @@
*.egg
*.egg/
*.egg-info/
*.pyc
.tox/
_build/
build
dist/
.coverage
.cache
htmlcov
dummy
1 change: 1 addition & 0 deletions .pycheckrc
@@ -0,0 +1 @@
moduleImportErrors = 0 # config.py:22, configparser import for Python2, 3
18 changes: 18 additions & 0 deletions .pylintrc
@@ -0,0 +1,18 @@
[MESSAGES CONTROL]
disable=
# mock argument,
unused-argument,
# configparser import,
import-error,
# unit test method,
too-many-public-methods,
# python3 sys.stdin.buffer,
maybe-no-member,
# decorator of mock,
arguments-differ,
# print for Python3 syntax,
superfluous-parens,
# for unit test,
duplicate-code,
# List Comprehensions for loop,
expression-not-assigned,
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,6 @@ python:
- "2.7"
env:
- TOX_ENV=py27
- TOX_ENV=py32
- TOX_ENV=py33
- TOX_ENV=py34
- TOX_ENV=pypy
Expand Down
2 changes: 1 addition & 1 deletion backup2swift/__init__.py
Expand Up @@ -15,4 +15,4 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__version__ = '0.8.2'
__version__ = '0.8.3'
111 changes: 71 additions & 40 deletions backup2swift/backup.py
Expand Up @@ -26,9 +26,40 @@


class Backup(object):
def __init__(self, auth_url, username, password, rotate_limit=ROTATE_LIMIT,
verify=True, tenant_id=None, container_name=utils.FQDN):
self.verify = verify
"""
Arguments:
auth_url: authentication url of swift
username: username for swift
password: password for swift
rotate_limit: limitation count of rotation
verify: verification of ssl certification
tenant_id: tenant id when using KeyStone
container_name: container name of swift
"""

def __init__(self, *args, **kwargs):
auth_url = args[0]
username = args[1]
password = args[2]
if kwargs.get('rotate_limit'):
rotate_limit = kwargs.get('rotate_limit')
else:
rotate_limit = ROTATE_LIMIT
if kwargs.get('verify'):
self.verify = kwargs.get('verify')
else:
self.verify = True

if kwargs.get('tenant_id'):
tenant_id = kwargs.get('tenant_id')
else:
tenant_id = None

if kwargs.get('container_name'):
container_name = kwargs.get('container_name')
else:
container_name = utils.FQDN

(self.token,
self.storage_url) = client.retrieve_token(auth_url,
username,
Expand Down Expand Up @@ -68,16 +99,16 @@ def backup_file(self, filename, data=None):
if not client.is_container(self.token, self.storage_url,
self.container_name, verify=self.verify):
# False is no container
rc = client.create_container(self.token,
self.storage_url,
self.container_name,
verify=self.verify)
if not (rc == 201 or rc == 202):
status_code = client.create_container(self.token,
self.storage_url,
self.container_name,
verify=self.verify)
if not (status_code == 201 or status_code == 202):
# 201; Created, 202; Accepted
raise RuntimeError('Failed to create the container "%s"'
% self.container_name, verify=self.verify)
% self.container_name)

objects_list = [object.get('name') for object in
objects_list = [obj.get('name') for obj in
client.list_objects(self.token,
self.storage_url,
self.container_name,
Expand All @@ -91,13 +122,13 @@ def backup_file(self, filename, data=None):
if object_name in objects_list:
self.rotate(filename, object_name, objects_list)
else:
rc = client.create_object(self.token,
self.storage_url,
self.container_name,
filename,
object_name=object_name,
verify=self.verify)
if not (rc == 201 or rc == 202):
status_code = client.create_object(self.token,
self.storage_url,
self.container_name,
filename,
object_name=object_name,
verify=self.verify)
if not (status_code == 201 or status_code == 202):
raise RuntimeError('Failed to create the object "%s"'
% object_name)
return True
Expand All @@ -113,18 +144,18 @@ def rotate(self, filename, object_name, objects_list):
# copy current object to new object
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
new_object_name = object_name + '_' + timestamp
rc = client.copy_object(self.token, self.storage_url,
self.container_name, object_name,
new_object_name, verify=self.verify)
if rc != 201:
status_code = client.copy_object(self.token, self.storage_url,
self.container_name, object_name,
new_object_name, verify=self.verify)
if status_code != 201:
raise RuntimeError('Failed to copy object "%s"' % new_object_name)

# create new object
rc = client.create_object(self.token, self.storage_url,
self.container_name, filename,
object_name=object_name,
verify=self.verify)
if rc != 201:
status_code = client.create_object(self.token, self.storage_url,
self.container_name, filename,
object_name=object_name,
verify=self.verify)
if status_code != 201:
raise RuntimeError('Failed to create the object "%s"'
% object_name)

Expand Down Expand Up @@ -178,12 +209,12 @@ def retrieve_backup_data(self, object_name, output_filepath=None):
client.is_object(self.token, self.storage_url,
self.container_name, object_name,
verify=self.verify)):
rc, content = client.retrieve_object(self.token,
self.storage_url,
self.container_name,
object_name,
verify=self.verify)
if not rc:
status_code, content = client.retrieve_object(self.token,
self.storage_url,
self.container_name,
object_name,
verify=self.verify)
if not status_code:
raise RuntimeError('Failed to retrieve the object "%s"'
% object_name)
if output_filepath:
Expand All @@ -198,8 +229,8 @@ def retrieve_backup_data(self, object_name, output_filepath=None):
mode = 'bw'
else:
mode = 'w'
with open(fpath, mode) as f:
f.write(content)
with open(fpath, mode) as _file:
_file.write(content)
else:
raise RuntimeError('No such object "%s"' % object_name)

Expand All @@ -218,12 +249,12 @@ def delete_backup_data(self, object_name):
client.is_object(self.token, self.storage_url,
self.container_name, object_name,
verify=self.verify)):
rc = client.delete_object(self.token,
self.storage_url,
self.container_name,
object_name,
verify=self.verify)
if not rc == 204:
status_code = client.delete_object(self.token,
self.storage_url,
self.container_name,
object_name,
verify=self.verify)
if not status_code == 204:
raise RuntimeError('Failed to delete the object "%s"'
% object_name)
return True
Expand Down
22 changes: 12 additions & 10 deletions backup2swift/command.py
Expand Up @@ -25,6 +25,7 @@


def parse_options():
""" setup options. """
parser = argparse.ArgumentParser(description='usage')
setoption(parser, 'version')
setoption(parser, 'config')
Expand All @@ -36,7 +37,6 @@ def parse_options():

def setoption(parser, keyword):
"""
Arguments:
parser: object of argparse
keyword: switching keyword
Expand Down Expand Up @@ -102,33 +102,35 @@ def execute_swift_client(args):
container_name = args.container
else:
container_name = utils.FQDN
b = backup.Backup(auth_url, username, password, rotate_limit,
verify=verify, tenant_id=tenant_id,
container_name=container_name)
bkup = backup.Backup(auth_url, username, password,
rotate_limit=rotate_limit,
verify=verify, tenant_id=tenant_id,
container_name=container_name)
if args.list:
# listing backup data
backup_l = b.retrieve_backup_data_list(args.verbose)
backup_l = bkup.retrieve_backup_data_list(args.verbose)
utils.list_data(backup_l)
elif args.path:
# backup data to swift
b.backup(args.path)
bkup.backup(args.path)
elif args.stdin:
# backup via stdin pipe
if sys.version_info > (3, 0):
# for python3
b.backup_file(args.stdin, data=sys.stdin.buffer.raw)
bkup.backup_file(args.stdin, data=sys.stdin.buffer.raw)
else:
# for python2
b.backup_file(args.stdin, data=sys.stdin)
bkup.backup_file(args.stdin, data=sys.stdin)
elif args.retrieve:
# retrive backup data
b.retrieve_backup_data(args.retrieve, args.output)
bkup.retrieve_backup_data(args.retrieve, args.output)
elif args.delete:
# delete backup data
b.delete_backup_data(args.delete)
bkup.delete_backup_data(args.delete)


def main():
""" main function """
try:
args = parse_options()
args.func(args)
Expand Down
1 change: 1 addition & 0 deletions backup2swift/tests/__init__.py
@@ -0,0 +1 @@
""" test modules for backup2swift """
3 changes: 3 additions & 0 deletions backup2swift/tests/test__init__.py
Expand Up @@ -20,9 +20,12 @@


class InitTests(unittest.TestCase):
""" unit test for __init__ """

def test__version__(self):
""" check __version__ """
self.assertTrue(b.__version__)

def test__name__(self):
""" check __name__ """
self.assertTrue(b.__name__)

0 comments on commit c6ffb8f

Please sign in to comment.