[CalendarServer-changes] [9183] CalendarServer/trunk/calendarserver/tools/shell

source_changes at macosforge.org source_changes at macosforge.org
Tue Apr 24 20:05:35 PDT 2012


Revision: 9183
          http://trac.macosforge.org/projects/calendarserver/changeset/9183
Author:   wsanchez at apple.com
Date:     2012-04-24 20:05:35 -0700 (Tue, 24 Apr 2012)
Log Message:
-----------
Add rm

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/shell/cmd.py
    CalendarServer/trunk/calendarserver/tools/shell/vfs.py

Modified: CalendarServer/trunk/calendarserver/tools/shell/cmd.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/cmd.py	2012-04-25 01:40:25 UTC (rev 9182)
+++ CalendarServer/trunk/calendarserver/tools/shell/cmd.py	2012-04-25 03:05:35 UTC (rev 9183)
@@ -25,6 +25,8 @@
     "Commands",
 ]
 
+from getopt import getopt
+
 #from twisted.python import log
 from twisted.internet.defer import succeed
 from twisted.internet.defer import inlineCallbacks, returnValue
@@ -52,10 +54,18 @@
     Unknown arguments.
     """
     def __init__(self, arguments):
-        Exception.__init__(self, "Unknown arguments: %s" % (arguments,))
+        UsageError.__init__(self, "Unknown arguments: %s" % (arguments,))
         self.arguments = arguments
 
 
+class InsufficientArguments(UsageError):
+    """
+    Insufficient arguments.
+    """
+    def __init__(self):
+        UsageError.__init__(self, "Insufficient arguments.")
+
+
 class CommandsBase(object):
     def __init__(self, protocol):
         self.protocol = protocol
@@ -70,7 +80,7 @@
     # Utilities
     #
 
-    def getTarget(self, tokens):
+    def getTarget(self, tokens, wdFallback=False):
         """
         Pop's the first token from tokens and locates the File
         indicated by that token.
@@ -79,10 +89,13 @@
         if tokens:
             return self.wd.locate(tokens.pop(0).split("/"))
         else:
-            return succeed(self.wd)
+            if wdFallback:
+                return succeed(self.wd)
+            else:
+                return succeed(None)
 
     @inlineCallbacks
-    def getTargets(self, tokens):
+    def getTargets(self, tokens, wdFallback=False):
         """
         For each given C{token}, locate a File to operate on.
         @return: iterable of C{File} objects.
@@ -93,7 +106,10 @@
                 result.append((yield self.wd.locate(token.split("/"))))
             returnValue(result)
         else:
-            returnValue((self.wd,))
+            if wdFallback:
+                returnValue((self.wd,))
+            else:
+                returnValue(())
 
     def commands(self, showHidden=False):
         """
@@ -414,7 +430,7 @@
 
         usage: ls [folder]
         """
-        targets = (yield self.getTargets(tokens))
+        targets = (yield self.getTargets(tokens, wdFallback=True))
         multiple = len(targets) > 0
 
         for target in targets:
@@ -443,7 +459,7 @@
 
         usage: info [folder]
         """
-        target = (yield self.getTarget(tokens))
+        target = (yield self.getTarget(tokens, wdFallback=True))
 
         if tokens:
             raise UnknownArguments(tokens)
@@ -462,7 +478,12 @@
 
         usage: cat target [target ...]
         """
-        for target in (yield self.getTargets(tokens)):
+        targets = (yield self.getTargets(tokens))
+
+        if not targets:
+            raise InsufficientArguments()
+
+        for target in targets:
             if hasattr(target, "text"):
                 text = (yield target.text())
                 self.terminal.write(text)
@@ -470,6 +491,40 @@
     complete_cat = CommandsBase.complete_files
 
 
+    @inlineCallbacks
+    def cmd_rm(self, tokens):
+        """
+        Remove target.
+
+        usage: rm target [target ...]
+        """
+        options, tokens = getopt(tokens, "", ["no-implicit"])
+
+        implicit = True
+
+        for option, value in options:
+            if option == "--no-implicit":
+                # Not in docstring; this is really dangerous.
+                implicit = False
+            else:
+                raise AssertionError("We should't be here.")
+
+        targets = (yield self.getTargets(tokens))
+
+        if not targets:
+            raise InsufficientArguments()
+
+        for target in targets:
+            if hasattr(target, "delete"):
+                target.delete(implicit=implicit)
+            else:
+                self.terminal.write("Can not delete read-only target: %s\n" % (target,))
+
+    cmd_rm.hidden = "Incomplete"
+
+    complete_rm = CommandsBase.complete_files
+
+
     #
     # Principal tools
     #

Modified: CalendarServer/trunk/calendarserver/tools/shell/vfs.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-25 01:40:25 UTC (rev 9182)
+++ CalendarServer/trunk/calendarserver/tools/shell/vfs.py	2012-04-25 03:05:35 UTC (rev 9183)
@@ -566,7 +566,17 @@
 
         returnValue("\n".join(description))
 
+    def delete(self, implicit=True):
+        calendar = self.calendarObject.calendar()
 
+        if implicit:
+            # We need data store-level scheduling support to implement
+            # this.
+            raise NotImplementedError("Delete not implemented.")
+        else:
+            calendar.removeCalendarObjectWithUID(self.uid)
+
+
 class CalendarObject(File):
     """
     Calendar object.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120424/45db5c58/attachment.html>


More information about the calendarserver-changes mailing list