rtstop.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 rtstop 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 deactivate_actions(rtsprofile):
00040     deactivates = []
00041     for comp in rtsprofile.components:
00042         for ec in comp.execution_contexts:
00043             deactivates.append(actions.DeactivateCompAct(
00044                 '/' + comp.path_uri, comp.id, comp.instance_name, ec.id))
00045     return deactivates
00046 
00047 
00048 def stop(profile=None, xml=True, dry_run=False, tree=None):
00049     # Load the profile
00050     if profile:
00051         # Read from a file
00052         with open(profile) as f:
00053             if xml:
00054                 rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=f)
00055             else:
00056                 rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=f)
00057     else:
00058         # Read from standard input
00059         lines = sys.stdin.read()
00060         if xml:
00061             rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=lines)
00062         else:
00063             rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=lines)
00064 
00065     # Build a list of actions to perform that will stop the system
00066     deactivates = deactivate_actions(rtsp)
00067     p = plan.Plan()
00068     p.make(rtsp, deactivates, rtsp.deactivation,
00069             rtctree.component.Component.INACTIVE)
00070     if dry_run:
00071         print p
00072     else:
00073         if not tree:
00074             # Load the RTC Tree, using the paths from the profile
00075             tree = rtctree.tree.RTCTree(paths=[rtctree.path.parse_path(
00076                 '/' + c.path_uri)[0] for c in rtsp.components])
00077         try:
00078             p.execute(tree)
00079         except rts_exceptions.RequiredActionFailedError, e:
00080             p.cancel()
00081             raise
00082 
00083 
00084 def main(argv=None, tree=None):
00085     usage = '''Usage: %prog [options] [RTSProfile file]
00086 Stop an RT system using an RTSProfile.'''
00087     parser = optparse.OptionParser(usage=usage, version=rtshell.RTSH_VERSION)
00088     parser.add_option('--dry-run', dest='dry_run', action='store_true',
00089             default=False, help="Print what will be done but don't actually "
00090             "do anything. [Default: %default]")
00091     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00092             default=False,
00093             help='Output verbose information. [Default: %default]')
00094     parser.add_option('-x', '--xml', dest='xml', action='store_true',
00095             default=True, help='Use XML input format. [Default: True]')
00096     parser.add_option('-y', '--yaml', dest='xml', action='store_false',
00097             help='Use YAML input format. [Default: False]')
00098 
00099     if argv:
00100         sys.argv = [sys.argv[0]] + argv
00101     try:
00102         options, args = parser.parse_args()
00103     except optparse.OptionError, e:
00104         print >>sys.stderr, 'OptionError: ', e
00105         return 1
00106     option_store.OptionStore().verbose = options.verbose
00107 
00108     if not args:
00109         profile = None
00110     elif len(args) == 1:
00111         profile = args[0]
00112     else:
00113         print >>sys.stderr, usage
00114         return 1
00115 
00116     try:
00117         stop(profile=profile, xml=options.xml, dry_run=options.dry_run,
00118                 tree=tree)
00119     except Exception, e:
00120         if options.verbose:
00121             traceback.print_exc()
00122         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00123         return 1
00124     return 0
00125 
00126 
00127 # vim: tw=79
00128 


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