Skip to content

Commit

Permalink
Normalize code formatting using yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
ppolewicz committed Oct 21, 2016
1 parent 7f9f93a commit 5250724
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 98 deletions.
29 changes: 24 additions & 5 deletions b2fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from b2fuse_main import B2Fuse


def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument("mountpoint", type=str, help="Mountpoint for the B2 bucket")
Expand All @@ -41,9 +42,24 @@ def create_parser():
parser.add_argument('--use_disk', dest='use_disk', action='store_true')
parser.set_defaults(use_disk=False)

parser.add_argument("--account_id", type=str, default=None, help="Account ID for your B2 account (overrides config)")
parser.add_argument("--application_key", type=str, default=None, help="Application key for your account (overrides config)")
parser.add_argument("--bucket_id", type=str, default=None, help="Bucket ID for the bucket to mount (overrides config)")
parser.add_argument(
"--account_id",
type=str,
default=None,
help="Account ID for your B2 account (overrides config)"
)
parser.add_argument(
"--application_key",
type=str,
default=None,
help="Application key for your account (overrides config)"
)
parser.add_argument(
"--bucket_id",
type=str,
default=None,
help="Bucket ID for the bucket to mount (overrides config)"
)

parser.add_argument("--memory_limit", type=int, default=128, help="Memory limit")
parser.add_argument("--temp_folder", type=str, default=".tmp/", help="Temporary file folder")
Expand All @@ -56,6 +72,7 @@ def load_config(config_filename):
with open(config_filename) as f:
return yaml.load(f.read())


if __name__ == '__main__':
logging.basicConfig(level=logging.WARNING, format="%(asctime)s:%(levelname)s:%(message)s")

Expand All @@ -81,7 +98,6 @@ def load_config(config_filename):
else:
config["enableHashfiles"] = False


if args.memory_limit:
config["memoryLimit"] = args.memory_limit

Expand All @@ -93,5 +109,8 @@ def load_config(config_filename):
else:
config["useDisk"] = False

with B2Fuse(config["accountId"], config["applicationKey"], config["bucketId"], config["enableHashfiles"], config["memoryLimit"], config["tempFolder"], config["useDisk"]) as filesystem:
with B2Fuse(
config["accountId"], config["applicationKey"], config["bucketId"],
config["enableHashfiles"], config["memoryLimit"], config["tempFolder"], config["useDisk"]
) as filesystem:
FUSE(filesystem, args.mountpoint, nothreads=True, foreground=True)
98 changes: 60 additions & 38 deletions b2fuse_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@

B2File = B2SequentialFileMemory


class B2Fuse(Operations):
def __init__(self, account_id, application_key, bucket_id, enable_hashfiles, memory_limit, temp_folder, use_disk ):
def __init__(
self, account_id, application_key, bucket_id, enable_hashfiles, memory_limit, temp_folder,
use_disk
):
account_info = InMemoryAccountInfo()
self.api = B2Api(account_info)
self.api.authorize_account('production', account_id, application_key)
self.bucket_api = CachedBucket(self.api, bucket_id)

self.logger = logging.getLogger("%s.%s" % (__name__,self.__class__.__name__))

self.logger = logging.getLogger("%s.%s" % (__name__, self.__class__.__name__))

self.enable_hashfiles = enable_hashfiles
self.memory_limit = memory_limit
Expand All @@ -76,8 +79,6 @@ def __enter__(self):
def __exit__(self, *args, **kwargs):
return



# Helper methods
# ==================

Expand All @@ -101,7 +102,7 @@ def _get_memory_consumption(self):

memory = sum(open_file_sizes)

return float(memory)/(1024*1024)
return float(memory) / (1024 * 1024)

def _get_cloud_space_consumption(self):

Expand All @@ -123,7 +124,6 @@ def _update_directory_structure(self):
online_files = self.bucket_api.list_file_names()['files']
self._directories.update_structure(online_files, self.local_directories)


def _remove_local_file(self, path, delete_online=True):
if path in self.open_files.keys():
self.open_files[path].delete(delete_online)
Expand All @@ -137,11 +137,9 @@ def _remove_start_slash(self, path):
path = path[1:]
return path


# Filesystem methods
# ==================


def access(self, path, mode):
self.logger.debug("Access %s (mode:%s)", path, mode)
path = self._remove_start_slash(path)
Expand All @@ -152,7 +150,7 @@ def access(self, path, mode):

