]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - contrib/fuse/impl_b/pyfuse/tahoe.py
189298e47e5a36deb0bb5baf4a81a4175030c439
[tahoe-lafs/tahoe-lafs.git] / contrib / fuse / impl_b / pyfuse / tahoe.py
1 """
2 PyFuse client for the Tahoe distributed file system.
3 See http://allmydata.org/
4 """
5
6 # Read-only for now.
7
8 # Portions copied from the file contrib/fuse/tahoe_fuse.py distributed
9 # with Tahoe 1.0.0.
10
11 import os, sys
12 from objectfs import ObjectFs
13 from handler import Handler
14 import simplejson
15 import urllib
16
17
18 ### Config:
19 TahoeConfigDir = '~/.tahoe'
20
21
22 ### Utilities for debug:
23 def log(msg, *args):
24     print msg % args
25
26
27 class TahoeConnection:
28     def __init__(self, confdir):
29         self.confdir = confdir
30         self._init_url()
31
32     def _init_url(self):
33         if os.path.exists(os.path.join(self.confdir, 'node.url')):
34             self.url = file(os.path.join(self.confdir, 'node.url'), 'rb').read().strip()
35             if not self.url.endswith('/'):
36                 self.url += '/'
37         else:
38             f = open(os.path.join(self.confdir, 'webport'), 'r')
39             contents = f.read()
40             f.close()
41             fields = contents.split(':')
42             proto, port = fields[:2]
43             assert proto == 'tcp'
44             port = int(port)
45             self.url = 'http://localhost:%d/' % (port,)
46
47     def get_root(self):
48         # For now we just use the same default as the CLI:
49         rootdirfn = os.path.join(self.confdir, 'private', 'root_dir.cap')
50         f = open(rootdirfn, 'r')
51         cap = f.read().strip()
52         f.close()
53         return TahoeDir(self, canonicalize_cap(cap))
54
55
56 class TahoeNode:
57     def __init__(self, conn, uri):
58         self.conn = conn
59         self.uri = uri
60
61     def get_metadata(self):
62         f = self._open('?t=json')
63         json = f.read()
64         f.close()
65         return simplejson.loads(json)
66
67     def _open(self, postfix=''):
68         url = '%suri/%s%s' % (self.conn.url, self.uri, postfix)
69         log('*** Fetching: %r', url)
70         return urllib.urlopen(url)
71
72
73 class TahoeDir(TahoeNode):
74     def listdir(self):
75         flag, md = self.get_metadata()
76         assert flag == 'dirnode'
77         result = []
78         for name, (childflag, childmd) in md['children'].items():
79             if childflag == 'dirnode':
80                 cls = TahoeDir
81             else:
82                 cls = TahoeFile
83             result.append((str(name), cls(self.conn, childmd['ro_uri'])))
84         return result
85
86 class TahoeFile(TahoeNode):
87     def size(self):
88         rawsize = self.get_metadata()[1]['size']
89         return rawsize
90
91     def read(self):
92         return self._open().read()
93
94
95 def canonicalize_cap(cap):
96     cap = urllib.unquote(cap)
97     i = cap.find('URI:')
98     assert i != -1, 'A cap must contain "URI:...", but this does not: ' + cap
99     return cap[i:]
100
101 def main(mountpoint, basedir):
102     conn = TahoeConnection(basedir)
103     root = conn.get_root()
104     handler = Handler(mountpoint, ObjectFs(root))
105     handler.loop_forever()
106
107 if __name__ == '__main__':
108     basedir = os.path.expanduser(TahoeConfigDir)
109     for i, arg in enumerate(sys.argv):
110         if arg == '--basedir':
111             basedir = sys.argv[i+1]
112             sys.argv[i:i+2] = []
113
114     [mountpoint] = sys.argv[1:]
115     main(mountpoint, basedir)