rtexit.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 the command to make a component exit.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.tree
00026 import rtctree.path
00027 import RTC
00028 import sys
00029 import traceback
00030 
00031 import path
00032 import rts_exceptions
00033 import rtshell
00034 
00035 
00036 def exit_target(cmd_path, full_path, options, tree=None):
00037     path, port = rtctree.path.parse_path(full_path)
00038     if port:
00039         raise rts_exceptions.NotAComponentError(cmd_path)
00040 
00041     trailing_slash = False
00042     if not path[-1]:
00043         raise rts_exceptions.NotAComponentError(cmd_path)
00044 
00045     if not tree:
00046         tree = rtctree.tree.RTCTree(paths=path, filter=[path])
00047 
00048     if not tree.has_path(path):
00049         raise rts_exceptions.NoSuchObjectError(cmd_path)
00050     object = tree.get_node(path)
00051     if object.is_zombie:
00052         raise rts_exceptions.ZombieObjectError(cmd_path)
00053     if not object.is_component:
00054         raise rts_exceptions.NotAComponentError(cmd_path)
00055 
00056     object.exit()
00057 
00058 
00059 def main(argv=None, tree=None):
00060     usage = '''Usage: %prog [options] <path>
00061 Make a component exit.'''
00062     version = rtshell.RTSH_VERSION
00063     parser = optparse.OptionParser(usage=usage, version=version)
00064     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00065             default=False,
00066             help='Output verbose information. [Default: %default]')
00067 
00068     if argv:
00069         sys.argv = [sys.argv[0]] + argv
00070     try:
00071         options, args = parser.parse_args()
00072     except optparse.OptionError, e:
00073         print >>sys.stderr, 'OptionError:', e
00074         return 1
00075 
00076     if not args:
00077         # If no path given then can't do anything.
00078         print >>sys.stderr, '{0}: No component specified.'.format(
00079                 os.path.basename(sys.argv[0]))
00080         return 1
00081     elif len(args) == 1:
00082         cmd_path = args[0]
00083     else:
00084         print >>sys.stderr, usage
00085         return 1
00086     full_path = path.cmd_path_to_full_path(cmd_path)
00087 
00088     try:
00089         exit_target(cmd_path, full_path, options, tree)
00090     except Exception, e:
00091         if options.verbose:
00092             traceback.print_exc()
00093         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00094         return 1
00095     return 0
00096 
00097 
00098 # vim: tw=79
00099 


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