]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/tahoe_add_alias.py
f3ed15c4ad49ba617a2f0876032ab6af1241ce58
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / tahoe_add_alias.py
1
2 import os.path
3 import codecs
4 from allmydata import uri
5 from allmydata.scripts.common_http import do_http, check_http_error
6 from allmydata.scripts.common import get_aliases
7 from allmydata.util.fileutil import move_into_place
8 from allmydata.util.encodingutil import unicode_to_output, quote_output
9
10
11 def add_line_to_aliasfile(aliasfile, alias, cap):
12     # we use os.path.exists, rather than catching EnvironmentError, to avoid
13     # clobbering the valuable alias file in case of spurious or transient
14     # filesystem errors.
15     if os.path.exists(aliasfile):
16         f = codecs.open(aliasfile, "r", "utf-8")
17         aliases = f.read()
18         f.close()
19         if not aliases.endswith("\n"):
20             aliases += "\n"
21     else:
22         aliases = ""
23     aliases += "%s: %s\n" % (alias, cap)
24     f = codecs.open(aliasfile+".tmp", "w", "utf-8")
25     f.write(aliases)
26     f.close()
27     move_into_place(aliasfile+".tmp", aliasfile)
28
29 def add_alias(options):
30     nodedir = options['node-directory']
31     alias = options.alias
32     cap = options.cap
33     stdout = options.stdout
34     stderr = options.stderr
35     if u":" in alias:
36         # a single trailing colon will already have been stripped if present
37         print >>stderr, "Alias names cannot contain colons."
38         return 1
39     if u" " in alias:
40         print >>stderr, "Alias names cannot contain spaces."
41         return 1
42
43     old_aliases = get_aliases(nodedir)
44     if alias in old_aliases:
45         print >>stderr, "Alias %s already exists!" % quote_output(alias)
46         return 1
47     aliasfile = os.path.join(nodedir, "private", "aliases")
48     cap = uri.from_string_dirnode(cap).to_string()
49
50     add_line_to_aliasfile(aliasfile, alias, cap)
51
52     print >>stdout, "Alias %s added" % quote_output(alias)
53     return 0
54
55 def create_alias(options):
56     # mkdir+add_alias
57     nodedir = options['node-directory']
58     alias = options.alias
59     stdout = options.stdout
60     stderr = options.stderr
61     if u":" in alias:
62         # a single trailing colon will already have been stripped if present
63         print >>stderr, "Alias names cannot contain colons."
64         return 1
65     if u" " in alias:
66         print >>stderr, "Alias names cannot contain spaces."
67         return 1
68
69     old_aliases = get_aliases(nodedir)
70     if alias in old_aliases:
71         print >>stderr, "Alias %s already exists!" % quote_output(alias)
72         return 1
73
74     aliasfile = os.path.join(nodedir, "private", "aliases")
75
76     nodeurl = options['node-url']
77     if not nodeurl.endswith("/"):
78         nodeurl += "/"
79     url = nodeurl + "uri?t=mkdir"
80     resp = do_http("POST", url)
81     rc = check_http_error(resp, stderr)
82     if rc:
83         return rc
84     new_uri = resp.read().strip()
85
86     # probably check for others..
87
88     add_line_to_aliasfile(aliasfile, alias, new_uri)
89
90     print >>stdout, "Alias %s created" % (quote_output(alias),)
91     return 0
92
93 def list_aliases(options):
94     nodedir = options['node-directory']
95     stdout = options.stdout
96     stderr = options.stderr
97     aliases = get_aliases(nodedir)
98     alias_names = sorted(aliases.keys())
99     max_width = max([len(quote_output(name)) for name in alias_names] + [0])
100     fmt = "%" + str(max_width) + "s: %s"
101     rc = 0
102     for name in alias_names:
103         try:
104             print >>stdout, fmt % (unicode_to_output(name), unicode_to_output(aliases[name].decode('utf-8')))
105         except (UnicodeEncodeError, UnicodeDecodeError):
106             print >>stderr, fmt % (quote_output(name), quote_output(aliases[name]))
107             rc = 1
108
109     if rc == 1:
110         print >>stderr, "\nThis listing included aliases or caps that could not be converted to the terminal" \
111                         "\noutput encoding. These are shown using backslash escapes and in quotes."
112     return rc