3 from allmydata import uri
4 from allmydata.scripts.common_http import do_http, check_http_error
5 from allmydata.scripts.common import get_aliases
6 from allmydata.util.fileutil import move_into_place
8 def add_alias(options):
9 nodedir = options['node-directory']
12 stdout = options.stdout
13 stderr = options.stderr
14 assert ":" not in alias
15 assert " " not in alias
17 old_aliases = get_aliases(nodedir)
18 if alias in old_aliases:
19 print >>stderr, "Alias '%s' already exists!" % alias
21 aliasfile = os.path.join(nodedir, "private", "aliases")
22 cap = uri.from_string_dirnode(cap).to_string()
24 # we use os.path.exists, rather than catching EnvironmentError, to avoid
25 # clobbering the valuable alias file in case of spurious or transient
27 if os.path.exists(aliasfile):
28 f = open(aliasfile, "r")
31 if not aliases.endswith("\n"):
35 aliases += "%s: %s\n" % (alias, cap)
36 f = open(aliasfile+".tmp", "w")
39 move_into_place(aliasfile+".tmp", aliasfile)
41 print >>stdout, "Alias '%s' added" % (alias,)
44 def create_alias(options):
46 nodedir = options['node-directory']
48 stdout = options.stdout
49 stderr = options.stderr
50 assert ":" not in alias
51 assert " " not in alias
53 old_aliases = get_aliases(nodedir)
54 if alias in old_aliases:
55 print >>stderr, "Alias '%s' already exists!" % alias
58 aliasfile = os.path.join(nodedir, "private", "aliases")
60 nodeurl = options['node-url']
61 if not nodeurl.endswith("/"):
63 url = nodeurl + "uri?t=mkdir"
64 resp = do_http("POST", url)
65 rc = check_http_error(resp, stderr)
68 new_uri = resp.read().strip()
70 # probably check for others..
72 # see above about EnvironmentError
73 if os.path.exists(aliasfile):
74 f = open(aliasfile, "r")
77 if not aliases.endswith("\n"):
81 aliases += "%s: %s\n" % (alias, new_uri)
82 f = open(aliasfile+".tmp", "w")
85 move_into_place(aliasfile+".tmp", aliasfile)
87 print >>stdout, "Alias '%s' created" % (alias,)
90 def list_aliases(options):
91 nodedir = options['node-directory']
92 stdout = options.stdout
93 aliases = get_aliases(nodedir)
94 alias_names = sorted(aliases.keys())
95 max_width = max([len(name) for name in alias_names] + [0])
96 fmt = "%" + str(max_width) + "s: %s"
97 for name in alias_names:
98 print >>stdout, fmt % (name, aliases[name])