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 rtstart library.
00018
00019 '''
00020
00021
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.component
00026 import rtctree.path
00027 import rtctree.tree
00028 import rtsprofile.rts_profile
00029 import sys
00030 import traceback
00031
00032 import actions
00033 import option_store
00034 import plan
00035 import rts_exceptions
00036 import rtshell
00037
00038
00039 def check_required_component_actions(rtsprofile):
00040 checks = []
00041
00042
00043 for comp in [c for c in rtsprofile.components if c.is_required]:
00044 checks.append(actions.CheckForRequiredCompAct('/' + comp.path_uri,
00045 comp.id, comp.instance_name,
00046 callbacks=[actions.RequiredActionCB()]))
00047 return checks
00048
00049
00050 def activate_actions(rtsprofile):
00051 checks = check_required_component_actions(rtsprofile)
00052
00053 activates = []
00054 for comp in [c for c in rtsprofile.components if c.is_required]:
00055 for ec in comp.execution_contexts:
00056 activates.append(actions.ActivateCompAct('/' + comp.path_uri,
00057 comp.id, comp.instance_name, ec.id,
00058 callbacks=[actions.RequiredActionCB()]))
00059
00060 for comp in [c for c in rtsprofile.components if not c.is_required]:
00061 for ec in comp.execution_contexts:
00062 activates.append(actions.ActivateCompAct('/' + comp.path_uri,
00063 comp.id, comp.instance_name, ec.id))
00064
00065 return checks, activates
00066
00067
00068 def start(profile=None, xml=True, dry_run=False, tree=None):
00069
00070 if profile:
00071
00072 with open(profile) as f:
00073 if xml:
00074 rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=f)
00075 else:
00076 rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=f)
00077 else:
00078
00079 lines = sys.stdin.read()
00080 if xml:
00081 rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=lines)
00082 else:
00083 rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=lines)
00084
00085
00086 checks, activates = activate_actions(rtsp)
00087 p = plan.Plan()
00088 p.make(rtsp, activates, rtsp.activation,
00089 rtctree.component.Component.ACTIVE)
00090 if dry_run:
00091 for a in checks:
00092 print a
00093 print p
00094 else:
00095 if not tree:
00096
00097 tree = rtctree.tree.RTCTree(paths=[rtctree.path.parse_path(
00098 '/' + c.path_uri)[0] for c in rtsp.components])
00099 try:
00100 for a in checks:
00101 a(tree)
00102 p.execute(tree)
00103 except rts_exceptions.RequiredActionFailedError:
00104 p.cancel()
00105 raise
00106
00107
00108 def main(argv=None, tree=None):
00109 usage = '''Usage: %prog [options] [RTSProfile file]
00110 Start an RT system using an RTSProfile.'''
00111 parser = optparse.OptionParser(usage=usage, version=rtshell.RTSH_VERSION)
00112 parser.add_option('--dry-run', dest='dry_run', action='store_true',
00113 default=False, help="Print what will be done but don't actually "
00114 "do anything. [Default: %default]")
00115 parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00116 default=False,
00117 help='Output verbose information. [Default: %default]')
00118 parser.add_option('-x', '--xml', dest='xml', action='store_true',
00119 default=True, help='Use XML input format. [Default: True]')
00120 parser.add_option('-y', '--yaml', dest='xml', action='store_false',
00121 help='Use YAML input format. [Default: False]')
00122
00123 if argv:
00124 sys.argv = [sys.argv[0]] + argv
00125 try:
00126 options, args = parser.parse_args()
00127 except optparse.OptionError, e:
00128 print >>sys.stderr, 'OptionError: ', e
00129 return 1
00130 option_store.OptionStore().verbose = options.verbose
00131
00132 if not args:
00133 profile = None
00134 elif len(args) == 1:
00135 profile = args[0]
00136 else:
00137 print >>sys.stderr, usage
00138 return 1
00139
00140 try:
00141 start(profile=profile, xml=options.xml, dry_run=options.dry_run,
00142 tree=tree)
00143 except Exception, e:
00144 if options.verbose:
00145 traceback.print_exc()
00146 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00147 return 1
00148 return 0
00149
00150
00151
00152