Go to the documentation of this file.00001
00002
00003
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 Base for the scripts that change component state.
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 alter_component_states(action, paths, options, tree=None):
00036 cmd_paths, fps = zip(*paths)
00037 pathports = [rtctree.path.parse_path(fp) for fp in fps]
00038 for ii, p in enumerate(pathports):
00039 if p[1]:
00040 raise rts_exceptions.NotAComponentError(cmd_paths[ii])
00041 if not p[0][-1]:
00042 raise rts_exceptions.NotAComponentError(cmd_paths[ii])
00043 paths, ports = zip(*pathports)
00044
00045 if not tree:
00046 tree = rtctree.tree.RTCTree(paths=paths, filter=paths)
00047
00048 for ii, p in enumerate(paths):
00049 if not tree.has_path(p):
00050 raise rts_exceptions.NoSuchObjectError(cmd_paths[ii])
00051 object_ = tree.get_node(p)
00052 if object_.is_zombie:
00053 raise rts_exceptions.ZombieObjectError(cmd_paths[ii])
00054 if not object_.is_component:
00055 raise rts_exceptions.NotAComponentError(cmd_paths[ii])
00056 action(object_, options.ec_index)
00057
00058
00059 def base_main(description, action, argv=None, tree=None):
00060 usage = '''Usage: %prog [options] <path> [<path> ...]
00061 {0}'''.format(description)
00062 version = rtshell.RTSH_VERSION
00063 parser = optparse.OptionParser(usage=usage, version=version)
00064 parser.add_option('-e', '--exec_context', dest='ec_index', type='int',
00065 action='store', default=0, help='Index of the execution context '\
00066 'to change state in. [Default: %default]')
00067 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00068 default=False,
00069 help='Output verbose information. [Default: %default]')
00070
00071 if argv:
00072 sys.argv = [sys.argv[0]] + argv
00073 try:
00074 options, args = parser.parse_args()
00075 except optparse.OptionError, e:
00076 print >>sys.stderr, 'OptionError:', e
00077 return 1
00078
00079 if not args:
00080
00081 print >>sys.stderr, '{0}: No components specified.'.format(
00082 os.path.basename(sys.argv[0]))
00083 return 1
00084 paths = [(p, path.cmd_path_to_full_path(p)) for p in args]
00085
00086 try:
00087 alter_component_states(action, paths, options, tree)
00088 except Exception, e:
00089 if options.verbose:
00090 traceback.print_exc()
00091 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00092 return 1
00093 return 0
00094
00095
00096
00097