tahoe-lafs/mac/fuseparts/subbedopts.py

272 lines
8.2 KiB
Python
Raw Normal View History

macfuse: another tahoe fuse implementation This is the result of various experimentation done into using python-fuse to provide access to tahoe on the mac. It's rough in quite a few places, and is really the result of investigation more than a thorough implemenation of the fuse api. upon launch, it looks for the users root_dir by opening ~/.tahoe/node.url and ~/.tahoe/private/root_dir.cap it then proceeds to cache the directory structure found by walking the users tahoe drive (safely in the face of directory loops) into memory and then mounts that filesystem. when a file is read, it calls the tahoe node to first download the file into a cache directory (~/.tahoe/_cache) and then serves up the file from there. when a file is written, a temporary file is allocated within the tmp dir of the cache, and upon close() (specifically upon release()) the file is uploaded to the tahoe node, and the new directory entry written. note that while the durectory structure is cached into memory only when the filesystem is mounted, that it is 'write through' i.e. changes made via fuse are reflected into the underlying tahoe fs, even though changes made to the tahoe fs otherwise show up only upon restart. in addition to opening files for read and write, the mkdir() and rename() calls are supported. most other file system operations are not yet supported. notably stat() metadata is not currently tracked by tahoe, and is variably reported by this fs depending on write cache files. also note that this version does not fully support Finder. access through normal unix commands such as cat, cp, mv, ls etc works fine, and read access to file from within finder (including preview images and double- click to open) work ok. but copies to the tahoe drive from within finder may or may not succeed, but will always report an error. This is still under investigation. also note that this does not include any build integration. the included _fusemodule.so was built on mac os 10.4 against macfuse 1.3.0, and is known to not work against 10.5-1.3.1 it's possible it may also contain dependencies upon parts of macports used to build the python that it was built against. this will be cleaned up later. usage: python tahoefuse.py /Path/to/choice/of/mountpoint or optionally python tahoefuse.py -ovolicon=/Path/to/icon.icns /Path/to/mountpoint upon startup, tahoefuse will walk the tahoe directory, then print a summary of files and folders found, and then daemonise itself. to exit, either eject the 'drive' (note: 10.5 doesn't show it as a drive, since it considers fuse to be a connected server instead) or unmount it via umount /Path/to/mountpoint etc.
2008-02-15 01:35:10 +00:00
#
# Copyright (C) 2006 Csaba Henk <csaba.henk@creo.hu>
#
# This program can be distributed under the terms of the GNU LGPL.
# See the file COPYING.
#
# On 2009-09-21 Csaba Henk granted permission for this file to be
# licensed under the same terms as Tahoe-LAFS itself.
#
macfuse: another tahoe fuse implementation This is the result of various experimentation done into using python-fuse to provide access to tahoe on the mac. It's rough in quite a few places, and is really the result of investigation more than a thorough implemenation of the fuse api. upon launch, it looks for the users root_dir by opening ~/.tahoe/node.url and ~/.tahoe/private/root_dir.cap it then proceeds to cache the directory structure found by walking the users tahoe drive (safely in the face of directory loops) into memory and then mounts that filesystem. when a file is read, it calls the tahoe node to first download the file into a cache directory (~/.tahoe/_cache) and then serves up the file from there. when a file is written, a temporary file is allocated within the tmp dir of the cache, and upon close() (specifically upon release()) the file is uploaded to the tahoe node, and the new directory entry written. note that while the durectory structure is cached into memory only when the filesystem is mounted, that it is 'write through' i.e. changes made via fuse are reflected into the underlying tahoe fs, even though changes made to the tahoe fs otherwise show up only upon restart. in addition to opening files for read and write, the mkdir() and rename() calls are supported. most other file system operations are not yet supported. notably stat() metadata is not currently tracked by tahoe, and is variably reported by this fs depending on write cache files. also note that this version does not fully support Finder. access through normal unix commands such as cat, cp, mv, ls etc works fine, and read access to file from within finder (including preview images and double- click to open) work ok. but copies to the tahoe drive from within finder may or may not succeed, but will always report an error. This is still under investigation. also note that this does not include any build integration. the included _fusemodule.so was built on mac os 10.4 against macfuse 1.3.0, and is known to not work against 10.5-1.3.1 it's possible it may also contain dependencies upon parts of macports used to build the python that it was built against. this will be cleaned up later. usage: python tahoefuse.py /Path/to/choice/of/mountpoint or optionally python tahoefuse.py -ovolicon=/Path/to/icon.icns /Path/to/mountpoint upon startup, tahoefuse will walk the tahoe directory, then print a summary of files and folders found, and then daemonise itself. to exit, either eject the 'drive' (note: 10.5 doesn't show it as a drive, since it considers fuse to be a connected server instead) or unmount it via umount /Path/to/mountpoint etc.
2008-02-15 01:35:10 +00:00
from optparse import Option, OptionParser, OptParseError, OptionConflictError
from optparse import HelpFormatter, IndentedHelpFormatter, SUPPRESS_HELP
from fuseparts.setcompatwrap import set
##########
###
### Generic suboption parsing stuff.
###
##########
class SubOptsHive(object):
"""
Class for collecting unhandled suboptions.
"""
def __init__(self):
self.optlist = set()
self.optdict = {}
def _str_core(self):
sa = []
for k, v in self.optdict.iteritems():
sa.append(str(k) + '=' + str(v))
ra = (list(self.optlist) + sa) or ["(none)"]
ra.sort()
return ra
def __str__(self):
return "< opts: " + ", ".join(self._str_core()) + " >"
def canonify(self):
"""
Transform self to an equivalent canonical form:
delete optdict keys with False value, move optdict keys
with True value to optlist, stringify other values.
"""
for k, v in self.optdict.iteritems():
if v == False:
self.optdict.pop(k)
elif v == True:
self.optdict.pop(k)
self.optlist.add(v)
else:
self.optdict[k] = str(v)
def filter(self, other):
"""
Throw away those options which are not in the other one.
Returns a new instance with the rejected options.
"""
self.canonify()
other.canonify()
rej = self.__class__()
rej.optlist = self.optlist.difference(other.optlist)
self.optlist.difference_update(rej.optlist)
for x in self.optdict.copy():
if x not in other.optdict:
self.optdict.pop(x)
rej.optdict[x] = None
return rej
def add(self, opt, val=None):
"""Add a suboption."""
ov = opt.split('=', 1)
o = ov[0]
v = len(ov) > 1 and ov[1] or None
if (v):
if val != None:
raise AttributeError, "ambiguous option value"
val = v
if val == False:
return
if val in (None, True):
self.optlist.add(o)
else:
self.optdict[o] = val
class SubbedOpt(Option):
"""
`Option` derivative enhanced with the attribute of being a suboption of
some other option (like ``foo`` and ``bar`` for ``-o`` in ``-o foo,bar``).
"""
ATTRS = Option.ATTRS + ["subopt", "subsep", "subopts_hive"]
ACTIONS = Option.ACTIONS + ("store_hive",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("store_hive",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("store_hive",)
def __init__(self, *opts, **attrs):
self.subopt_map = {}
if "subopt" in attrs:
self._short_opts = []
self._long_opts = []
self._set_opt_strings(opts)
self.baseopt = self._short_opts[0] or self._long_opts[0]
opts = ()
Option.__init__(self, *opts, **attrs)
def __str__(self):
pf = ""
if hasattr(self, "subopt") and self.subopt:
pf = " %s...,%s,..." % (self.baseopt, self.subopt)
return Option.__str__(self) + pf
def _check_opt_strings(self, opts):
return opts
def _check_dest(self):
try:
Option._check_dest(self)
except IndexError:
if self.subopt:
self.dest = "__%s__%s" % (self.baseopt, self.subopt)
self.dest = self.dest.replace("-", "")
else:
raise
def get_opt_string(self):
if hasattr(self, 'subopt'):
return self.subopt
else:
return Option.get_opt_string(self)
def take_action(self, action, dest, opt, value, values, parser):
if action == "store_hive":
if not hasattr(values, dest) or getattr(values, dest) == None:
if hasattr(self, "subopts_hive") and self.subopts_hive:
hive = self.subopts_hive
else:
hive = parser.hive_class()
setattr(values, dest, hive)
for o in value.split(self.subsep or ","):
oo = o.split('=')
ok = oo[0]
ov = None
if (len(oo) > 1):
ov = oo[1]
if ok in self.subopt_map:
self.subopt_map[ok].process(ok, ov, values, parser)
else:
getattr(values, dest).add(*oo)
return
Option.take_action(self, action, dest, opt, value, values, parser)
def register_sub(self, o):
"""Register argument a suboption for `self`."""
if o.subopt in self.subopt_map:
raise OptionConflictError(
"conflicting suboption handlers for `%s'" % o.subopt,
o)
self.subopt_map[o.subopt] = o
CHECK_METHODS = []
for m in Option.CHECK_METHODS:
#if not m == Option._check_dest:
if not m.__name__ == '_check_dest':
CHECK_METHODS.append(m)
CHECK_METHODS.append(_check_dest)
class SubbedOptFormatter(HelpFormatter):
def format_option_strings(self, option):
if hasattr(option, "subopt") and option.subopt:
res = '-o ' + option.subopt
if option.takes_value():
res += "="
res += option.metavar or 'FOO'
return res
return HelpFormatter.format_option_strings(self, option)
class SubbedOptIndentedFormatter(IndentedHelpFormatter, SubbedOptFormatter):
def format_option_strings(self, option):
return SubbedOptFormatter.format_option_strings(self, option)
class SubbedOptParse(OptionParser):
"""
This class alters / enhances `OptionParser` with *suboption handlers*.
That is, calling `sop.add_option('-x', subopt=foo)` installs a handler
which will be triggered if there is ``-x foo`` in the command line being
parsed (or, eg., ``-x foo,bar``).
Moreover, ``-x`` implicitly gets a handler which collects the unhandled
suboptions of ``-x`` into a `SubOptsHive` instance (accessible post festam
via the `x` attribute of the returned Values object). (The only exception
is when ``-x`` has *explicitly* been added with action ``store_hive``.
This opens up the possibility of customizing the ``-x`` handler at some
rate.)
Suboption handlers have all the nice features of normal option handlers,
eg. they are displayed in the automatically generated help message
(and can have their own help info).
"""
def __init__(self, *args, **kw):
if not 'formatter' in kw:
kw['formatter'] = SubbedOptIndentedFormatter()
if not 'option_class' in kw:
kw['option_class'] = SubbedOpt
if 'hive_class' in kw:
self.hive_class = kw.pop('hive_class')
else:
self.hive_class = SubOptsHive
OptionParser.__init__(self, *args, **kw)
def add_option(self, *args, **kwargs):
if 'action' in kwargs and kwargs['action'] == 'store_hive':
if 'subopt' in kwargs:
raise OptParseError(
"""option can't have a `subopt' attr and `action="store_hive"' at the same time""")
if not 'type' in kwargs:
kwargs['type'] = 'string'
elif 'subopt' in kwargs:
o = self.option_class(*args, **kwargs)
oo = self.get_option(o.baseopt)
if oo:
if oo.action != "store_hive":
raise OptionConflictError(
"can't add subopt as option has already a handler that doesn't do `store_hive'",
oo)
else:
self.add_option(o.baseopt, action='store_hive',
metavar="sub1,[sub2,...]")
oo = self.get_option(o.baseopt)
oo.register_sub(o)
args = (o,)
kwargs = {}
return OptionParser.add_option(self, *args, **kwargs)