2 import os.path, re, sys
3 from twisted.python import usage
4 from allmydata.scripts.common import BaseOptions, get_aliases
6 NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
8 class VDriveOptions(BaseOptions, usage.Options):
10 ["node-directory", "d", "~/.tahoe",
11 "Look here to find out which Tahoe node should be used for all "
12 "operations. The directory should either contain a full Tahoe node, "
13 "or a file named node.url which points to some other Tahoe node. "
14 "It should also contain a file named root_dir.cap which contains "
15 "the root dirnode URI that should be used."
17 ["node-url", "u", None,
18 "URL of the tahoe node to use, a URL like \"http://127.0.0.1:8123\". "
19 "This overrides the URL found in the --node-directory ."],
20 ["dir-cap", "r", None,
21 "Which dirnode URI should be used as the 'tahoe' alias."]
24 def postOptions(self):
25 # compute a node-url from the existing options, put in self['node-url']
26 if self['node-directory']:
27 if sys.platform == 'win32' and self['node-directory'] == '~/.tahoe':
28 from allmydata.windows import registry
29 self['node-directory'] = registry.get_base_dir_path()
31 self['node-directory'] = os.path.expanduser(self['node-directory'])
33 if (not isinstance(self['node-url'], basestring)
34 or not NODEURL_RE.match(self['node-url'])):
35 msg = ("--node-url is required to be a string and look like "
36 "\"http://HOSTNAMEORADDR:PORT\", not: %r" %
38 raise usage.UsageError(msg)
40 node_url_file = os.path.join(self['node-directory'], "node.url")
41 self['node-url'] = open(node_url_file, "r").read().strip()
43 aliases = get_aliases(self['node-directory'])
45 aliases["tahoe"] = self['dir-cap']
46 self.aliases = aliases # maps alias name to dircap
49 class MakeDirectoryOptions(VDriveOptions):
50 def parseArgs(self, where=""):
52 longdesc = """Create a new directory, either unlinked or as a subdirectory."""
54 class AddAliasOptions(VDriveOptions):
55 def parseArgs(self, alias, cap):
59 class ListAliasOptions(VDriveOptions):
62 class ListOptions(VDriveOptions):
64 ("long", "l", "Use long format: show file sizes, and timestamps"),
65 ("uri", "u", "Show file/directory URIs"),
66 ("readonly-uri", None, "Show readonly file/directory URIs"),
67 ("classify", "F", "Append '/' to directory names, and '*' to mutable"),
68 ("json", None, "Show the raw JSON output"),
70 def parseArgs(self, where=""):
73 longdesc = """List the contents of some portion of the virtual drive."""
75 class GetOptions(VDriveOptions):
76 def parseArgs(self, arg1, arg2=None):
77 # tahoe get FOO |less # write to stdout
78 # tahoe get tahoe:FOO |less # same
79 # tahoe get FOO bar # write to local file
80 # tahoe get tahoe:FOO bar # same
84 if self.to_file == "-":
87 def getSynopsis(self):
88 return "%s get VDRIVE_FILE LOCAL_FILE" % (os.path.basename(sys.argv[0]),)
90 longdesc = """Retrieve a file from the virtual drive and write it to the
91 local filesystem. If LOCAL_FILE is omitted or '-', the contents of the file
92 will be written to stdout."""
94 class PutOptions(VDriveOptions):
96 ("mutable", "m", "Create a mutable file instead of an immutable one."),
99 def parseArgs(self, arg1=None, arg2=None):
100 # cat FILE > tahoe put # create unlinked file from stdin
101 # cat FILE > tahoe put FOO # create tahoe:FOO from stdin
102 # cat FILE > tahoe put tahoe:FOO # same
103 # tahoe put bar FOO # copy local 'bar' to tahoe:FOO
104 # tahoe put bar tahoe:FOO # same
106 if arg1 is not None and arg2 is not None:
107 self.from_file = arg1
109 elif arg1 is not None and arg2 is None:
110 self.from_file = None
113 self.from_file = arg1
115 if self.from_file == "-":
116 self.from_file = None
118 def getSynopsis(self):
119 return "%s put LOCAL_FILE VDRIVE_FILE" % (os.path.basename(sys.argv[0]),)
121 longdesc = """Put a file into the virtual drive (copying the file's
122 contents from the local filesystem). LOCAL_FILE is required to be a
123 local file (it can't be stdin)."""
125 class CpOptions(VDriveOptions):
127 ("recursive", "r", "Copy source directory recursively."),
128 ("verbose", "v", "Be noisy about what is happening."),
130 def parseArgs(self, *args):
132 raise usage.UsageError("cp requires at least two arguments")
133 self.sources = args[:-1]
134 self.destination = args[-1]
136 class RmOptions(VDriveOptions):
137 def parseArgs(self, where):
140 def getSynopsis(self):
141 return "%s rm VE_FILE" % (os.path.basename(sys.argv[0]),)
143 class MvOptions(VDriveOptions):
144 def parseArgs(self, frompath, topath):
145 self.from_file = frompath
146 self.to_file = topath
148 def getSynopsis(self):
149 return "%s mv FROM TO" % (os.path.basename(sys.argv[0]),)
151 class LnOptions(VDriveOptions):
152 def parseArgs(self, frompath, topath):
153 self.from_file = frompath
154 self.to_file = topath
156 def getSynopsis(self):
157 return "%s ln FROM TO" % (os.path.basename(sys.argv[0]),)
159 class WebopenOptions(VDriveOptions):
160 def parseArgs(self, vdrive_pathname=""):
161 self['vdrive_pathname'] = vdrive_pathname
163 longdesc = """Opens a webbrowser to the contents of some portion of the virtual drive."""
165 class ReplOptions(usage.Options):
169 ["mkdir", None, MakeDirectoryOptions, "Create a new directory"],
170 ["add-alias", None, AddAliasOptions, "Add a new alias cap"],
171 ["list-aliases", None, ListAliasOptions, "List all alias caps"],
172 ["ls", None, ListOptions, "List a directory"],
173 ["get", None, GetOptions, "Retrieve a file from the virtual drive."],
174 ["put", None, PutOptions, "Upload a file into the virtual drive."],
175 ["cp", None, CpOptions, "Copy one or more files."],
176 ["rm", None, RmOptions, "Unlink a file or directory in the virtual drive."],
177 ["mv", None, MvOptions, "Move a file within the virtual drive."],
178 ["ln", None, LnOptions, "Make an additional link to an existing file."],
179 ["webopen", None, WebopenOptions, "Open a webbrowser to the root_dir"],
180 ["repl", None, ReplOptions, "Open a python interpreter"],
183 def mkdir(config, stdout, stderr):
184 from allmydata.scripts import tahoe_mkdir
185 rc = tahoe_mkdir.mkdir(config['node-url'],
191 def add_alias(config, stdout, stderr):
192 from allmydata.scripts import tahoe_add_alias
193 rc = tahoe_add_alias.add_alias(config['node-directory'],
199 def list_aliases(config, stdout, stderr):
200 from allmydata.scripts import tahoe_add_alias
201 rc = tahoe_add_alias.list_aliases(config['node-directory'],
205 def list(config, stdout, stderr):
206 from allmydata.scripts import tahoe_ls
207 rc = tahoe_ls.list(config['node-url'],
214 def get(config, stdout, stderr):
215 from allmydata.scripts import tahoe_get
216 rc = tahoe_get.get(config['node-url'],
222 if config.to_file is None:
223 # be quiet, since the file being written to stdout should be
224 # proof enough that it worked, unless the user is unlucky
225 # enough to have picked an empty file
228 print >>stderr, "%s retrieved and written to %s" % \
229 (config.from_file, config.to_file)
232 def put(config, stdout, stderr, stdin=sys.stdin):
233 from allmydata.scripts import tahoe_put
238 rc = tahoe_put.put(config['node-url'],
244 stdin, stdout, stderr)
247 def cp(config, stdout, stderr):
248 from allmydata.scripts import tahoe_cp
253 rc = tahoe_cp.copy(config['node-url'],
262 def rm(config, stdout, stderr):
263 from allmydata.scripts import tahoe_rm
268 rc = tahoe_rm.rm(config['node-url'],
275 def mv(config, stdout, stderr):
276 from allmydata.scripts import tahoe_mv
277 rc = tahoe_mv.mv(config['node-url'],
285 def ln(config, stdout, stderr):
286 from allmydata.scripts import tahoe_mv
287 rc = tahoe_mv.mv(config['node-url'],
295 def webopen(config, stdout, stderr):
296 import urllib, webbrowser
297 nodeurl = config['node-url']
298 if nodeurl[-1] != "/":
300 url = nodeurl + "uri/%s/" % urllib.quote(config['dir-cap'])
301 if config['vdrive_pathname']:
302 url += urllib.quote(config['vdrive_pathname'])
306 def repl(config, stdout, stderr):
308 return code.interact()
312 "add-alias": add_alias,
313 "list-aliases": list_aliases,