From 06a5d0c1a3135b8c36023ba2a0f5a45f887789a6 Mon Sep 17 00:00:00 2001
From: david-sarah <david-sarah@jacaranda.org>
Date: Mon, 1 Aug 2011 15:01:08 -0700
Subject: [PATCH] cleanup: implement rm as a synonym for unlink rather than
 vice-versa. refs #776

---
 src/allmydata/scripts/cli.py                  | 23 ++++++----
 .../scripts/{tahoe_rm.py => tahoe_unlink.py}  |  4 +-
 src/allmydata/test/test_cli.py                | 46 +++++++++++--------
 3 files changed, 42 insertions(+), 31 deletions(-)
 rename src/allmydata/scripts/{tahoe_rm.py => tahoe_unlink.py} (88%)

diff --git a/src/allmydata/scripts/cli.py b/src/allmydata/scripts/cli.py
index d4da88fd..0eeb78d9 100644
--- a/src/allmydata/scripts/cli.py
+++ b/src/allmydata/scripts/cli.py
@@ -247,16 +247,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 "Usage:  %s rm [options] REMOTE_FILE" % (self.command_name,)
+        return "Usage:  %s unlink [options] REMOTE_FILE" % (self.command_name,)
 
-class UnlinkOptions(RmOptions):
+class RmOptions(UnlinkOptions):
     def getSynopsis(self):
-        return "Usage:  %s unlink [options] REMOTE_FILE" % (self.command_name,)
+        return "Usage:  %s rm [options] REMOTE_FILE" % (self.command_name,)
 
 class MvOptions(VDriveOptions):
     def parseArgs(self, frompath, topath):
