#! /usr/bin/env python # # Name: mpenv # # Purpose: This script creates an environment in which the Mp module # can run and then executes a user-supplied script. It does # not actually load Mp itself or an MX database. # # 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 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 ] # Execute the supplied script file. script_filename = sys.argv[1] if ( sys.version_info[0] <= 2 ): execfile( script_filename ) else: exec( open( script_filename ).read() )