def get_aliases(nodedir): from allmydata import uri aliases = {} aliasfile = os.path.join(nodedir, "private", "aliases") rootfile = os.path.join(nodedir, "private", "root_dir.cap") try: f = open(rootfile, "r") rootcap = f.read().strip() if rootcap: aliases[DEFAULT_ALIAS] = uri.from_string_dirnode(rootcap).to_string() except EnvironmentError: pass try: f = codecs.open(aliasfile, "r", "utf-8") for line in f.readlines(): line = line.strip() if line.startswith("#") or not line: continue name, cap = line.split(u":", 1) # normalize it: remove http: prefix, urldecode cap = cap.strip().encode("utf-8") aliases[name] = uri.from_string_dirnode(cap).to_string() except EnvironmentError: pass return aliases
def get_aliases(nodedir): from allmydata import uri aliases = {} aliasfile = os.path.join(nodedir, "private", "aliases") rootfile = os.path.join(nodedir, "private", "root_dir.cap") try: f = open(rootfile, "r") rootcap = f.read().strip() if rootcap: aliases[DEFAULT_ALIAS] = uri.from_string_dirnode( rootcap).to_string() except EnvironmentError: pass try: f = codecs.open(aliasfile, "r", "utf-8") for line in f.readlines(): line = line.strip() if line.startswith("#") or not line: continue name, cap = line.split(u":", 1) # normalize it: remove http: prefix, urldecode cap = cap.strip().encode('utf-8') aliases[name] = uri.from_string_dirnode(cap).to_string() except EnvironmentError: pass return aliases
def get_alias(aliases, path_unicode, default): """ Transform u"work:path/filename" into (aliases[u"work"], u"path/filename".encode('utf-8')). If default=None, then an empty alias is indicated by returning DefaultAliasMarker. We special-case strings with a recognized cap URI prefix, to make it easy to access specific files/directories by their caps. If the transformed alias is either not found in aliases, or is blank and default is not found in aliases, an UnknownAliasError is raised. """ precondition(isinstance(path_unicode, unicode), path_unicode) from allmydata import uri path = path_unicode.encode('utf-8').strip(" ") if uri.has_uri_prefix(path): # We used to require "URI:blah:./foo" in order to get a subpath, # stripping out the ":./" sequence. We still allow that for compatibility, # but now also allow just "URI:blah/foo". sep = path.find(":./") if sep != -1: return path[:sep], path[sep+3:] sep = path.find("/") if sep != -1: return path[:sep], path[sep+1:] return path, "" colon = path.find(":") if colon == -1: # no alias if default == None: return DefaultAliasMarker, path if default not in aliases: raise UnknownAliasError("No alias specified, and the default %s alias doesn't exist. " "To create it, use 'tahoe create-alias %s'." % (quote_output(default), quote_output(default, quotemarks=False))) return uri.from_string_dirnode(aliases[default]).to_string(), path if colon == 1 and default is None and platform_uses_lettercolon_drivename(): # treat C:\why\must\windows\be\so\weird as a local path, not a tahoe # file in the "C:" alias return DefaultAliasMarker, path # decoding must succeed because path is valid UTF-8 and colon & space are ASCII alias = path[:colon].decode('utf-8') if u"/" in alias: # no alias, but there's a colon in a dirname/filename, like # "foo/bar:7" if default == None: return DefaultAliasMarker, path if default not in aliases: raise UnknownAliasError("No alias specified, and the default %s alias doesn't exist. " "To create it, use 'tahoe create-alias %s'." % (quote_output(default), quote_output(default, quotemarks=False))) return uri.from_string_dirnode(aliases[default]).to_string(), path if alias not in aliases: raise UnknownAliasError("Unknown alias %s, please create it with 'tahoe add-alias' or 'tahoe create-alias'." % quote_output(alias)) return uri.from_string_dirnode(aliases[alias]).to_string(), path[colon+1:]
def add_alias(options): nodedir = options['node-directory'] alias = options.alias precondition(isinstance(alias, unicode), alias=alias) cap = options.cap stdout = options.stdout stderr = options.stderr if u":" in alias: # a single trailing colon will already have been stripped if present print >>stderr, "Alias names cannot contain colons." return 1 if u" " in alias: print >>stderr, "Alias names cannot contain spaces." return 1 old_aliases = get_aliases(nodedir) if alias in old_aliases: print >>stderr, "Alias %s already exists!" % quote_output(alias) return 1 aliasfile = os.path.join(nodedir, "private", "aliases") cap = uri.from_string_dirnode(cap).to_string() add_line_to_aliasfile(aliasfile, alias, cap) print >>stdout, "Alias %s added" % quote_output(alias) return 0
def _add_alias(node_directory, alias, cap): if u":" in alias: raise Exception("Alias names cannot contain colons.") if u" " in alias: raise Exception("Alias names cannot contain spaces.") old_aliases = get_aliases(node_directory) if alias in old_aliases: raise Exception("Alias {} already exists!".format(quote_output(alias))) aliasfile = os.path.join(node_directory, "private", "aliases") cap = uri.from_string_dirnode(cap).to_string() add_line_to_aliasfile(aliasfile, alias, cap) return 0
def add_alias(options): nodedir = options['node-directory'] alias = options.alias cap = options.cap stdout = options.stdout stderr = options.stderr assert ":" not in alias assert " " not in alias old_aliases = get_aliases(nodedir) if alias in old_aliases: print >>stderr, "Alias %s already exists!" % quote_output(alias) return 1 aliasfile = os.path.join(nodedir, "private", "aliases") cap = uri.from_string_dirnode(cap).to_string() add_line_to_aliasfile(aliasfile, alias, cap) print >>stdout, "Alias %s added" % quote_output(alias) return 0
def abbreviated_dirnode(dirnode): u = from_string_dirnode(dirnode.get_uri()) return u.abbrev_si()