3 from twisted.python import usage
7 # unit tests can override these to point at StringIO instances
13 ["quiet", "q", "Operate silently."],
14 ["version", "V", "Display version numbers and exit."],
15 ["version-and-path", None, "Display version numbers and paths to their locations and exit."],
18 def opt_version(self):
20 print allmydata.get_package_versions_string()
23 def opt_version_and_path(self):
25 print allmydata.get_package_versions_string(show_paths=True)
31 ["multiple", "m", "allow multiple basedirs to be specified at once"],
34 def postOptions(self):
36 raise usage.UsageError("<basedir> parameter is required")
39 self['basedirs'] = [os.path.abspath(os.path.expanduser(b)) for b in self.basedirs]
41 def parseArgs(self, *args):
42 from allmydata.util.assertutil import precondition
45 precondition(isinstance(self['basedir'], (str, unicode)), self['basedir'])
46 self.basedirs.append(self['basedir'])
48 precondition(not [x for x in args if not isinstance(x, (str, unicode))], args)
49 self.basedirs.extend(args)
51 if len(args) == 0 and not self.basedirs:
52 if sys.platform == 'win32':
53 from allmydata.windows import registry
54 rbdp = registry.get_base_dir_path()
56 precondition(isinstance(registry.get_base_dir_path(), (str, unicode)), registry.get_base_dir_path())
57 self.basedirs.append(rbdp)
59 precondition(isinstance(os.path.expanduser("~/.tahoe"), (str, unicode)), os.path.expanduser("~/.tahoe"))
60 self.basedirs.append(os.path.expanduser("~/.tahoe"))
62 precondition(isinstance(args[0], (str, unicode)), args[0])
63 self.basedirs.append(args[0])
65 raise usage.UsageError("I wasn't expecting so many arguments")
67 class NoDefaultBasedirMixin(BasedirMixin):
68 def parseArgs(self, *args):
69 from allmydata.util.assertutil import precondition
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 = "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["tahoe"] = uri.from_string_dirnode(rootcap).to_string()
100 except EnvironmentError:
103 f = open(aliasfile, "r")
104 for line in f.readlines():
106 if line.startswith("#") or not line:
108 name, cap = line.split(":", 1)
109 # normalize it: remove http: prefix, urldecode
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):
127 class UnknownAliasError(Exception):
130 def get_alias(aliases, path, default):
131 from allmydata import uri
132 # transform "work:path/filename" into (aliases["work"], "path/filename").
133 # If default=None, then an empty alias is indicated by returning
134 # DefaultAliasMarker. We special-case strings with a recognized cap URI
135 # prefix, to make it easy to access specific files/directories by their
137 # If the transformed alias is either not found in aliases, or is blank
138 # and default is not found in aliases, an UnknownAliasError is
141 if uri.has_uri_prefix(path):
142 # The only way to get a sub-path is to use URI:blah:./foo, and we
143 # strip out the :./ sequence.
144 sep = path.find(":./")
146 return path[:sep], path[sep+3:]
148 colon = path.find(":")
152 return DefaultAliasMarker, path
153 if default not in aliases:
154 raise UnknownAliasError("No alias specified, and the default "
155 "'tahoe' alias doesn't exist. To create "
156 "it, use 'tahoe create-alias tahoe'.")
157 return aliases[default], path
158 if colon == 1 and default == None and platform_uses_lettercolon_drivename():
159 # treat C:\why\must\windows\be\so\weird as a local path, not a tahoe
160 # file in the "C:" alias
161 return DefaultAliasMarker, path
164 # no alias, but there's a colon in a dirname/filename, like
167 return DefaultAliasMarker, path
168 if default not in aliases:
169 raise UnknownAliasError("No alias specified, and the default "
170 "'tahoe' alias doesn't exist. To create "
171 "it, use 'tahoe create-alias tahoe'.")
172 return aliases[default], path
173 if alias not in aliases:
174 raise UnknownAliasError("Unknown alias '%s', please create it with 'tahoe add-alias' or 'tahoe create-alias'." % alias)
175 return aliases[alias], path[colon+1:]
177 def escape_path(path):
178 segments = path.split("/")
179 return "/".join([urllib.quote(s) for s in segments])