X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=python%2Fcumulus%2Fstore%2Ffile.py;h=83044017ecf497dc6ea06e1fdf6ac354d80dc0da;hb=197d9dca9416c09c7e79e56b88b9eb53c26d30d7;hp=6034835c276ee15d7ae8bcd1f9398a7e56578011;hpb=f51789bfa9d0f6f9826b72f3c32ff89307ec3dc6;p=cumulus.git diff --git a/python/cumulus/store/file.py b/python/cumulus/store/file.py index 6034835..8304401 100644 --- a/python/cumulus/store/file.py +++ b/python/cumulus/store/file.py @@ -1,3 +1,21 @@ +# Cumulus: Efficient Filesystem Backup to the Cloud +# Copyright (C) 2008-2009 The Cumulus Developers +# See the AUTHORS file for a list of contributors. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + import os, sys, tempfile import cumulus.store @@ -5,33 +23,42 @@ import cumulus.store type_patterns = cumulus.store.type_patterns class FileStore(cumulus.store.Store): - def __init__(self, prefix): - while prefix.endswith("/") and prefix != "/": prefix = prefix[:-1] - self.prefix = prefix + def __init__(self, url, **kw): + # if constructor isn't called via factory interpret url as filename + if not hasattr (self, 'path'): + self.path = url + self.prefix = self.path.rstrip("/") def _get_path(self, type, name): - return "%s/%s" % (self.prefix, name) + return os.path.join(self.prefix, type, name) - def list(self, type): - files = os.listdir(self.prefix) - return (f for f in files if type_patterns[type].match(f)) + def list(self, subdir): + try: + return os.listdir(os.path.join(self.prefix, subdir)) + except OSError: + raise cumulus.store.NotFoundError(subdir) - def get(self, type, name): - k = self._get_path(type, name) - return open(k, 'rb') + def get(self, path): + try: + return open(os.path.join(self.prefix, path), 'rb') + except IOError: + raise cumulus.store.NotFoundError(path) - def put(self, type, name, fp): - k = self._get_path(type, name) - out = open(k, 'wb') + def put(self, path, fp): + out = open(os.path.join(self.prefix, path), 'wb') buf = fp.read(4096) while len(buf) > 0: out.write(buf) buf = fp.read(4096) - def delete(self, type, name): - k = self._get_path(type, name) - os.unlink(k) + def delete(self, path): + os.unlink(os.path.join(self.prefix, path)) + + def stat(self, path): + try: + stat = os.stat(os.path.join(self.prefix, path)) + return {'size': stat.st_size} + except OSError: + raise cumulus.store.NotFoundError, path - def stat(self, type, name): - stat = os.stat(self._get_path(type, name)) - return {'size': stat.st_size} +Store = FileStore