4 from twisted.python import usage
5 from allmydata.util.encodingutil import unicode_to_url, quote_output
6 from allmydata.util.assertutil import precondition
9 # unit tests can override these to point at StringIO instances
15 ["quiet", "q", "Operate silently."],
16 ["version", "V", "Display version numbers and exit."],
17 ["version-and-path", None, "Display version numbers and paths to their locations and exit."],
20 def opt_version(self):
22 print >>self.stdout, allmydata.get_package_versions_string()
25 def opt_version_and_path(self):
27 print >>self.stdout, allmydata.get_package_versions_string(show_paths=True)
33 ["multiple", "m", "allow multiple basedirs to be specified at once"],
36 def postOptions(self):
38 raise usage.UsageError("<basedir> parameter is required")
41 self['basedirs'] = [os.path.abspath(os.path.expanduser(b)) for b in self.basedirs]
43 def parseArgs(self, *args):
46 precondition(isinstance(self['basedir'], (str, unicode)), self['basedir'])
47 self.basedirs.append(self['basedir'])
49 precondition(not [x for x in args if not isinstance(x, (str, unicode))], args)
50 self.basedirs.extend(args)
52 if len(args) == 0 and not self.basedirs:
53 if sys.platform == 'win32':
54 from allmydata.windows import registry
55 rbdp = registry.get_base_dir_path()
57 precondition(isinstance(registry.get_base_dir_path(), (str, unicode)), registry.get_base_dir_path())
58 self.basedirs.append(rbdp)
60 precondition(isinstance(os.path.expanduser("~/.tahoe"), (str, unicode)), os.path.expanduser("~/.tahoe"))
61 self.basedirs.append(os.path.expanduser("~/.tahoe"))
63 precondition(isinstance(args[0], (str, unicode)), args[0])
64 self.basedirs.append(args[0])
66 raise usage.UsageError("I wasn't expecting so many arguments")
68 class NoDefaultBasedirMixin(BasedirMixin):
69 def parseArgs(self, *args):
70 # create-client won't default to --basedir=~/.tahoe
73 precondition(isinstance(self['basedir'], (str, unicode)), self['basedir'])
74 self.basedirs.append(self['basedir'])
76 precondition(not [x for x in args if not isinstance(x, (str, unicode))], args)
77 self.basedirs.extend(args)
80 precondition(isinstance(args[0], (str, unicode)), args[0])
81 self.basedirs.append(args[0])
83 raise usage.UsageError("I wasn't expecting so many arguments")
85 raise usage.UsageError("--basedir must be provided")
87 DEFAULT_ALIAS = u"tahoe"
90 def get_aliases(nodedir):
91 from allmydata import uri
93 aliasfile = os.path.join(nodedir, "private", "aliases")
94 rootfile = os.path.join(nodedir, "private", "root_dir.cap")
96 f = open(rootfile, "r")
97 rootcap = f.read().strip()
99 aliases[u"tahoe"] = uri.from_string_dirnode(rootcap).to_string()
100 except EnvironmentError:
103 f = codecs.open(aliasfile, "r", "utf-8")
104 for line in f.readlines():
106 if line.startswith("#") or not line:
108 name, cap = line.split(u":", 1)
109 # normalize it: remove http: prefix, urldecode
110 cap = cap.strip().encode('utf-8')
111 aliases[name] = uri.from_string_dirnode(cap).to_string()
112 except EnvironmentError:
116 class DefaultAliasMarker:
119 pretend_platform_uses_lettercolon = False # for tests
120 def platform_uses_lettercolon_drivename():
121 if ("win32" in sys.platform.lower()
122 or "cygwin" in sys.platform.lower()
123 or pretend_platform_uses_lettercolon):
128 class TahoeError(Exception):
129 def __init__(self, msg):
130 Exception.__init__(self, msg)
133 def display(self, err):
134 print >>err, self.msg
137 class UnknownAliasError(TahoeError):
138 def __init__(self, msg):
139 TahoeError.__init__(self, "error: " + msg)
142 def get_alias(aliases, path_unicode, default):
144 Transform u"work:path/filename" into (aliases[u"work"], u"path/filename".encode('utf-8')).
145 If default=None, then an empty alias is indicated by returning
146 DefaultAliasMarker. We special-case strings with a recognized cap URI
147 prefix, to make it easy to access specific files/directories by their
149 If the transformed alias is either not found in aliases, or is blank
150 and default is not found in aliases, an UnknownAliasError is
153 precondition(isinstance(path_unicode, unicode), path_unicode)
155 from allmydata import uri
156 path = path_unicode.encode('utf-8').strip(" ")
157 if uri.has_uri_prefix(path):
158 # We used to require "URI:blah:./foo" in order to get a subpath,
159 # stripping out the ":./" sequence. We still allow that for compatibility,
160 # but now also allow just "URI:blah/foo".
161 sep = path.find(":./")
163 return path[:sep], path[sep+3:]
166 return path[:sep], path[sep+1:]
168 colon = path.find(":")
172 return DefaultAliasMarker, path
173 if default not in aliases:
174 raise UnknownAliasError("No alias specified, and the default "
175 "'tahoe' alias doesn't exist. To create "
176 "it, use 'tahoe create-alias tahoe'.")
177 return aliases[default], path
178 if colon == 1 and default is None and platform_uses_lettercolon_drivename():
179 # treat C:\why\must\windows\be\so\weird as a local path, not a tahoe
180 # file in the "C:" alias
181 return DefaultAliasMarker, path
183 # decoding must succeed because path is valid UTF-8 and colon & space are ASCII
184 alias = path[:colon].decode('utf-8')
186 # no alias, but there's a colon in a dirname/filename, like
189 return DefaultAliasMarker, path
190 if default not in aliases:
191 raise UnknownAliasError("No alias specified, and the default "
192 "'tahoe' alias doesn't exist. To create "
193 "it, use 'tahoe create-alias tahoe'.")
194 return aliases[default], path
195 if alias not in aliases:
196 raise UnknownAliasError("Unknown alias %s, please create it with 'tahoe add-alias' or 'tahoe create-alias'." %
198 return aliases[alias], path[colon+1:]
200 def escape_path(path):
201 segments = path.split("/")
202 return "/".join([urllib.quote(unicode_to_url(s)) for s in segments])