#Return access granted if path is a file
if self._exists(path):
return
return

raise FuseOSError(errno.EACCES)

Expand All @@ -164,12 +162,18 @@ def access(self, path, mode):

def getattr(self, path, fh=None):
self.logger.debug("Get attr %s", path)
self.logger.debug("Memory used %s", round(self._get_memory_consumption(),2))
self.logger.debug("Memory used %s", round(self._get_memory_consumption(), 2))
path = self._remove_start_slash(path)

#Check if path is a directory
if self._directories.is_directory(path):
return dict(st_mode=(S_IFDIR | 0777), st_ctime=time(), st_mtime=time(), st_atime=time(), st_nlink=2)
return dict(
st_mode=(S_IFDIR | 0777),
st_ctime=time(),
st_mtime=time(),
st_atime=time(),
st_nlink=2
)

#Check if path is a file
elif self._exists(path):
Expand All @@ -179,23 +183,43 @@ def getattr(self, path, fh=None):
if path in online_files:
#print "File is in bucket"
file_info = self._directories.get_file_info(path)
return dict(st_mode=(S_IFREG | 0777), st_ctime=file_info['uploadTimestamp'], st_mtime=file_info['uploadTimestamp'], st_atime=file_info['uploadTimestamp'], st_nlink=1, st_size=file_info['size'])
return dict(
st_mode=(S_IFREG | 0777),
st_ctime=file_info['uploadTimestamp'],
st_mtime=file_info['uploadTimestamp'],
st_atime=file_info['uploadTimestamp'],
st_nlink=1,
st_size=file_info['size']
)

elif path.endswith(".sha1"):
#print "File is just a hash"
return dict(st_mode=(S_IFREG | 0444), st_ctime=0, st_mtime=0, st_atime=0, st_nlink=1, st_size=42)
return dict(
st_mode=(S_IFREG | 0444),
st_ctime=0,
st_mtime=0,
st_atime=0,
st_nlink=1,
st_size=42
)

else:
#print "File exists only locally"
return dict(st_mode=(S_IFREG | 0777), st_ctime=0, st_mtime=0, st_atime=0, st_nlink=1, st_size=len(self.open_files[path]))
return dict(
st_mode=(S_IFREG | 0777),
st_ctime=0,
st_mtime=0,
st_atime=0,
st_nlink=1,
st_size=len(self.open_files[path])
)

raise FuseOSError(errno.ENOENT)

def readdir(self, path, fh):
self.logger.debug("Readdir %s", path)
path = self._remove_start_slash(path)


self._update_directory_structure()

dirents = []
Expand All @@ -212,7 +236,6 @@ def in_folder(filename):

return False


#Add files found in bucket
directory = self._directories.get_directory(path)

Expand All @@ -237,7 +260,7 @@ def in_folder(filename):

#If filenames has a prefix (relative to path) remove this
if len(path) > 0:
dirents = map(lambda f: f[len(path)+1:], dirents)
dirents = map(lambda f: f[len(path) + 1:], dirents)

#Add hash files
if self.enable_hashfiles:
Expand All @@ -246,7 +269,7 @@ def in_folder(filename):

#Add directories
dirents.extend(['.', '..'])
dirents.extend(map(str,self._directories.get_directories(path)))
dirents.extend(map(str, self._directories.get_directories(path)))

return dirents

Expand Down Expand Up @@ -284,15 +307,17 @@ def in_folder(filename):
dirents.append(filename)

for filename in dirents:
online_files = [(f['fileName'], f['fileId']) for f in self.bucket_api.list_file_names()['files']]
online_files = [
(f['fileName'], f['fileId']) for f in self.bucket_api.list_file_names()['files']
]
fileName_to_fileId = dict(online_files)
self.api.delete_file_version(fileName_to_fileId[path], path)

self._remove_local_file(filename)

if self._directories.is_directory(path):
if path in self.local_directories:
i = self.local_directories.index(path)
i = self.local_directories.index(path)
self.local_directories.pop(i)

def mkdir(self, path, mode):
Expand All @@ -306,11 +331,15 @@ def mkdir(self, path, mode):
def statfs(self, path):
self.logger.debug("Fetching file system stats %s", path)
#Returns 1 petabyte free space, arbitrary number
block_size = 4096*16
total_block_count = 1024**4 #1 Petabyte
free_block_count = total_block_count - self._get_cloud_space_consumption()/block_size
return dict(f_bsize=block_size, f_blocks=total_block_count, f_bfree=free_block_count, f_bavail=free_block_count)

