]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/scripts/cli.py
cli: make --mutable-type imply --mutable in 'tahoe put'
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / cli.py
index 8a3d4180698cdea174def5e61f10b6484ff50b60..b041b581f4cd0f3d1109b6344fc8480822dcd92e 100644 (file)
@@ -1,4 +1,4 @@
-import os.path, re, sys, fnmatch
+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
@@ -50,30 +50,48 @@ class VDriveOptions(BaseOptions):
 
 
 class MakeDirectoryOptions(VDriveOptions):
+    optParameters = [
+        ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
+        ]
+
     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'])
+
+    def getSynopsis(self):
+        return "Usage:  %s mkdir [options] [REMOTE_DIR]" % (self.command_name,)
+
     longdesc = """Create a new directory, either unlinked or as a subdirectory."""
 
 class AddAliasOptions(VDriveOptions):
     def parseArgs(self, alias, cap):
         self.alias = argv_to_unicode(alias)
+        if self.alias.endswith(u':'):
+            self.alias = self.alias[:-1]
         self.cap = cap
 
     def getSynopsis(self):
-        return "%s add-alias ALIAS DIRCAP" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s add-alias [options] ALIAS[:] DIRCAP" % (self.command_name,)
 
     longdesc = """Add a new alias for an existing directory."""
 
 class CreateAliasOptions(VDriveOptions):
     def parseArgs(self, alias):
         self.alias = argv_to_unicode(alias)
+        if self.alias.endswith(u':'):
+            self.alias = self.alias[:-1]
 
     def getSynopsis(self):
-        return "%s create-alias ALIAS" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s create-alias [options] ALIAS[:]" % (self.command_name,)
 
     longdesc = """Create a new directory and add an alias for it."""
 
-class ListAliasOptions(VDriveOptions):
+class ListAliasesOptions(VDriveOptions):
+    def getSynopsis(self):
+        return "Usage:  %s list-aliases [options]" % (self.command_name,)
+
     longdesc = """Display a table of all configured aliases."""
 
 class ListOptions(VDriveOptions):
@@ -131,7 +149,7 @@ class GetOptions(VDriveOptions):
             self.to_file = None
 
     def getSynopsis(self):
-        return "%s get REMOTE_FILE LOCAL_FILE" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s get [options] REMOTE_FILE LOCAL_FILE" % (self.command_name,)
 
     longdesc = """
     Retrieve a file from the grid and write it to the local filesystem. If
@@ -153,6 +171,9 @@ class PutOptions(VDriveOptions):
     optFlags = [
         ("mutable", "m", "Create a mutable file instead of an immutable one."),
         ]
+    optParameters = [
+        ("mutable-type", None, False, "Create a mutable file in the given format. Valid formats are 'sdmf' for SDMF and 'mdmf' for MDMF"),
+        ]
 
     def parseArgs(self, arg1=None, arg2=None):
         # see Examples below
@@ -169,8 +190,15 @@ class PutOptions(VDriveOptions):
         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 self['mutable-type']:
+            self['mutable'] = True
+
+
     def getSynopsis(self):
-        return "%s put LOCAL_FILE REMOTE_FILE" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s put [options] LOCAL_FILE REMOTE_FILE" % (self.command_name,)
 
     longdesc = """
     Put a file into the grid, copying its contents from the local filesystem.
@@ -201,13 +229,16 @@ class CpOptions(VDriveOptions):
          "When copying to local files, write out filecaps instead of actual "
          "data (only useful for debugging and tree-comparison purposes)."),
         ]
+
     def parseArgs(self, *args):
         if len(args) < 2:
             raise usage.UsageError("cp requires at least two arguments")
         self.sources = map(argv_to_unicode, args[:-1])
         self.destination = argv_to_unicode(args[-1])
+
     def getSynopsis(self):
-        return "Usage: tahoe [options] cp FROM.. TO"
+        return "Usage: %s cp [options] FROM.. TO" % (self.command_name,)
+
     longdesc = """
     Use 'tahoe cp' to copy files between a local filesystem and a Tahoe grid.
     Any FROM/TO arguments that begin with an alias indicate Tahoe-side
@@ -233,16 +264,16 @@ class CpOptions(VDriveOptions):
     slashes.
     """
 
-class RmOptions(VDriveOptions):
+class UnlinkOptions(VDriveOptions):
     def parseArgs(self, where):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s rm REMOTE_FILE" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s unlink [options] REMOTE_FILE" % (self.command_name,)
 
-class UnlinkOptions(RmOptions):
+class RmOptions(UnlinkOptions):
     def getSynopsis(self):
-        return "%s unlink REMOTE_FILE" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s rm [options] REMOTE_FILE" % (self.command_name,)
 
 class MvOptions(VDriveOptions):
     def parseArgs(self, frompath, topath):
