#! /usr/bin/env python # # Name: mpscript # # Purpose: This script initializes Mp, loads an MX database, and then runs # a user specified script. # # Author: William Lavender # #--------------------------------------------------------------------------- # # Copyright 2002, 2006, 2008, 2020-2021 Illinois Institute of Technology # # See the file "LICENSE" for information on usage and redistribution # of this file, and for a DISCLAIMER OF ALL WARRANTIES. # import sys, os, traceback if ( len(sys.argv) <= 1 ): print ( "" ) print ( "Usage: %s script_filename script_arg1 script_arg2 ..." \ % ( sys.argv[0] ) ) print ( "" ) sys.exit(1) sys.stdout = sys.stderr # Figure out where the MX installation directory is. try: mxdir = os.environ["MXDIR"] except: mxdir = "/opt/mx" # This is the default location. # Are we using an installed version of Mp or a test version of Mp? try: test = os.environ["MXTEST"] test = int(test) if ( test == 1 ): use_test_version = 1 else: use_test_version = 0 except: use_test_version = 0 # Construct the name of the Mp modules directory. if ( use_test_version ): mp_modules_dir = os.getcwd() mp_modules_dir = os.path.join( mp_modules_dir, "..", "libMp" ) mp_modules_dir = os.path.normpath( mp_modules_dir ) else: mp_modules_dir = os.path.join( mxdir, "lib", "mp" ) mp_modules_dir = os.path.normpath( mp_modules_dir ) # Does the Mp modules directory exist? if ( os.path.isdir( mp_modules_dir ) == 0 ): error_message = \ "The Mp modules directory '%s' does not exist. Aborting..." \ % ( mp_modules_dir ) raise ValueError( error_message ) sys.exit(1) # Add the Mp modules directory to the Python module load path. sys.path[:0] = [ mp_modules_dir ] # Import the Mp module. import Mp # Get the name of the requested script file. script_filename = sys.argv[1] Mp.__program_name__ = script_filename # Try to execute the script file. try: python_major = sys.version_info[0] except: python_major = 1 if ( python_major <= 2 ): execfile( script_filename ) else: exec( open( script_filename ).read() ) # Figure out which MX database to use. try: # First try to get the name from an environment variable. database_filename = os.environ["MXDATABASE"] except: # If the environment variable does not exist, construct # the filename for the default MX database. database_filename = os.path.join( mxdir, "etc", "mxmotor.dat" ) database_filename = os.path.normpath( database_filename ) # Attempt to start up the MX database. record_list = Mp.setup_database( database_filename ) # Set the plot enable flag to 'nowait'. record_list.set_plot_enable(2) # Invoke the main() procedure in the script file. main( record_list, sys.argv[2:] )