]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/cli.py
Improve docs on 'cp -r', noting the recent 2329 changes
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / cli.py
index b041b581f4cd0f3d1109b6344fc8480822dcd92e..18ecc6590c573913015b782921cc0faaa1ed88f9 100644 (file)
@@ -1,31 +1,27 @@
 import os.path, re, fnmatch
 from twisted.python import usage
-from allmydata.scripts.common import BaseOptions, get_aliases, get_default_nodedir, DEFAULT_ALIAS
-from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath, quote_output
+from allmydata.scripts.common import get_aliases, get_default_nodedir, \
+     DEFAULT_ALIAS, BaseOptions
+from allmydata.util.encodingutil import argv_to_unicode, argv_to_abspath, quote_local_unicode_path
 
 NODEURL_RE=re.compile("http(s?)://([^:]*)(:([1-9][0-9]*))?")
 
 _default_nodedir = get_default_nodedir()
 
-class VDriveOptions(BaseOptions):
+class FilesystemOptions(BaseOptions):
     optParameters = [
-        ["node-directory", "d", None,
-         "Specify which Tahoe node directory should be used. The directory "
-         "should either contain a full Tahoe node, or a file named node.url "
-         "that points to some other Tahoe node. It should also contain a file "
-         "named '" + os.path.join('private', 'aliases') + "' which contains the "
-         "mapping from alias name to root dirnode URI." + (
-            _default_nodedir and (" [default: " + quote_output(_default_nodedir) + "]") or "")],
         ["node-url", "u", None,
-         "Specify the URL of the Tahoe gateway node, such as 'http://127.0.0.1:3456'. "
+         "Specify the URL of the Tahoe gateway node, such as "
+         "'http://127.0.0.1:3456'. "
          "This overrides the URL found in the --node-directory ."],
         ["dir-cap", None, None,
          "Specify which dirnode URI should be used as the 'tahoe' alias."]
         ]
 
     def postOptions(self):
-        if self['node-directory']:
-            self['node-directory'] = argv_to_abspath(self['node-directory'])
+        self["quiet"] = self.parent["quiet"]
+        if self.parent['node-directory']:
+            self['node-directory'] = argv_to_abspath(self.parent['node-directory'])
         else:
             self['node-directory'] = _default_nodedir
 
@@ -49,23 +45,24 @@ class VDriveOptions(BaseOptions):
         self.aliases = aliases # maps alias name to dircap
 
 
-class MakeDirectoryOptions(VDriveOptions):
+class MakeDirectoryOptions(FilesystemOptions):
     optParameters = [
-        ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
+        ("format", None, None, "Create a directory with the given format: SDMF or MDMF (case-insensitive)"),
         ]
 
     def parseArgs(self, where=""):
         self.where = argv_to_unicode(where)
 
-        if self['mutable-type'] and self['mutable-type'] not in ("sdmf", "mdmf"):
-            raise usage.UsageError("%s is an invalid format" % self['mutable-type'])
+        if self['format']:
+            if self['format'].upper() not in ("SDMF", "MDMF"):
+                raise usage.UsageError("%s is an invalid format" % self['format'])
 
     def getSynopsis(self):
-        return "Usage:  %s mkdir [options] [REMOTE_DIR]" % (self.command_name,)
+        return "Usage:  %s [global-opts] mkdir [options] [REMOTE_DIR]" % (self.command_name,)
 
     longdesc = """Create a new directory, either unlinked or as a subdirectory."""
 
-class AddAliasOptions(VDriveOptions):
+class AddAliasOptions(FilesystemOptions):
     def parseArgs(self, alias, cap):
         self.alias = argv_to_unicode(alias)
         if self.alias.endswith(u':'):
@@ -73,28 +70,28 @@ class AddAliasOptions(VDriveOptions):
         self.cap = cap
 
     def getSynopsis(self):
-        return "Usage:  %s add-alias [options] ALIAS[:] DIRCAP" % (self.command_name,)
+        return "Usage:  %s [global-opts] add-alias [options] ALIAS[:] DIRCAP" % (self.command_name,)
 
     longdesc = """Add a new alias for an existing directory."""
 
-class CreateAliasOptions(VDriveOptions):
+class CreateAliasOptions(FilesystemOptions):
     def parseArgs(self, alias):
         self.alias = argv_to_unicode(alias)
         if self.alias.endswith(u':'):
             self.alias = self.alias[:-1]
 
     def getSynopsis(self):
-        return "Usage:  %s create-alias [options] ALIAS[:]" % (self.command_name,)
+        return "Usage:  %s [global-opts] create-alias [options] ALIAS[:]" % (self.command_name,)
 
     longdesc = """Create a new directory and add an alias for it."""
 