block_size = 4096 * 16
total_block_count = 1024**4 #1 Petabyte
free_block_count = total_block_count - self._get_cloud_space_consumption() / block_size
return dict(
f_bsize=block_size,
f_blocks=total_block_count,
f_bfree=free_block_count,
f_bavail=free_block_count
)

def unlink(self, path):
self.logger.debug("Unlink %s", path)
Expand All @@ -323,7 +352,6 @@ def unlink(self, path):

self._update_directory_structure()


def rename(self, old, new):
self.logger.debug("Rename old: %s, new %s", old, new)

Expand All @@ -336,18 +364,16 @@ def rename(self, old, new):
if self._exists(new):
self.unlink(new)


self.open(old,0)
self.open(old, 0)
self.open_files[old]
self.release(old,0)
self.release(old, 0)

self.create(new,0)
self.create(new, 0)
self.write(new, self.open_files[old], 0, 0)
self.release(new, 0)

self.unlink(old)


def utimens(self, path, times=None):
self.logger.debug("Utimens %s", path)

Expand Down Expand Up @@ -379,7 +405,7 @@ def create(self, path, mode, fi=None):
file_info = {}
file_info['fileName'] = path

self.open_files[path] = B2File(self, file_info, True) #array.array('c')
self.open_files[path] = B2File(self, file_info, True) #array.array('c')

self.fd += 1
return self.fd
Expand Down Expand Up @@ -413,10 +439,6 @@ def release(self, path, fh):
self.logger.debug("Release %s %s", path, fh)

self.logger.debug("Flushing file in case it was dirty")
self.flush(self._remove_start_slash(path),fh)
self.flush(self._remove_start_slash(path), fh)

self._remove_local_file(path, False)




12 changes: 6 additions & 6 deletions cached_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@

from b2.bucket import Bucket


#General cache used for B2Bucket
class Cache(object):
def __init__(self, cache_timeout):
self.data = {}

self.cache_timeout = cache_timeout

def update(self, result, params = ""):
def update(self, result, params=""):
self.data[params] = (time(), result)

def get(self, params = ""):
def get(self, params=""):
if self.data.get(params) is not None:
entry_time, result = self.data.get(params)
if time() - entry_time < self.cache_timeout:
Expand All @@ -47,9 +48,11 @@ def get(self, params = ""):

return


class CacheNotFound(BaseException):
pass


class CachedBucket(Bucket):
def __init__(self, api, bucket_id):
super(CachedBucket, self).__init__(api, bucket_id)
Expand Down Expand Up @@ -80,7 +83,7 @@ def list_file_names(self):
try:
return self._get_cache(func_name)
except CacheNotFound:
result = super(CachedBucket, self).list_file_names()
result = super(CachedBucket, self).list_file_names()
return self._update_cache(func_name, result)

def delete_file_version(self, *args, **kwargs):
Expand All @@ -90,6 +93,3 @@ def delete_file_version(self, *args, **kwargs):
def download_file_by_id(self, *args, **kwargs):
self._reset_cache()
return super(CachedBucket, self).download_file_by_id(*args, **kwargs)



6 changes: 5 additions & 1 deletion directory_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.


class Directory(object):
def __init__(self, name):
self._name = name
Expand Down Expand Up @@ -67,6 +68,7 @@ def get_content_names(self):

return directories + files


class DirectoryStructure(object):
def __init__(self):
self._directories = Directory("")
Expand All @@ -78,7 +80,9 @@ def update_structure(self, file_info_list, local_directories):
for directory in local_directories_split:
self._lookup(self._directories, directory, True)

online_directories_split = map(lambda file_info: file_info['fileName'].split("/")[:-1], file_info_list)
online_directories_split = map(
lambda file_info: file_info['fileName'].split("/")[:-1], file_info_list
)
for directory in online_directories_split:
self._lookup(self._directories, directory, True)

Expand Down
1 change: 0 additions & 1 deletion filetypes/B2BaseFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ def delete(self):

def upload(self):
raise NotImplemented()

Loading

0 comments on commit 5250724

Please sign in to comment.