1 import os.path, re, fnmatch
2 from twisted.python import usage
3 from allmydata.scripts.common import BaseOptions, get_aliases, get_default_nodedir, DEFAULT_ALIAS
4 from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath, quote_output
6 NODEURL_RE=re.compile("http(s?)://([^:]*)(:([1-9][0-9]*))?")
8 _default_nodedir = get_default_nodedir()
10 class VDriveOptions(BaseOptions):
12 ["node-directory", "d", None,
13 "Specify which Tahoe node directory should be used. The directory "
14 "should either contain a full Tahoe node, or a file named node.url "
15 "that points to some other Tahoe node. It should also contain a file "
16 "named '" + os.path.join('private', 'aliases') + "' which contains the "
17 "mapping from alias name to root dirnode URI." + (
18 _default_nodedir and (" [default: " + quote_output(_default_nodedir) + "]") or "")],
19 ["node-url", "u", None,
20 "Specify the URL of the Tahoe gateway node, such as 'http://127.0.0.1:3456'. "
21 "This overrides the URL found in the --node-directory ."],
22 ["dir-cap", None, None,
23 "Specify which dirnode URI should be used as the 'tahoe' alias."]
26 def postOptions(self):
27 if self['node-directory']:
28 self['node-directory'] = argv_to_abspath(self['node-directory'])
30 self['node-directory'] = _default_nodedir
32 # compute a node-url from the existing options, put in self['node-url']
34 if (not isinstance(self['node-url'], basestring)
35 or not NODEURL_RE.match(self['node-url'])):
36 msg = ("--node-url is required to be a string and look like "
37 "\"http://HOSTNAMEORADDR:PORT\", not: %r" %
39 raise usage.UsageError(msg)
41 node_url_file = os.path.join(self['node-directory'], "node.url")
42 self['node-url'] = open(node_url_file, "r").read().strip()
43 if self['node-url'][-1] != "/":
44 self['node-url'] += "/"
46 aliases = get_aliases(self['node-directory'])
48 aliases[DEFAULT_ALIAS] = self['dir-cap']
49 self.aliases = aliases # maps alias name to dircap
52 class MakeDirectoryOptions(VDriveOptions):
54 ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
57 def parseArgs(self, where=""):
58 self.where = argv_to_unicode(where)
60 if self['mutable-type'] and self['mutable-type'] not in ("sdmf", "mdmf"):
61 raise usage.UsageError("%s is an invalid format" % self['mutable-type'])
63 def getSynopsis(self):
64 return "Usage: %s mkdir [options] [REMOTE_DIR]" % (self.command_name,)
66 longdesc = """Create a new directory, either unlinked or as a subdirectory."""
68 class AddAliasOptions(VDriveOptions):
69 def parseArgs(self, alias, cap):
70 self.alias = argv_to_unicode(alias)
71 if self.alias.endswith(u':'):
72 self.alias = self.alias[:-1]
75 def getSynopsis(self):
76 return "Usage: %s add-alias [options] ALIAS[:] DIRCAP" % (self.command_name,)
78 longdesc = """Add a new alias for an existing directory."""
80 class CreateAliasOptions(VDriveOptions):
81 def parseArgs(self, alias):
82 self.alias = argv_to_unicode(alias)
83 if self.alias.endswith(u':'):
84 self.alias = self.alias[:-1]
86 def getSynopsis(self):
87 return "Usage: %s create-alias [options] ALIAS[:]" % (self.command_name,)
89 longdesc = """Create a new directory and add an alias for it."""
91 class ListAliasesOptions(VDriveOptions):
92 def getSynopsis(self):
93 return "Usage: %s list-aliases [options]" % (self.command_name,)
95 longdesc = """Display a table of all configured aliases."""
97 class ListOptions(VDriveOptions):
99 ("long", "l", "Use long format: show file sizes, and timestamps."),
100 ("uri", "u", "Show file/directory URIs."),
101 ("readonly-uri", None, "Show read-only file/directory URIs."),
102 ("classify", "F", "Append '/' to directory names, and '*' to mutable."),
103 ("json", None, "Show the raw JSON output."),
105 def parseArgs(self, where=""):
106 self.where = argv_to_unicode(where)
109 List the contents of some portion of the grid.
111 When the -l or --long option is used, each line is shown in the
114 drwx <size> <date/time> <name in this directory>
116 where each of the letters on the left may be replaced by '-'.
117 If 'd' is present, it indicates that the object is a directory.
118 If the 'd' is replaced by a '?', the object type is unknown.
119 'rwx' is a Unix-like permissions mask: if the mask includes 'w',
120 then the object is writeable through its link in this directory
121 (note that the link might be replaceable even if the object is
122 not writeable through the current link).
123 The 'x' is a legacy of Unix filesystems. In Tahoe it is used
124 only to indicate that the contents of a directory can be listed.
126 Directories have no size, so their size field is shown as '-'.
127 Otherwise the size of the file, when known, is given in bytes.
128 The size of mutable files or unknown objects is shown as '?'.
130 The date/time shows when this link in the Tahoe filesystem was
134 class GetOptions(VDriveOptions):
135 def parseArgs(self, arg1, arg2=None):
136 # tahoe get FOO |less # write to stdout
137 # tahoe get tahoe:FOO |less # same
138 # tahoe get FOO bar # write to local file
139 # tahoe get tahoe:FOO bar # same
141 self.from_file = argv_to_unicode(arg1)
144 self.to_file = argv_to_unicode(arg2)
148 if self.to_file == "-":
151 def getSynopsis(self):
152 return "Usage: %s get [options] REMOTE_FILE LOCAL_FILE" % (self.command_name,)
155 Retrieve a file from the grid and write it to the local filesystem. If
156 LOCAL_FILE is omitted or '-', the contents of the file will be written to
159 def getUsage(self, width=None):
160 t = VDriveOptions.getUsage(self, width)
163 % tahoe get FOO |less # write to stdout
164 % tahoe get tahoe:FOO |less # same
165 % tahoe get FOO bar # write to local file
166 % tahoe get tahoe:FOO bar # same
170 class PutOptions(VDriveOptions):
172 ("mutable", "m", "Create a mutable file instead of an immutable one."),
175 ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
178 def parseArgs(self, arg1=None, arg2=None):
181 if arg1 is not None and arg2 is not None:
182 self.from_file = argv_to_unicode(arg1)
183 self.to_file = argv_to_unicode(arg2)
184 elif arg1 is not None and arg2 is None:
185 self.from_file = argv_to_unicode(arg1) # might be "-"
188 self.from_file = None
190 if self.from_file == u"-":
191 self.from_file = None
193 if self['mutable-type'] and self['mutable-type'] not in ("sdmf", "mdmf"):
194 raise usage.UsageError("%s is an invalid format" % self['mutable-type'])
197 def getSynopsis(self):
198 return "Usage: %s put [options] LOCAL_FILE REMOTE_FILE" % (self.command_name,)
201 Put a file into the grid, copying its contents from the local filesystem.
202 If REMOTE_FILE is missing, upload the file but do not link it into a
203 directory; also print the new filecap to stdout. If LOCAL_FILE is missing
204 or '-', data will be copied from stdin. REMOTE_FILE is assumed to start
205 with tahoe: unless otherwise specified."""
207 def getUsage(self, width=None):
208 t = VDriveOptions.getUsage(self, width)
211 % cat FILE | tahoe put # create unlinked file from stdin
212 % cat FILE | tahoe put - # same
213 % tahoe put bar # create unlinked file from local 'bar'
214 % cat FILE | tahoe put - FOO # create tahoe:FOO from stdin
215 % tahoe put bar FOO # copy local 'bar' to tahoe:FOO
216 % tahoe put bar tahoe:FOO # same
217 % tahoe put bar MUTABLE-FILE-WRITECAP # modify the mutable file in-place
221 class CpOptions(VDriveOptions):
223 ("recursive", "r", "Copy source directory recursively."),
224 ("verbose", "v", "Be noisy about what is happening."),
226 "When copying to local files, write out filecaps instead of actual "
227 "data (only useful for debugging and tree-comparison purposes)."),
230 def parseArgs(self, *args):
232 raise usage.UsageError("cp requires at least two arguments")
233 self.sources = map(argv_to_unicode, args[:-1])
234 self.destination = argv_to_unicode(args[-1])
236 def getSynopsis(self):
237 return "Usage: %s cp [options] FROM.. TO" % (self.command_name,)
240 Use 'tahoe cp' to copy files between a local filesystem and a Tahoe grid.
241 Any FROM/TO arguments that begin with an alias indicate Tahoe-side
242 files or non-file arguments. Directories will be copied recursively.
243 New Tahoe-side directories will be created when necessary. Assuming that
244 you have previously set up an alias 'home' with 'tahoe create-alias home',
245 here are some examples:
247 tahoe cp ~/foo.txt home: # creates tahoe-side home:foo.txt
249 tahoe cp ~/foo.txt /tmp/bar.txt home: # copies two files to home:
251 tahoe cp ~/Pictures home:stuff/my-pictures # copies directory recursively
253 You can also use a dircap as either FROM or TO target:
255 tahoe cp URI:DIR2-RO:ixqhc4kdbjxc7o65xjnveoewym:5x6lwoxghrd5rxhwunzavft2qygfkt27oj3fbxlq4c6p45z5uneq/blog.html ./ # copy Zooko's wiki page to a local file
257 This command still has some limitations: symlinks and special files
258 (device nodes, named pipes) are not handled very well. Arguments should
259 probably not have trailing slashes. 'tahoe cp' does not behave as much
260 like /bin/cp as you would wish, especially with respect to trailing
264 class UnlinkOptions(VDriveOptions):
265 def parseArgs(self, where):
266 self.where = argv_to_unicode(where)
268 def getSynopsis(self):
269 return "Usage: %s unlink [options] REMOTE_FILE" % (self.command_name,)
271 class RmOptions(UnlinkOptions):
272 def getSynopsis(self):
273 return "Usage: %s rm [options] REMOTE_FILE" % (self.command_name,)
275 class MvOptions(VDriveOptions):
276 def parseArgs(self, frompath, topath):
277 self.from_file = argv_to_unicode(frompath)
278 self.to_file = argv_to_unicode(topath)
280 def getSynopsis(self):
281 return "Usage: %s mv [options] FROM TO" % (self.command_name,)
284 Use 'tahoe mv' to move files that are already on the grid elsewhere on
285 the grid, e.g., 'tahoe mv alias:some_file alias:new_file'.
287 If moving a remote file into a remote directory, you'll need to append a
288 '/' to the name of the remote directory, e.g., 'tahoe mv tahoe:file1
289 tahoe:dir/', not 'tahoe mv tahoe:file1 tahoe:dir'.
291 Note that it is not possible to use this command to move local files to
292 the grid -- use 'tahoe cp' for that.
295 class LnOptions(VDriveOptions):
296 def parseArgs(self, frompath, topath):
297 self.from_file = argv_to_unicode(frompath)
298 self.to_file = argv_to_unicode(topath)
300 def getSynopsis(self):
301 return "Usage: %s ln [options] FROM_LINK TO_LINK" % (self.command_name,)
304 Use 'tahoe ln' to duplicate a link (directory entry) already on the grid
305 to elsewhere on the grid. For example 'tahoe ln alias:some_file
306 alias:new_file'. causes 'alias:new_file' to point to the same object that
307 'alias:some_file' points to.
309 (The argument order is the same as Unix ln. To remember the order, you
310 can think of this command as copying a link, rather than copying a file
311 as 'tahoe cp' does. Then the argument order is consistent with that of
314 When linking a remote file into a remote directory, you'll need to append
315 a '/' to the name of the remote directory, e.g. 'tahoe ln tahoe:file1
316 tahoe:dir/' (which is shorthand for 'tahoe ln tahoe:file1
317 tahoe:dir/file1'). If you forget the '/', e.g. 'tahoe ln tahoe:file1
318 tahoe:dir', the 'ln' command will refuse to overwrite the 'tahoe:dir'
319 directory, and will exit with an error.
321 Note that it is not possible to use this command to create links between
322 local and remote files.
325 class BackupConfigurationError(Exception):
328 class BackupOptions(VDriveOptions):
330 ("verbose", "v", "Be noisy about what is happening."),
331 ("ignore-timestamps", None, "Do not use backupdb timestamps to decide whether a local file is unchanged."),
334 vcs_patterns = ('CVS', 'RCS', 'SCCS', '.git', '.gitignore', '.cvsignore',
335 '.svn', '.arch-ids','{arch}', '=RELEASE-ID',
336 '=meta-update', '=update', '.bzr', '.bzrignore',
337 '.bzrtags', '.hg', '.hgignore', '_darcs')
340 super(BackupOptions, self).__init__()
341 self['exclude'] = set()
343 def parseArgs(self, localdir, topath):
344 self.from_dir = argv_to_unicode(localdir)
345 self.to_dir = argv_to_unicode(topath)
347 def getSynopsis(self):
348 return "Usage: %s backup [options] FROM ALIAS:TO" % (self.command_name,)
350 def opt_exclude(self, pattern):
351 """Ignore files matching a glob pattern. You may give multiple
352 '--exclude' options."""
353 g = argv_to_unicode(pattern).strip()
355 exclude = self['exclude']
358 def opt_exclude_from(self, filepath):
359 """Ignore file matching glob patterns listed in file, one per
360 line. The file is assumed to be in the argv encoding."""
361 abs_filepath = argv_to_abspath(filepath)
363 exclude_file = file(abs_filepath)
365 raise BackupConfigurationError('Error opening exclude file %s.' % quote_output(abs_filepath))
367 for line in exclude_file:
368 self.opt_exclude(line)
372 def opt_exclude_vcs(self):
373 """Exclude files and directories used by following version control
374 systems: CVS, RCS, SCCS, Git, SVN, Arch, Bazaar(bzr), Mercurial,
376 for pattern in self.vcs_patterns:
377 self.opt_exclude(pattern)
379 def filter_listdir(self, listdir):
380 """Yields non-excluded childpaths in path."""
381 exclude = self['exclude']
382 exclude_regexps = [re.compile(fnmatch.translate(pat)) for pat in exclude]
383 for filename in listdir:
384 for regexp in exclude_regexps:
385 if regexp.match(filename):
391 Add a versioned backup of the local FROM directory to a timestamped
392 subdirectory of the TO/Archives directory on the grid, sharing as many
393 files and directories as possible with earlier backups. Create TO/Latest
394 as a reference to the latest backup. Behaves somewhat like 'rsync -a
395 --link-dest=TO/Archives/(previous) FROM TO/Archives/(new); ln -sf
396 TO/Archives/(new) TO/Latest'."""
398 class WebopenOptions(VDriveOptions):
400 ("info", "i", "Open the t=info page for the file"),
402 def parseArgs(self, where=''):
403 self.where = argv_to_unicode(where)
405 def getSynopsis(self):
406 return "Usage: %s webopen [options] [ALIAS:PATH]" % (self.command_name,)
408 longdesc = """Open a web browser to the contents of some file or
409 directory on the grid. When run without arguments, open the Welcome
412 class ManifestOptions(VDriveOptions):
414 ("storage-index", "s", "Only print storage index strings, not pathname+cap."),
415 ("verify-cap", None, "Only print verifycap, not pathname+cap."),
416 ("repair-cap", None, "Only print repaircap, not pathname+cap."),
417 ("raw", "r", "Display raw JSON data instead of parsed."),
419 def parseArgs(self, where=''):
420 self.where = argv_to_unicode(where)
422 def getSynopsis(self):
423 return "Usage: %s manifest [options] [ALIAS:PATH]" % (self.command_name,)
425 longdesc = """Print a list of all files and directories reachable from
426 the given starting point."""
428 class StatsOptions(VDriveOptions):
430 ("raw", "r", "Display raw JSON data instead of parsed"),
432 def parseArgs(self, where=''):
433 self.where = argv_to_unicode(where)
435 def getSynopsis(self):
436 return "Usage: %s stats [options] [ALIAS:PATH]" % (self.command_name,)
438 longdesc = """Print statistics about of all files and directories
439 reachable from the given starting point."""
441 class CheckOptions(VDriveOptions):
443 ("raw", None, "Display raw JSON data instead of parsed."),
444 ("verify", None, "Verify all hashes, instead of merely querying share presence."),
445 ("repair", None, "Automatically repair any problems found."),
446 ("add-lease", None, "Add/renew lease on all shares."),
448 def parseArgs(self, where=''):
449 self.where = argv_to_unicode(where)
451 def getSynopsis(self):
452 return "Usage: %s check [options] [ALIAS:PATH]" % (self.command_name,)
455 Check a single file or directory: count how many shares are available and
456 verify their hashes. Optionally repair the file if any problems were
459 class DeepCheckOptions(VDriveOptions):
461 ("raw", None, "Display raw JSON data instead of parsed."),
462 ("verify", None, "Verify all hashes, instead of merely querying share presence."),
463 ("repair", None, "Automatically repair any problems found."),
464 ("add-lease", None, "Add/renew lease on all shares."),
465 ("verbose", "v", "Be noisy about what is happening."),
467 def parseArgs(self, where=''):
468 self.where = argv_to_unicode(where)
470 def getSynopsis(self):
471 return "Usage: %s deep-check [options] [ALIAS:PATH]" % (self.command_name,)
474 Check all files and directories reachable from the given starting point
475 (which must be a directory), like 'tahoe check' but for multiple files.
476 Optionally repair any problems found."""
479 ["mkdir", None, MakeDirectoryOptions, "Create a new directory."],
480 ["add-alias", None, AddAliasOptions, "Add a new alias cap."],
481 ["create-alias", None, CreateAliasOptions, "Create a new alias cap."],
482 ["list-aliases", None, ListAliasesOptions, "List all alias caps."],
483 ["ls", None, ListOptions, "List a directory."],
484 ["get", None, GetOptions, "Retrieve a file from the grid."],
485 ["put", None, PutOptions, "Upload a file into the grid."],
486 ["cp", None, CpOptions, "Copy one or more files or directories."],
487 ["unlink", None, UnlinkOptions, "Unlink a file or directory on the grid."],
488 ["rm", None, RmOptions, "Unlink a file or directory on the grid (same as unlink)."],
489 ["mv", None, MvOptions, "Move a file within the grid."],
490 ["ln", None, LnOptions, "Make an additional link to an existing file or directory."],
491 ["backup", None, BackupOptions, "Make target dir look like local dir."],
492 ["webopen", None, WebopenOptions, "Open a web browser to a grid file or directory."],
493 ["manifest", None, ManifestOptions, "List all files/directories in a subtree."],
494 ["stats", None, StatsOptions, "Print statistics about all files/directories in a subtree."],
495 ["check", None, CheckOptions, "Check a single file or directory."],
496 ["deep-check", None, DeepCheckOptions, "Check all files/directories reachable from a starting point."],
500 from allmydata.scripts import tahoe_mkdir
501 rc = tahoe_mkdir.mkdir(options)
504 def add_alias(options):
505 from allmydata.scripts import tahoe_add_alias
506 rc = tahoe_add_alias.add_alias(options)
509 def create_alias(options):
510 from allmydata.scripts import tahoe_add_alias
511 rc = tahoe_add_alias.create_alias(options)
514 def list_aliases(options):
515 from allmydata.scripts import tahoe_add_alias
516 rc = tahoe_add_alias.list_aliases(options)
520 from allmydata.scripts import tahoe_ls
521 rc = tahoe_ls.list(options)
525 from allmydata.scripts import tahoe_get
526 rc = tahoe_get.get(options)
528 if options.to_file is None:
529 # be quiet, since the file being written to stdout should be
530 # proof enough that it worked, unless the user is unlucky
531 # enough to have picked an empty file
534 print >>options.stderr, "%s retrieved and written to %s" % \
535 (options.from_file, options.to_file)
539 from allmydata.scripts import tahoe_put
540 rc = tahoe_put.put(options)
544 from allmydata.scripts import tahoe_cp
545 rc = tahoe_cp.copy(options)
548 def unlink(options, command="unlink"):
549 from allmydata.scripts import tahoe_unlink
550 rc = tahoe_unlink.unlink(options, command=command)
554 return unlink(options, command="rm")
557 from allmydata.scripts import tahoe_mv
558 rc = tahoe_mv.mv(options, mode="move")
562 from allmydata.scripts import tahoe_mv
563 rc = tahoe_mv.mv(options, mode="link")
567 from allmydata.scripts import tahoe_backup
568 rc = tahoe_backup.backup(options)
571 def webopen(options, opener=None):
572 from allmydata.scripts import tahoe_webopen
573 rc = tahoe_webopen.webopen(options, opener=opener)
576 def manifest(options):
577 from allmydata.scripts import tahoe_manifest
578 rc = tahoe_manifest.manifest(options)
582 from allmydata.scripts import tahoe_manifest
583 rc = tahoe_manifest.stats(options)
587 from allmydata.scripts import tahoe_check
588 rc = tahoe_check.check(options)
591 def deepcheck(options):
592 from allmydata.scripts import tahoe_check
593 rc = tahoe_check.deepcheck(options)
598 "add-alias": add_alias,
599 "create-alias": create_alias,
600 "list-aliases": list_aliases,
611 "manifest": manifest,
614 "deep-check": deepcheck,