rtteardown.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 rtteardown library.
00018 
00019 '''
00020 
00021 
00022 import optparse
00023 import os
00024 import os.path
00025 import rtctree.path
00026 import rtctree.tree
00027 import rtsprofile.rts_profile
00028 import sys
00029 import traceback
00030 
00031 import actions
00032 import option_store
00033 import rtshell
00034 
00035 
00036 def disconnect_actions(rtsprofile):
00037     disconnects = []
00038     for conn in rtsprofile.data_port_connectors:
00039         source_comp = rtsprofile.find_comp_by_target(conn.source_data_port)
00040         source_path = '/' + source_comp.path_uri
00041         source_port = conn.source_data_port.port_name
00042         prefix = source_comp.instance_name + '.'
00043         if source_port.startswith(prefix):
00044             source_port = source_port[len(prefix):]
00045         dest_comp = rtsprofile.find_comp_by_target(conn.target_data_port)
00046         dest_path = '/' + dest_comp.path_uri
00047         dest_port =conn.target_data_port.port_name
00048         prefix = dest_comp.instance_name + '.'
00049         if dest_port.startswith(prefix):
00050             dest_port = dest_port[len(prefix):]
00051         disconnects.append(actions.DisconnectPortsAct(source_path, source_port,
00052             dest_path, dest_port, conn.connector_id))
00053 
00054     for conn in rtsprofile.service_port_connectors:
00055         source_comp = rtsprofile.find_comp_by_target(conn.source_service_port)
00056         source_path = '/' + source_comp.path_uri
00057         source_port = conn.source_service_port.port_name
00058         prefix = source_comp.instance_name + '.'
00059         if source_port.startswith(prefix):
00060             source_port = source_port[len(prefix):]
00061         dest_comp = rtsprofile.find_comp_by_target(conn.target_service_port)
00062         dest_path = '/' + dest_comp.path_uri
00063         dest_port = conn.target_service_port.port_name
00064         prefix = dest_comp.instance_name + '.'
00065         if dest_port.startswith(prefix):
00066             dest_port = dest_port[len(prefix):]
00067         disconnects.append(actions.DisconnectPortsAct(source_path, source_port,
00068             dest_path, dest_port, conn.connector_id))
00069     return disconnects
00070 
00071 
00072 def teardown(profile=None, xml=True, dry_run=False, tree=None):
00073     # Load the profile
00074     if profile:
00075         # Read from a file
00076         with open(profile) as f:
00077             if xml:
00078                 rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=f)
00079             else:
00080                 rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=f)
00081     else:
00082         # Read from standard input
00083         lines = sys.stdin.read()
00084         if xml:
00085             rtsp = rtsprofile.rts_profile.RtsProfile(xml_spec=lines)
00086         else:
00087             rtsp = rtsprofile.rts_profile.RtsProfile(yaml_spec=lines)
00088 
00089     # Build a list of actions to perform that will destroy the system
00090     actions = disconnect_actions(rtsp)
00091     if dry_run:
00092         for a in actions:
00093             print a
00094     else:
00095         if not tree:
00096             # Load the RTC Tree, using the paths from the profile
00097             tree = rtctree.tree.RTCTree(paths=[rtctree.path.parse_path(
00098                 '/' + c.path_uri)[0] for c in rtsp.components])
00099         for a in actions:
00100             a(tree)
00101 
00102 
00103 def main(argv=None, tree=None):
00104     usage = '''Usage: %prog [options] [RTSProfile file]
00105 Destroy an RT system using an RTSProfile.'''
00106     parser = optparse.OptionParser(usage=usage, version=rtshell.RTSH_VERSION)
00107     parser.add_option('--dry-run', dest='dry_run', action='store_true',
00108             default=False,
00109             help="Print what will be done but don't actually do anything. \
00110 [Default: %default]")
00111     parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
00112             default=False, help='Verbose output. [Default: %default]')
00113     parser.add_option('-x', '--xml', dest='xml', action='store_true',
00114             default=True, help='Use XML input format. [Default: True]')
00115     parser.add_option('-y', '--yaml', dest='xml', action='store_false',
00116             help='Use YAML input format. [Default: False]')
00117 
00118     if argv:
00119         sys.argv = [sys.argv[0]] + argv
00120     try:
00121         options, args = parser.parse_args()
00122     except optparse.OptionError, e:
00123         print >>sys.stderr, 'OptionError: ', e
00124         return 1
00125     option_store.OptionStore().verbose = options.verbose
00126 
00127 
00128     if not args:
00129         profile = None
00130     elif len(args) == 1:
00131         profile = args[0]
00132     else:
00133         print >>sys.stderr, usage
00134         return 1
00135 
00136     try:
00137         teardown(profile=profile, xml=options.xml, dry_run=options.dry_run,
00138                 tree=tree)
00139     except Exception, e:
00140         if options.verbose:
00141             traceback.print_exc()
00142         print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00143         return 1
00144     return 0
00145 
00146 
00147 # vim: tw=79
00148 


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