@@ -470,8 +470,8 @@ subCommands = [
     ["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."],
@@ -531,11 +531,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")
@@ -585,8 +588,8 @@ dispatch = {
     "get": get,
     "put": put,
     "cp": cp,
+    "unlink": unlink,
     "rm": rm,
-    "unlink": rm,
     "mv": mv,
     "ln": ln,
     "backup": backup,
diff --git a/src/allmydata/scripts/tahoe_rm.py b/src/allmydata/scripts/tahoe_unlink.py
similarity index 88%
rename from src/allmydata/scripts/tahoe_rm.py
rename to src/allmydata/scripts/tahoe_unlink.py
index 51fb28c3..979fcc4e 100644
--- a/src/allmydata/scripts/tahoe_rm.py
+++ b/src/allmydata/scripts/tahoe_unlink.py
@@ -4,7 +4,7 @@ from allmydata.scripts.common_http import do_http, format_http_success, format_h
 from allmydata.scripts.common import get_alias, DEFAULT_ALIAS, escape_path, \
                                      UnknownAliasError
 
-def rm(options):
+def unlink(options, command="unlink"):
     """
     @return: a Deferred which eventually fires with the exit code
     """
@@ -23,7 +23,7 @@ def rm(options):
         return 1
     if not path:
         print >>stderr, """
-'tahoe rm' can only unlink directory entries, so a path must be given."""
+'tahoe %s' can only unlink directory entries, so a path must be given.""" % (command,)
         return 1
 
     url = nodeurl + "uri/%s" % urllib.quote(rootcap)
diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py
index 2c2d130c..498064f9 100644
--- a/src/allmydata/test/test_cli.py
+++ b/src/allmydata/test/test_cli.py
@@ -15,10 +15,10 @@ from allmydata.dirnode import normalize
 # Test that the scripts can be imported.
 from allmydata.scripts import create_node, debug, keygen, startstop_node, \
     tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls, \
-    tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_rm, tahoe_webopen
+    tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen
 _hush_pyflakes = [create_node, debug, keygen, startstop_node,
     tahoe_add_alias, tahoe_backup, tahoe_check, tahoe_cp, tahoe_get, tahoe_ls,
-    tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_rm, tahoe_webopen]
+    tahoe_manifest, tahoe_mkdir, tahoe_mv, tahoe_put, tahoe_unlink, tahoe_webopen]
 
 from allmydata.scripts import common
 from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases, get_alias, \
@@ -2941,35 +2941,37 @@ class Mkdir(GridTestMixin, CLITestMixin, unittest.TestCase):
         return d
 
 
-class Rm(GridTestMixin, CLITestMixin, unittest.TestCase):
+class Unlink(GridTestMixin, CLITestMixin, unittest.TestCase):
+    command = "unlink"
+
     def _create_test_file(self):
         data = "puppies" * 1000
         path = os.path.join(self.basedir, "datafile")
         fileutil.write(path, data)
         self.datafile = path
 
-    def test_rm_without_alias(self):
-        # 'tahoe rm' should behave sensibly when invoked without an explicit
+    def test_unlink_without_alias(self):
+        # 'tahoe unlink' should behave sensibly when invoked without an explicit
         # alias before the default 'tahoe' alias has been created.
-        self.basedir = "cli/Rm/rm_without_alias"
+        self.basedir = "cli/Unlink/%s_without_alias" % (self.command,)
         self.set_up_grid()
-        d = self.do_cli("rm", "afile")
+        d = self.do_cli(self.command, "afile")
         def _check((rc, out, err)):
             self.failUnlessReallyEqual(rc, 1)
             self.failUnlessIn("error:", err)
             self.failUnlessReallyEqual(out, "")
         d.addCallback(_check)
 
-        d.addCallback(lambda ign: self.do_cli("unlink", "afile"))
+        d.addCallback(lambda ign: self.do_cli(self.command, "afile"))
         d.addCallback(_check)
         return d
 
-    def test_rm_with_nonexistent_alias(self):
-        # 'tahoe rm' should behave sensibly when invoked with an explicit
+    def test_unlink_with_nonexistent_alias(self):
+        # 'tahoe unlink' should behave sensibly when invoked with an explicit
         # alias that doesn't exist.
-        self.basedir = "cli/Rm/rm_with_nonexistent_alias"
+        self.basedir = "cli/Unlink/%s_with_nonexistent_alias" % (self.command,)
         self.set_up_grid()
-        d = self.do_cli("rm", "nonexistent:afile")
+        d = self.do_cli(self.command, "nonexistent:afile")
         def _check((rc, out, err)):
             self.failUnlessReallyEqual(rc, 1)
             self.failUnlessIn("error:", err)
@@ -2977,31 +2979,37 @@ class Rm(GridTestMixin, CLITestMixin, unittest.TestCase):
             self.failUnlessReallyEqual(out, "")
         d.addCallback(_check)
 
-        d.addCallback(lambda ign: self.do_cli("unlink", "nonexistent:afile"))
+        d.addCallback(lambda ign: self.do_cli(self.command, "nonexistent:afile"))
         d.addCallback(_check)
         return d
 
-    def test_rm_without_path(self):
-        # 'tahoe rm' should give a sensible error message when invoked without a path.
-        self.basedir = "cli/Rm/rm_without_path"
+    def test_unlink_without_path(self):
+        # 'tahoe unlink' should give a sensible error message when invoked without a path.
+        self.basedir = "cli/Unlink/%s_without_path" % (self.command,)
         self.set_up_grid()
         self._create_test_file()
         d = self.do_cli("create-alias", "tahoe")
         d.addCallback(lambda ign: self.do_cli("put", self.datafile, "tahoe:test"))
-        def _do_rm((rc, out, err)):
+        def _do_unlink((rc, out, err)):
             self.failUnlessReallyEqual(rc, 0)
             self.failUnless(out.startswith("URI:"), out)
-            return self.do_cli("rm", out.strip('\n'))
-        d.addCallback(_do_rm)
+            return self.do_cli(self.command, out.strip('\n'))
+        d.addCallback(_do_unlink)
 
         def _check((rc, out, err)):
             self.failUnlessReallyEqual(rc, 1)
+            self.failUnlessIn("'tahoe %s'" % (self.command,), err)
             self.failUnlessIn("path must be given", err)
             self.failUnlessReallyEqual(out, "")
         d.addCallback(_check)
         return d
 
 
+class Rm(Unlink):
+    """Test that 'tahoe rm' behaves in the same way as 'tahoe unlink'."""
+    command = "rm"
+
+
 class Stats(GridTestMixin, CLITestMixin, unittest.TestCase):
     def test_empty_directory(self):
         self.basedir = "cli/Stats/empty_directory"
-- 
2.45.2