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 private/aliases which contains "
15 "the mapping from alias name to root dirnode URI."
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", None, 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 CreateAliasOptions(VDriveOptions):
60 def parseArgs(self, alias):
63 class ListAliasOptions(VDriveOptions):
66 class ListOptions(VDriveOptions):
68 ("long", "l", "Use long format: show file sizes, and timestamps"),
69 ("uri", "u", "Show file/directory URIs"),
70 ("readonly-uri", None, "Show readonly file/directory URIs"),
71 ("classify", "F", "Append '/' to directory names, and '*' to mutable"),
72 ("json", None, "Show the raw JSON output"),
74 def parseArgs(self, where=""):
77 longdesc = """List the contents of some portion of the virtual drive."""
79 class GetOptions(VDriveOptions):
80 def parseArgs(self, arg1, arg2=None):
81 # tahoe get FOO |less # write to stdout
82 # tahoe get tahoe:FOO |less # same
83 # tahoe get FOO bar # write to local file
84 # tahoe get tahoe:FOO bar # same
88 if self.to_file == "-":
91 def getSynopsis(self):
92 return "%s get VDRIVE_FILE LOCAL_FILE" % (os.path.basename(sys.argv[0]),)
94 longdesc = """Retrieve a file from the virtual drive and write it to the
95 local filesystem. If LOCAL_FILE is omitted or '-', the contents of the file
96 will be written to stdout."""
98 def getUsage(self, width=None):
99 t = VDriveOptions.getUsage(self, width)
102 % tahoe get FOO |less # write to stdout
103 % tahoe get tahoe:FOO |less # same
104 % tahoe get FOO bar # write to local file
105 % tahoe get tahoe:FOO bar # same
109 class PutOptions(VDriveOptions):
111 ("mutable", "m", "Create a mutable file instead of an immutable one."),
114 def parseArgs(self, arg1=None, arg2=None):
115 # cat FILE > tahoe put # create unlinked file from stdin
116 # cat FILE > tahoe put - # same
117 # tahoe put bar # create unlinked file from local 'bar'
118 # cat FILE > tahoe put - FOO # create tahoe:FOO from stdin
119 # tahoe put bar FOO # copy local 'bar' to tahoe:FOO
120 # tahoe put bar tahoe:FOO # same
122 if arg1 is not None and arg2 is not None:
123 self.from_file = arg1
125 elif arg1 is not None and arg2 is None:
126 self.from_file = arg1 # might be "-"
129 self.from_file = None
131 if self.from_file == "-":
132 self.from_file = None
134 def getSynopsis(self):
135 return "%s put LOCAL_FILE VDRIVE_FILE" % (os.path.basename(sys.argv[0]),)
137 longdesc = """Put a file into the virtual drive (copying the file's
138 contents from the local filesystem). If VDRIVE_FILE is missing, upload
139 the file but do not link it into a directory: prints the new filecap to
140 stdout. If LOCAL_FILE is missing or '-', data will be copied from stdin.
141 VDRIVE_FILE is assumed to start with tahoe: unless otherwise specified."""
143 def getUsage(self, width=None):
144 t = VDriveOptions.getUsage(self, width)
147 % cat FILE > tahoe put # create unlinked file from stdin
148 % cat FILE > tahoe - # same
149 % tahoe put bar # create unlinked file from local 'bar'
150 % cat FILE > tahoe put - FOO # create tahoe:FOO from stdin
151 % tahoe put bar FOO # copy local 'bar' to tahoe:FOO
152 % tahoe put bar tahoe:FOO # same
153 % tahoe put bar MUTABLE-FILE-WRITECAP # modify the mutable file in-place
157 class CpOptions(VDriveOptions):
159 ("recursive", "r", "Copy source directory recursively."),
160 ("verbose", "v", "Be noisy about what is happening."),
162 def parseArgs(self, *args):
164 raise usage.UsageError("cp requires at least two arguments")
165 self.sources = args[:-1]
166 self.destination = args[-1]
168 class RmOptions(VDriveOptions):
169 def parseArgs(self, where):
172 def getSynopsis(self):
173 return "%s rm VE_FILE" % (os.path.basename(sys.argv[0]),)
175 class MvOptions(VDriveOptions):
176 def parseArgs(self, frompath, topath):
177 self.from_file = frompath
178 self.to_file = topath
180 def getSynopsis(self):
181 return "%s mv FROM TO" % (os.path.basename(sys.argv[0]),)
183 class LnOptions(VDriveOptions):
184 def parseArgs(self, frompath, topath):
185 self.from_file = frompath
186 self.to_file = topath
188 def getSynopsis(self):
189 return "%s ln FROM TO" % (os.path.basename(sys.argv[0]),)
191 class WebopenOptions(VDriveOptions):
192 def parseArgs(self, vdrive_pathname=""):
193 self['vdrive_pathname'] = vdrive_pathname
195 longdesc = """Opens a webbrowser to the contents of some portion of the virtual drive."""
197 class ReplOptions(usage.Options):
201 ["mkdir", None, MakeDirectoryOptions, "Create a new directory"],
202 ["add-alias", None, AddAliasOptions, "Add a new alias cap"],
203 ["create-alias", None, CreateAliasOptions, "Create a new alias cap"],
204 ["list-aliases", None, ListAliasOptions, "List all alias caps"],
205 ["ls", None, ListOptions, "List a directory"],
206 ["get", None, GetOptions, "Retrieve a file from the virtual drive."],
207 ["put", None, PutOptions, "Upload a file into the virtual drive."],
208 ["cp", None, CpOptions, "Copy one or more files."],
209 ["rm", None, RmOptions, "Unlink a file or directory in the virtual drive."],
210 ["mv", None, MvOptions, "Move a file within the virtual drive."],
211 ["ln", None, LnOptions, "Make an additional link to an existing file."],
212 ["webopen", None, WebopenOptions, "Open a webbrowser to the root_dir"],
213 ["repl", None, ReplOptions, "Open a python interpreter"],
217 from allmydata.scripts import tahoe_mkdir
218 rc = tahoe_mkdir.mkdir(options)
221 def add_alias(options):
222 from allmydata.scripts import tahoe_add_alias
223 rc = tahoe_add_alias.add_alias(options)
226 def create_alias(options):
227 from allmydata.scripts import tahoe_add_alias
228 rc = tahoe_add_alias.create_alias(options)
231 def list_aliases(options):
232 from allmydata.scripts import tahoe_add_alias
233 rc = tahoe_add_alias.list_aliases(options)
237 from allmydata.scripts import tahoe_ls
238 rc = tahoe_ls.list(options)
242 from allmydata.scripts import tahoe_get
243 rc = tahoe_get.get(options)
245 if options.to_file is None:
246 # be quiet, since the file being written to stdout should be
247 # proof enough that it worked, unless the user is unlucky
248 # enough to have picked an empty file
251 print >>options.stderr, "%s retrieved and written to %s" % \
252 (options.from_file, options.to_file)
256 from allmydata.scripts import tahoe_put
257 rc = tahoe_put.put(options)
261 from allmydata.scripts import tahoe_cp
262 rc = tahoe_cp.copy(options)
266 from allmydata.scripts import tahoe_rm
267 rc = tahoe_rm.rm(options)
271 from allmydata.scripts import tahoe_mv
272 rc = tahoe_mv.mv(options, mode="move")
276 from allmydata.scripts import tahoe_mv
277 rc = tahoe_mv.mv(options, mode="link")
280 def webopen(options):
281 import urllib, webbrowser
282 nodeurl = options['node-url']
283 if nodeurl[-1] != "/":
285 root_cap = options.aliases["tahoe"]
286 url = nodeurl + "uri/%s/" % urllib.quote(root_cap)
287 if options['vdrive_pathname']:
288 url += urllib.quote(options['vdrive_pathname'])
294 return code.interact()
298 "add-alias": add_alias,
299 "create-alias": create_alias,
300 "list-aliases": list_aliases,