rtdel.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- Python -*-
00003 # -*- coding: utf-8 -*-
00004 
00005 '''rtshell
00006 
00007 Copyright (C) 2009-2014
00008     Geoffrey Biggs
00009     RT-Synthesis Research Group
00010     Intelligent Systems Research Institute,
00011     National Institute of Advanced Industrial Science and Technology (AIST),
00012     Japan
00013     All rights reserved.
00014 Licensed under the Eclipse Public License -v 1.0 (EPL)
00015 http://www.opensource.org/licenses/eclipse-1.0.txt
00016 
00017 Implementation of deleting an object from a name server.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.tree
00026 import rtctree.path
00027 import sys
00028 import traceback
00029 
00030 import path
00031 import rts_exceptions
00032 import rtshell
00033 
00034 
00035 def delete_object_reference(cmd_path, full_path, options, tree=None):
00036     path, port = rtctree.path.parse_path(full_path)
00037     if port:
00038         raise rts_exceptions.UndeletableObjectError(cmd_path)
00039     if not path[-1]:
00040         path = path[:-1]
00041 
00042     # Cannot delete name servers
00043     if len(path) == 2:
00044         raise rts_exceptions.UndeletableObjectError(cmd_path)
00045 
00046     if not tree:
00047         tree = rtctree.tree.RTCTree(paths=path, filter=[path])
00048 
00049     if options.zombies and not tree.is_zombie(path):
00050         raise rts_exceptions.NotZombieObjectError(cmd_path)
00051 
00052     # There is no point in doing path checks for the path, as the path we are
00053     # deleting may not be in the tree if it's a zombie. Instead, we need to
00054     # find its parent, and use that to remove the name.
00055     parent = tree.get_node(path[:-1])
00056     if parent.is_manager:
00057         raise rts_exceptions.ParentNotADirectoryError(cmd_path)
00058     if not parent.is_directory:
00059         raise rts_exceptions.ParentNotADirectoryError(cmd_path)
00060     parent.unbind(path[-1])
00061 
00062 
00063 def delete_all_zombies(options, tree=None):
00064     if not tree:
00065         tree = rtctree.tree.RTCTree()
00066     if not tree:
00067         return 1
00068     def del_zombie(node, args):
00069         try:
00070             node.parent.unbind(node.name)
00071         except Exception, e:
00072             if options.verbose:
00073                 traceback.print_exc()
00074             print >>sys.stderr, '{0}: {1}'.format(sys.argv[0], e)
00075     tree.iterate(del_zombie, filter=['is_zombie'])
00076 
00077 
00078 def main(argv=None, tree=None):
00079     usage = '''Usage: %prog [options] <path>
00080 Delete an object from a name server.'''
00081     version = rtshell.RTSH_VERSION
00082     parser = optparse.OptionParser(usage=usage, version=version)
00083     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00084             default=False,
00085             help='Output verbose information. [Default: %default]')
00086     parser.add_option('-z', '--zombies', dest='zombies', action='store_true',
00087             default=False, help='Delete only zombies. [Default: %default]')
00088 
00089     if argv:
00090         sys.argv = [sys.argv[0]] + argv
00091     try:
00092         options, args = parser.parse_args()
00093     except optparse.OptionError, e:
00094         print >>sys.stderr, 'OptionError:', e
00095         return 1
00096 
00097     try:
00098         if not args:
00099             if not options.zombies:
00100                 print >>sys.stderr, '{0}: No path given.'.format(sys.argv[0])
00101                 return 1
00102             else:
00103                 # If no path given, delete all zombies found
00104                 delete_all_zombies(options, tree)
00105         elif len(args) == 1:
00106             full_path = path.cmd_path_to_full_path(args[0])
00107             # Some sanity checks
00108             if full_path == '/':
00109                 print >>sys.stderr, '{0}: Cannot delete the root '\
00110                         'directory.'.format(sys.argv[0])
00111                 return 1
00112             delete_object_reference(args[0], full_path, options, tree)
00113         else:
00114             print >>sys.stderr, usage
00115             return 1
00116     except Exception, e:
00117         if options.verbose:
00118             traceback.print_exc()
00119         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00120         return 1
00121     return 0
00122 
00123 
00124 # vim: tw=79
00125 


rtshell
Author(s): Geoffrey Biggs
autogenerated on Fri Aug 28 2015 12:55:12