BASIS  version 1.2.3 (revision 2104)
executabletargetinfo.py
00001 ##############################################################################
00002 # @file  executabletargetinfo.py
00003 # @brief Provides information about executables built by BASIS.
00004 #
00005 # @note The executabletargetinfo.py module is automatically created by
00006 #       BASIS from the template file executabletargetinfo.py.in which is
00007 #       part of the BASIS installation.
00008 #
00009 # Copyright (c) 2011 University of Pennsylvania. All rights reserved.<br />
00010 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00011 #
00012 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00013 #
00014 # @ingroup BasisPythonUtilities
00015 ##############################################################################
00016 
00017 """
00018 Provides information about executables built by BASIS.
00019 
00020 Copyright (c) 2011 University of Pennsylvania. All rights reserved.
00021 See COPYING file or https://www.rad.upenn.edu/sbia/software/license.html.
00022 
00023 Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00024 
00025 """
00026 
00027 # ============================================================================
00028 # modules
00029 # ============================================================================
00030 
00031 import os.path # path functions
00032 import sys     # argv[0]
00033 
00034 from sbia.basis.which import which, WhichError
00035 
00036 # ============================================================================
00037 # global variables
00038 # ============================================================================
00039 
00040 _module_dir = os.path.realpath(os.path.dirname(__file__))
00041 _locations  = None
00042 
00043 # ============================================================================
00044 # public function
00045 # ============================================================================
00046 
00047 ## @addtogroup BasisPythonUtilities
00048 # @{
00049 
00050 
00051 # ----------------------------------------------------------------------------
00052 def get_target_uid(target):
00053     """
00054     Get UID of build target.
00055     
00056     The UID of a build target is its name prepended by a namespace identifier
00057     which should be unique for each project.
00058     
00059     This function further initializes the dictionaries storing the information
00060     about the executable targets upon the first invocation. Reason to do it
00061     here is that every access to the dictionaries first calls this function
00062     to get the UID of a build target. Moreover, also this function needs to
00063     have the already initialized dictionaries to ensure that an already valid
00064     target identifier is not modified.
00065     
00066     """
00067     global _locations
00068     
00069     # initialize module if not done yet - this is only done here because
00070     # whenever information is looked up about an executable target, this
00071     # function is invoked first
00072     if _locations is None:
00073         _initialize()
00074     # handle invalid arguments
00075     if target is None or target == "":
00076         return None
00077     # in case of a leading namespace separator, do not modify target name
00078     if target.startswith('.'):
00079         return target
00080     # project namespace
00081     prefix = 'sbia.basis'
00082     # try prepending namespace or parts of it until target is known
00083     separator = '.'
00084     while True:
00085         if separator.join([prefix, target]) in _locations:
00086             return separator.join([prefix, target])
00087         parts = prefix.split(separator, 1)
00088         if len(parts) == 1:
00089             break
00090         prefix = parts[0]
00091     # otherwise, return target name unchanged
00092     return target
00093 
00094 # ----------------------------------------------------------------------------
00095 def is_known_target(target):
00096     """Determine whether a given build target is known by this module."""
00097     global _locations
00098     uid = get_target_uid(target)
00099     if uid is None or uid == "":
00100         return False
00101     if uid.startswith('.'):
00102         uid = uid[1:]
00103     return uid in _locations
00104 
00105 # ----------------------------------------------------------------------------
00106 def get_executable_name(target=None):
00107     """
00108     Get name of executable file built by given target. If no target is
00109     specified, the name of this executable is returned.
00110     
00111     """
00112     path = get_executable_path(target)
00113     if path is None:
00114         return None
00115     return os.path.basename(path)
00116 
00117 # ----------------------------------------------------------------------------
00118 def get_executable_directory(target=None):
00119     """
00120     Get directory of executable file built by given target. If no target is
00121     specified, the directory of this executable is returned.
00122     
00123     """
00124     path = get_executable_path(target)
00125     if path is None:
00126         return None
00127     return os.path.dirname(path)
00128 
00129 # ----------------------------------------------------------------------------
00130 def get_executable_path(target=None):
00131     """
00132     Get absolute path of executable file.
00133     
00134     This function determines the absolute file path of an executable. If no
00135     arguments are given, the absolute path of this executable is returned.
00136     If the given argument is a known build target name, the absolute path
00137     of the executable built by this target is returned. Otherwise, the named
00138     command is searched in the system PATH and it's absolute path returned
00139     if found. If the given argument is neither the name of a known build target
00140     nor an executable found on the PATH, undef is returned.
00141     
00142     """
00143     global _module_dir
00144     global _locations
00145     
00146     path = None
00147     if target is None:
00148         path = os.path.realpath(sys.argv [0])
00149         if os.path.isdir(path):
00150             path += "/<stdin>" # interactive shell
00151     else:
00152         uid = get_target_uid(target)
00153         if uid is not None and uid.startswith('.'):
00154             uid = uid[1:]
00155         if uid is not None and uid in _locations:
00156             path = os.path.normpath(os.path.join(_module_dir, _locations[uid]))
00157             if '$(IntDir)' in path:
00158                 for intdir in ['Release', 'Debug', 'RelWithDebInfo', 'MinSizeRel']:
00159                     tmppath = path.replace('$(IntDir)', intdir)
00160                     if os.path.exists(tmppath):
00161                         path = tmppath
00162                         break
00163                 path = path.replace('$(IntDir)', '')
00164         else:
00165             try:
00166                 path = which (target)
00167             except WhichError:
00168                 pass
00169     
00170     return path
00171 
00172 
00173 ## @}
00174 # end of Doxygen group
00175 
00176 # ============================================================================
00177 # private functions
00178 # ============================================================================
00179 
00180 # ----------------------------------------------------------------------------
00181 def _initialize():
00182     """Initialize dictionaries with information about executable targets."""
00183     global _locations
00184     
00185     _locations = {
00186         'sbia.basis.basisproject' : '../../../../bin/basisproject',
00187         'sbia.basis.which' : '../../../../bin/which',
00188         'sbia.basis.doxyfilter' : '../../../doxyfilter',
00189         'sbia.basis.doxyfilter-matlab' : '../../../doxyfilter-matlab',
00190         'sbia.basis.doxyfilter-bash' : '../../../doxyfilter-bash',
00191         'sbia.basis.doxyfilter-cmake' : '../../../doxyfilter-cmake',
00192         'sbia.basis.doxyfilter-python' : '../../../doxyfilter-python',
00193         'sbia.basis.testdriver' : '../../../../bin/basistest-driver',
00194         'sbia.basis.basistest-svn' : '../../../../bin/basistest-svn',
00195         'sbia.basis.basistest-slave' : '../../../../bin/basistest-slave',
00196         'sbia.basis.basistest-master' : '../../../../bin/basistest-master',
00197         'sbia.basis.basistest-cron' : '../../../../bin/basistest-cron',
00198         'sbia.basis.basistest' : '../../../../bin/basistest',
00199         'sbia.basis.make_html_verbatim' : '../../../../bin/make_html_verbatim',
00200         'sbia.basis.dummy_command' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/dummy_command',
00201         'sbia.basis.test_matlabtools' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_matlabtools',
00202         'sbia.basis.test_basisproject' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_basisproject',
00203         'sbia.basis.test_path' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_path',
00204         'sbia.basis.test_subprocess' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_subprocess',
00205         'sbia.basis.test_core' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_core',
00206         'sbia.basis.test_shtap' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_shtap',
00207         'sbia.basis.parseargs' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/parseargs',
00208         'sbia.basis.test_utilities' : '../../../../../../../../../home/schuha/sandbox/build/basis-1.2.3/Testing/bin/test_utilities',
00209     }