-class ListAliasesOptions(VDriveOptions):
+class ListAliasesOptions(FilesystemOptions):
     def getSynopsis(self):
-        return "Usage:  %s list-aliases [options]" % (self.command_name,)
+        return "Usage:  %s [global-opts] list-aliases [options]" % (self.command_name,)
 
     longdesc = """Display a table of all configured aliases."""
 
-class ListOptions(VDriveOptions):
+class ListOptions(FilesystemOptions):
     optFlags = [
         ("long", "l", "Use long format: show file sizes, and timestamps."),
         ("uri", "u", "Show file/directory URIs."),
@@ -105,9 +102,14 @@ class ListOptions(VDriveOptions):
     def parseArgs(self, where=""):
         self.where = argv_to_unicode(where)
 
+    def getSynopsis(self):
+        return "Usage:  %s [global-opts] ls [options] [PATH]" % (self.command_name,)
+
     longdesc = """
     List the contents of some portion of the grid.
 
+    If PATH is omitted, "tahoe:" is assumed.
+
     When the -l or --long option is used, each line is shown in the
     following format:
 
@@ -131,25 +133,21 @@ class ListOptions(VDriveOptions):
     last modified.
     """
 
-class GetOptions(VDriveOptions):
+class GetOptions(FilesystemOptions):
     def parseArgs(self, arg1, arg2=None):
         # tahoe get FOO |less            # write to stdout
         # tahoe get tahoe:FOO |less      # same
         # tahoe get FOO bar              # write to local file
         # tahoe get tahoe:FOO bar        # same
 
-        self.from_file = argv_to_unicode(arg1)
+        if arg2 == "-":
+            arg2 = None
 
-        if arg2:
-            self.to_file = argv_to_unicode(arg2)
-        else:
-            self.to_file = None
-
-        if self.to_file == "-":
-            self.to_file = None
+        self.from_file = argv_to_unicode(arg1)
+        self.to_file   = None if arg2 is None else argv_to_abspath(arg2)
 
     def getSynopsis(self):
-        return "Usage:  %s get [options] REMOTE_FILE LOCAL_FILE" % (self.command_name,)
+        return "Usage:  %s [global-opts] get [options] REMOTE_FILE LOCAL_FILE" % (self.command_name,)
 
     longdesc = """
     Retrieve a file from the grid and write it to the local filesystem. If
@@ -157,7 +155,7 @@ class GetOptions(VDriveOptions):
     stdout."""
 
     def getUsage(self, width=None):
-        t = VDriveOptions.getUsage(self, width)
+        t = FilesystemOptions.getUsage(self, width)
         t += """
 Examples:
  % tahoe get FOO |less            # write to stdout
@@ -167,48 +165,43 @@ Examples:
 """
         return t
 
-class PutOptions(VDriveOptions):
+class PutOptions(FilesystemOptions):
     optFlags = [
-        ("mutable", "m", "Create a mutable file instead of an immutable one."),
+        ("mutable", "m", "Create a mutable file instead of an immutable one (like --format=SDMF)"),
         ]
     optParameters = [
-        ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
+        ("format", None, None, "Create a file with the given format: SDMF and MDMF for mutable, CHK (default) for immutable. (case-insensitive)"),
         ]
 
     def parseArgs(self, arg1=None, arg2=None):
         # see Examples below
 
-        if arg1 is not None and arg2 is not None:
-            self.from_file = argv_to_unicode(arg1)
-            self.to_file =  argv_to_unicode(arg2)
-        elif arg1 is not None and arg2 is None:
-            self.from_file = argv_to_unicode(arg1) # might be "-"
-            self.to_file = None
-        else:
-            self.from_file = None
-            self.to_file = None
-        if self.from_file == u"-":
-            self.from_file = None
-
-        if self['mutable-type'] and self['mutable-type'] not in ("sdmf", "mdmf"):
-            raise usage.UsageError("%s is an invalid format" % self['mutable-type'])
+        if arg1 == "-":
+            arg1 = None
 
-        if self['mutable-type']:
-            self['mutable'] = True
+        self.from_file = None if arg1 is None else argv_to_abspath(arg1)
+        self.to_file   = None if arg2 is None else argv_to_unicode(arg2)
 
+        if self['format']:
+            if self['format'].upper() not in ("SDMF", "MDMF", "CHK"):
+                raise usage.UsageError("%s is an invalid format" % self['format'])
 
     def getSynopsis(self):
-        return "Usage:  %s put [options] LOCAL_FILE REMOTE_FILE" % (self.command_name,)
+        return "Usage:  %s [global-opts] put [options] LOCAL_FILE REMOTE_FILE" % (self.command_name,)
 
     longdesc = """
     Put a file into the grid, copying its contents from the local filesystem.
     If REMOTE_FILE is missing, upload the file but do not link it into a
     directory; also print the new filecap to stdout. If LOCAL_FILE is missing
     or '-', data will be copied from stdin. REMOTE_FILE is assumed to start
-    with tahoe: unless otherwise specified."""
+    with tahoe: unless otherwise specified.
+
+    If the destination file already exists and is mutable, it will be modified
+    in-place, whether or not --mutable is specified. (--mutable only affects
+    creation of new files.)"""
 
     def getUsage(self, width=None):
-        t = VDriveOptions.getUsage(self, width)
+        t = FilesystemOptions.getUsage(self, width)
         t += """
 Examples:
  % cat FILE | tahoe put                # create unlinked file from stdin
@@ -221,7 +214,7 @@ Examples:
 """
         return t
 
-class CpOptions(VDriveOptions):
+class CpOptions(FilesystemOptions):
     optFlags = [
         ("recursive", "r", "Copy source directory recursively."),
         ("verbose", "v", "Be noisy about what is happening."),
@@ -237,7 +230,7 @@ class CpOptions(VDriveOptions):
         self.destination = argv_to_unicode(args[-1])
 
     def getSynopsis(self):
-        return "Usage: %s cp [options] FROM.. TO" % (self.command_name,)
+        return "Usage: %s [global-opts] cp [options] FROM.. TO" % (self.command_name,)
 
     longdesc = """
     Use 'tahoe cp' to copy files between a local filesystem and a Tahoe grid.
@@ -259,29 +252,34 @@ class CpOptions(VDriveOptions):
 
     This command still has some limitations: symlinks and special files
     (device nodes, named pipes) are not handled very well. Arguments should
-    probably not have trailing slashes. 'tahoe cp' does not behave as much
-    like /bin/cp as you would wish, especially with respect to trailing
-    slashes.
+    not have trailing slashes (they are ignored for directory arguments, but
+    trigger errors for file arguments). When copying directories, it can be
+    unclear whether you mean to copy the contents of a source directory, or
+    the source directory itself (i.e. whether the output goes under the
+    target directory, or one directory lower). Tahoe's rule is that source
+    directories with names are referring to the directory as a whole, and
+    source directories without names (e.g. a raw dircap) are referring to the
+    contents.
     """
 
-class UnlinkOptions(VDriveOptions):
+class UnlinkOptions(FilesystemOptions):
     def parseArgs(self, where):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "Usage:  %s unlink [options] REMOTE_FILE" % (self.command_name,)
+        return "Usage:  %s [global-opts] unlink [options] REMOTE_FILE" % (self.command_name,)
 
 class RmOptions(UnlinkOptions):
     def getSynopsis(self):
-        return "Usage:  %s rm [options] REMOTE_FILE" % (self.command_name,)
+        return "Usage:  %s [global-opts] rm [options] REMOTE_FILE" % (self.command_name,)
 
-class MvOptions(VDriveOptions):
+class MvOptions(FilesystemOptions):
     def parseArgs(self, frompath, topath):
         self.from_file = argv_to_unicode(frompath)
         self.to_file = argv_to_unicode(topath)
 
     def getSynopsis(self):
-        return "Usage:  %s mv [options] FROM TO" % (self.command_name,)
+        return "Usage:  %s [global-opts] mv [options] FROM TO" % (self.command_name,)
 
     longdesc = """
     Use 'tahoe mv' to move files that are already on the grid elsewhere on
@@ -295,13 +293,13 @@ class MvOptions(VDriveOptions):
     the grid -- use 'tahoe cp' for that.
     """
 
-class LnOptions(VDriveOptions):
+class LnOptions(FilesystemOptions):
     def parseArgs(self, frompath, topath):
         self.from_file = argv_to_unicode(frompath)
         self.to_file = argv_to_unicode(topath)
 
     def getSynopsis(self):
-        return "Usage:  %s ln [options] FROM_LINK TO_LINK" % (self.command_name,)
+        return "Usage:  %s [global-opts] ln [options] FROM_LINK TO_LINK" % (self.command_name,)
 
     longdesc = """
     Use 'tahoe ln' to duplicate a link (directory entry) already on the grid
@@ -328,7 +326,7 @@ class LnOptions(VDriveOptions):
 class BackupConfigurationError(Exception):
     pass
 
-class BackupOptions(VDriveOptions):
+class BackupOptions(FilesystemOptions):
     optFlags = [
         ("verbose", "v", "Be noisy about what is happening."),
         ("ignore-timestamps", None, "Do not use backupdb timestamps to decide whether a local file is unchanged."),
@@ -344,11 +342,11 @@ class BackupOptions(VDriveOptions):
         self['exclude'] = set()
 
     def parseArgs(self, localdir, topath):
-        self.from_dir = argv_to_unicode(localdir)
+        self.from_dir = argv_to_abspath(localdir)
         self.to_dir = argv_to_unicode(topath)
 
     def getSynopsis(self):
-        return "Usage:  %s backup [options] FROM ALIAS:TO" % (self.command_name,)
+        return "Usage:  %s [global-opts] backup [options] FROM ALIAS:TO" % (self.command_name,)
 
     def opt_exclude(self, pattern):
         """Ignore files matching a glob pattern. You may give multiple
@@ -365,7 +363,7 @@ class BackupOptions(VDriveOptions):
         try:
             exclude_file = file(abs_filepath)
         except:
-            raise BackupConfigurationError('Error opening exclude file %s.' % quote_output(abs_filepath))
+            raise BackupConfigurationError('Error opening exclude file %s.' % quote_local_unicode_path(abs_filepath))
         try:
             for line in exclude_file:
                 self.opt_exclude(line)
@@ -398,7 +396,7 @@ class BackupOptions(VDriveOptions):
     --link-dest=TO/Archives/(previous) FROM TO/Archives/(new); ln -sf
     TO/Archives/(new) TO/Latest'."""
 
-class WebopenOptions(VDriveOptions):
+class WebopenOptions(FilesystemOptions):
     optFlags = [
         ("info", "i", "Open the t=info page for the file"),
         ]
@@ -406,13 +404,13 @@ class WebopenOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "Usage:  %s webopen [options] [ALIAS:PATH]" % (self.command_name,)
+        return "Usage:  %s [global-opts] webopen [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """Open a web browser to the contents of some file or
     directory on the grid. When run without arguments, open the Welcome
     page."""
 
-class ManifestOptions(VDriveOptions):
+class ManifestOptions(FilesystemOptions):
     optFlags = [
         ("storage-index", "s", "Only print storage index strings, not pathname+cap."),
         ("verify-cap", None, "Only print verifycap, not pathname+cap."),
@@ -423,12 +421,12 @@ class ManifestOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "Usage:  %s manifest [options] [ALIAS:PATH]" % (self.command_name,)
+        return "Usage:  %s [global-opts] manifest [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """Print a list of all files and directories reachable from
     the given starting point."""
 
-class StatsOptions(VDriveOptions):
+class StatsOptions(FilesystemOptions):
     optFlags = [
         ("raw", "r", "Display raw JSON data instead of parsed"),
         ]
@@ -436,30 +434,30 @@ class StatsOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "Usage:  %s stats [options] [ALIAS:PATH]" % (self.command_name,)
+        return "Usage:  %s [global-opts] stats [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """Print statistics about of all files and directories
     reachable from the given starting point."""
 
-class CheckOptions(VDriveOptions):
+class CheckOptions(FilesystemOptions):
     optFlags = [
         ("raw", None, "Display raw JSON data instead of parsed."),
         ("verify", None, "Verify all hashes, instead of merely querying share presence."),
         ("repair", None, "Automatically repair any problems found."),
         ("add-lease", None, "Add/renew lease on all shares."),
         ]
-    def parseArgs(self, where=''):
-        self.where = argv_to_unicode(where)
+    def parseArgs(self, *locations):
+        self.locations = map(argv_to_unicode, locations)
 
     def getSynopsis(self):
-        return "Usage:  %s check [options] [ALIAS:PATH]" % (self.command_name,)
+        return "Usage:  %s [global-opts] check [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """
     Check a single file or directory: count how many shares are available and
     verify their hashes. Optionally repair the file if any problems were
     found."""
 
-class DeepCheckOptions(VDriveOptions):
+class DeepCheckOptions(FilesystemOptions):
     optFlags = [
         ("raw", None, "Display raw JSON data instead of parsed."),
         ("verify", None, "Verify all hashes, instead of merely querying share presence."),
@@ -467,11 +465,11 @@ class DeepCheckOptions(VDriveOptions):
         ("add-lease", None, "Add/renew lease on all shares."),
         ("verbose", "v", "Be noisy about what is happening."),
         ]
-    def parseArgs(self, where=''):
-        self.where = argv_to_unicode(where)
+    def parseArgs(self, *locations):
+        self.locations = map(argv_to_unicode, locations)
 
     def getSynopsis(self):
-        return "Usage:  %s deep-check [options] [ALIAS:PATH]" % (self.command_name,)
+        return "Usage:  %s [global-opts] deep-check [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """
     Check all files and directories reachable from the given starting point