@@ -250,7 +281,8 @@ class MvOptions(VDriveOptions):
         self.to_file = argv_to_unicode(topath)
 
     def getSynopsis(self):
-        return "%s mv FROM TO" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s mv [options] FROM TO" % (self.command_name,)
+
     longdesc = """
     Use 'tahoe mv' to move files that are already on the grid elsewhere on
     the grid, e.g., 'tahoe mv alias:some_file alias:new_file'.
@@ -269,7 +301,7 @@ class LnOptions(VDriveOptions):
         self.to_file = argv_to_unicode(topath)
 
     def getSynopsis(self):
-        return "%s ln FROM_LINK TO_LINK" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s ln [options] FROM_LINK TO_LINK" % (self.command_name,)
 
     longdesc = """
     Use 'tahoe ln' to duplicate a link (directory entry) already on the grid
@@ -315,8 +347,8 @@ class BackupOptions(VDriveOptions):
         self.from_dir = argv_to_unicode(localdir)
         self.to_dir = argv_to_unicode(topath)
 
-    def getSynopsis(Self):
-        return "%s backup FROM ALIAS:TO" % os.path.basename(sys.argv[0])
+    def getSynopsis(self):
+        return "Usage:  %s backup [options] FROM ALIAS:TO" % (self.command_name,)
 
     def opt_exclude(self, pattern):
         """Ignore files matching a glob pattern. You may give multiple
@@ -374,7 +406,7 @@ class WebopenOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s webopen [ALIAS:PATH]" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s 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
@@ -391,7 +423,7 @@ class ManifestOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s manifest [ALIAS:PATH]" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s manifest [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """Print a list of all files and directories reachable from
     the given starting point."""
@@ -404,7 +436,7 @@ class StatsOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s stats [ALIAS:PATH]" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s stats [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """Print statistics about of all files and directories
     reachable from the given starting point."""
@@ -420,7 +452,7 @@ class CheckOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s check [ALIAS:PATH]" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s check [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """
     Check a single file or directory: count how many shares are available and
@@ -439,7 +471,7 @@ class DeepCheckOptions(VDriveOptions):
         self.where = argv_to_unicode(where)
 
     def getSynopsis(self):
-        return "%s deep-check [ALIAS:PATH]" % (os.path.basename(sys.argv[0]),)
+        return "Usage:  %s deep-check [options] [ALIAS:PATH]" % (self.command_name,)
 
     longdesc = """
     Check all files and directories reachable from the given starting point
@@ -450,13 +482,13 @@ subCommands = [
     ["mkdir", None, MakeDirectoryOptions, "Create a new directory."],
     ["add-alias", None, AddAliasOptions, "Add a new alias cap."],
     ["create-alias", None, CreateAliasOptions, "Create a new alias cap."],
-    ["list-aliases", None, ListAliasOptions, "List all alias caps."],
+    ["list-aliases", None, ListAliasesOptions, "List all alias caps."],
     ["ls", None, ListOptions, "List a directory."],
     ["get", None, GetOptions, "Retrieve a file from the grid."],
     ["put", None, PutOptions, "Upload a file into the grid."],
     ["cp", None, CpOptions, "Copy one or more files or directories."],
-    ["rm", None, RmOptions, "Unlink a file or directory on the grid."],
-    ["unlink", None, UnlinkOptions, "Unlink a file or directory on the grid (same as rm)."],
+    ["unlink", None, UnlinkOptions, "Unlink a file or directory on the grid."],
+    ["rm", None, RmOptions, "Unlink a file or directory on the grid (same as unlink)."],
     ["mv", None, MvOptions, "Move a file within the grid."],
     ["ln", None, LnOptions, "Make an additional link to an existing file or directory."],
     ["backup", None, BackupOptions, "Make target dir look like local dir."],
@@ -516,11 +548,14 @@ def cp(options):
     rc = tahoe_cp.copy(options)
     return rc
 
-def rm(options):
-    from allmydata.scripts import tahoe_rm
-    rc = tahoe_rm.rm(options)
+def unlink(options, command="unlink"):
+    from allmydata.scripts import tahoe_unlink
+    rc = tahoe_unlink.unlink(options, command=command)
     return rc
 
+def rm(options):
+    return unlink(options, command="rm")
+
 def mv(options):
     from allmydata.scripts import tahoe_mv
     rc = tahoe_mv.mv(options, mode="move")
@@ -570,8 +605,8 @@ dispatch = {
     "get": get,
     "put": put,
     "cp": cp,
+    "unlink": unlink,
     "rm": rm,
-    "unlink": rm,
     "mv": mv,
     "ln": ln,
     "backup": backup,