BASIS  r3148
basis.py
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  basis.py
00003 # @brief BASIS utilities of BASIS package.
00004 #
00005 # @note The basis.py module was automatically created by BASIS from the
00006 #       template file basis.py.in which is part of the BASIS installation.
00007 #
00008 # This module defines BASIS Utilities for Python for the BASIS package,
00009 # i.e., some functions such as print_version() are customized and hence particular
00010 # to this software package.
00011 #
00012 # Only this module should be imported in scripts and modules of the
00013 # BASIS package. For use of these utility functions outside a
00014 # particular BASIS-based project, use the project-independent utilities
00015 # defined by the sbia.basis.utilities module instead.
00016 #
00017 # Copyright (c) 2011, 2012, 2013 University of Pennsylvania. All rights reserved.<br />
00018 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00019 #
00020 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00021 #
00022 # @ingroup BasisPythonUtilities
00023 ##############################################################################
00024 
00025 import os
00026 import sys
00027 
00028 
00029 __all__ = [] # use of import * is discouraged
00030 
00031 # The statement "from basis import utilities" (or "import basis.utilities")
00032 # cannot be used here with Python 2.x because the name of this module is
00033 # the same as the name of the basis package. Therefore, load the source
00034 # file of the basis.utilities module explicitly here.
00035 #
00036 # Note: imp.load_source() would be quite more compact. However, the following
00037 #       will also still work with Python 3.1 and 3.2. For Python 3.3 a
00038 #       different implementation might be necessary using loader objects.
00039 import imp
00040 # path to BASIS Utilities modules
00041 _path = os.getenv('BASIS_PYTHONPATH')
00042 if _path is None:
00043     for _syspath in sys.path:
00044         if (os.path.isfile(os.path.join(_syspath, 'basis/utilities.py' )) or
00045             os.path.isfile(os.path.join(_syspath, 'basis/utilities.pyc'))):
00046             _path = _syspath
00047             break
00048 if _path is None:
00049     _path = '..'
00050     if not os.path.isabs(_path):
00051         _path = os.path.realpath(os.path.join(os.path.dirname(__file__), _path))
00052 if (not os.path.isfile(os.path.join(_path, 'basis/utilities.py' )) and
00053     not os.path.isfile(os.path.join(_path, 'basis/utilities.pyc'))):
00054     raise "Module basis.utilities not found at " + _path + "!\nSpecify path using the PYTHONPATH or BASIS_PYTHONPATH environment variable."
00055 # basis/__init__.py must be loaded first
00056 (_file, _filepath, _desc) = imp.find_module('basis', [_path])
00057 try:
00058     basis = imp.load_module('basis', _file, _filepath, _desc)
00059 finally:
00060     if _file: _file.close()
00061 # then we can load basis.utilities
00062 (_file, _filepath, _desc) = imp.find_module('utilities', [os.path.join(_path, 'basis')])
00063 try:
00064     utilities = imp.load_module('basis.utilities', _file, _filepath, _desc)
00065 finally:
00066     if _file: _file.close()
00067 # clean up scope
00068 del _path
00069 del _syspath
00070 del _file
00071 del _filepath
00072 del _desc
00073 del imp
00074 
00075 
00076 ## @addtogroup BasisPythonUtilities
00077 # @{
00078 
00079 
00080 # ============================================================================
00081 # constants
00082 # ============================================================================
00083 
00084 ## @brief Project name.
00085 PROJECT = 'BASIS'
00086 ## @brief Project version.
00087 VERSION = '0.0.0'
00088 ## @brief Project version string used by print_version().
00089 RELEASE = 'r3148'
00090 ## @brief Default copyright of executables.
00091 COPYRIGHT = "2011, 2012, 2013 University of Pennsylvania"
00092 ## @brief Default license of executables.
00093 LICENSE = "See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file."
00094 ## @brief Default contact to use for help output of executables.
00095 CONTACT = "SBIA Group <sbia-software at uphs.upenn.edu>"
00096 
00097 # prefix used to convert target names into target UIDs
00098 _TARGET_UID_PREFIX = 'basis'
00099 # paths of executables build by the targets of this project relative to this module
00100 _EXECUTABLE_TARGETS = {
00101     'basis.basisproject':      '../../../bin/basisproject',
00102     'basis.doxyfilter':        '../../doxyfilter',
00103     'basis.doxyfilter-perl':   '../../doxyfilter-perl',
00104     'basis.testdriver':        '../../testdriver',
00105     'basis.basistest-svn':     '../../basistest-svn',
00106     'basis.basistest-slave':   '../../basistest-slave',
00107     'basis.basistest-master':  '../../basistest-master',
00108     'basis.basistest-cron':    '../../basistest-cron',
00109     'basis.basistest':         '../../../bin/basistest',
00110     'basis.dummy_command':     '../../../bin/dummy_command',
00111     'basis.test_matlabtools':  '',
00112     'basis.test_basisproject': '',
00113     'basis.test_os':           '../../../bin/test_os',
00114     'basis.test_path':         '../../../bin/test_path',
00115     'basis.test_subprocess':   '../../../bin/test_subprocess',
00116     'basis.test_core':         '',
00117     'basis.test_shutilities':  '',
00118     'basis.test_shtap':        '',
00119     'basis.parseargs':         '../../../bin/parseargs',
00120     'basis.test_utilities':    '',
00121 }
00122 # used to make paths relative to this module absolute
00123 _TARGETS_BASE = os.path.dirname(os.path.realpath(__file__))
00124 
00125 # ============================================================================
00126 # executable information
00127 # ============================================================================
00128 
00129 # ----------------------------------------------------------------------------
00130 ## @brief Print contact information.
00131 #
00132 # @param [in] contact Name of contact.
00133 def print_contact(contact=CONTACT):
00134     utilities.print_contact(contact)
00135 
00136 # ----------------------------------------------------------------------------
00137 ## @brief Print version information including copyright and license notices.
00138 #
00139 # @param [in] name      Name of executable. Should not be set programmatically
00140 #                       to the first argument of the @c __main__ module, but
00141 #                       a string literal instead.
00142 # @param [in] version   Version of executable, e.g., release of project
00143 #                       this executable belongs to.
00144 # @param [in] project   Name of project this executable belongs to.
00145 #                       If @c None or an empty string, no project information
00146 #                       is printed.
00147 # @param [in] copyright The copyright notice, excluding the common prefix
00148 #                       "Copyright (c) " and suffix ". All rights reserved.".
00149 #                       If @c None or an empty string, no copyright notice
00150 #                       is printed.
00151 # @param [in] license   Information regarding licensing. If @c None or an empty
00152 #                        string, no license information is printed.
00153 def print_version(name,
00154                   version=RELEASE,
00155                   project=PROJECT,
00156                   copyright=COPYRIGHT,
00157                   license=LICENSE):
00158     utilities.print_version(name, version, project=project, copyright=copyright, license=license)
00159 
00160 # ----------------------------------------------------------------------------
00161 ## @brief Get UID of build target.
00162 #
00163 # The UID of a build target is its name prepended by a namespace identifier
00164 # which should be unique for each project.
00165 #
00166 # @param [in] name    Name of build target.
00167 # @param [in] prefix  Common prefix of targets belonging to this project.
00168 # @param [in] targets Dictionary mapping target UIDs to executable paths.
00169 #
00170 # @returns UID of named build target.
00171 def targetuid(name, prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS):
00172     return utilities.targetuid(name, prefix=prefix, targets=targets)
00173 
00174 # ----------------------------------------------------------------------------
00175 ## @brief Determine whether a given build target is known.
00176 #
00177 # @param [in] name    Name of build target.
00178 # @param [in] prefix  Common prefix of targets belonging to this project.
00179 # @param [in] targets Dictionary mapping target UIDs to executable paths.
00180 #
00181 # @returns Whether the named target is a known executable target.
00182 def istarget(name, prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS):
00183     return utilities.istarget(name, prefix=prefix, targets=targets)
00184 
00185 # ----------------------------------------------------------------------------
00186 ## @brief Get absolute path of executable file.
00187 #
00188 # This function determines the absolute file path of an executable. If no
00189 # arguments are given, the absolute path of this executable is returned.
00190 # If the command names a known executable build target, the absolute path to
00191 # the corresonding built (and installed) executable file is returned.
00192 # Otherwise, the named command is searched in the system @c PATH and its
00193 # absolute path returned if found. If the executable is not found, @c None
00194 # is returned.
00195 #
00196 # @param [in] name    Name of command or @c None.
00197 # @param [in] prefix  Common prefix of targets belonging to this project.
00198 # @param [in] targets Dictionary mapping target UIDs to executable paths.
00199 # @param [in] base    Base directory for relative paths in @p targets.
00200 #
00201 # @returns Absolute path of executable or @c None if not found.
00202 #          If @p name is @c None, the path of this executable is returned.
00203 def exepath(name=None, prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS, base=_TARGETS_BASE):
00204     return utilities.exepath(name, prefix=prefix, targets=targets, base=base)
00205 
00206 # ----------------------------------------------------------------------------
00207 ## @brief Get name of executable file.
00208 #
00209 # @param [in] name    Name of command or @c None.
00210 # @param [in] prefix  Common prefix of targets belonging to this project.
00211 # @param [in] targets Dictionary mapping target UIDs to executable paths.
00212 # @param [in] base    Base directory for relative paths in @p targets.
00213 #
00214 # @returns Name of executable file or @c None if not found.
00215 #          If @p name is @c None, the name of this executable is returned.
00216 def exename(name=None, prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS, base=_TARGETS_BASE):
00217     return utilities.exename(name, prefix=prefix, targets=targets, base=base)
00218 
00219 # ----------------------------------------------------------------------------
00220 ## @brief Get directory of executable file.
00221 #
00222 # @param [in] name    Name of command or @c None.
00223 # @param [in] prefix  Common prefix of targets belonging to this project.
00224 # @param [in] targets Dictionary mapping target UIDs to executable paths.
00225 # @param [in] base    Base directory for relative paths in @p targets.
00226 #
00227 # @returns Absolute path of directory containing executable or @c None if not found.
00228 #         If @p name is @c None, the directory of this executable is returned.
00229 def exedir(name=None, prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS, base=_TARGETS_BASE):
00230     return utilities.exedir(name, prefix=prefix, targets=targets, base=base)
00231 
00232 # ============================================================================
00233 # command execution
00234 # ============================================================================
00235 
00236 # ----------------------------------------------------------------------------
00237 ## @brief Exception thrown when command execution failed.
00238 #
00239 # @sa sbia.basis.utilities.SubprocessError
00240 SubprocessError = utilities.SubprocessError
00241 
00242 # ----------------------------------------------------------------------------
00243 ## @brief Convert array of arguments to quoted string.
00244 #
00245 # @param [in] args Array of arguments.
00246 #
00247 # @returns Double quoted string, i.e., string where arguments are separated
00248 #          by a space character and surrounded by double quotes if necessary.
00249 #          Double quotes within an argument are escaped with a backslash.
00250 #
00251 # @sa qsplit()
00252 tostring = utilities.tostring
00253 
00254 # ----------------------------------------------------------------------------
00255 ## @brief Split quoted string of arguments.
00256 #
00257 # @param [in] args Quoted string of arguments.
00258 #
00259 # @returns Array of arguments.
00260 #
00261 # @sa tostring()
00262 qsplit = utilities.qsplit
00263 
00264 # ----------------------------------------------------------------------------
00265 ## @brief Execute command as subprocess.
00266 #
00267 # @param [in] args       Command with arguments given either as quoted string
00268 #                        or array of command name and arguments. In the latter
00269 #                        case, the array elements are converted to strings
00270 #                        using the built-in str() function. Hence, any type
00271 #                        which can be converted to a string is permitted.
00272 #                        The first argument must be the name or path of the
00273 #                        executable of the command.
00274 # @param [in] quiet      Turns off output of @c stdout of child process to
00275 #                        stdout of parent process.
00276 # @param [in] stdout     Whether to return the command output.
00277 # @param [in] allow_fail If true, does not raise an exception if return
00278 #                        value is non-zero. Otherwise, a @c SubprocessError is
00279 #                        raised by this function.
00280 # @param [in] verbose    Verbosity of output messages.
00281 #                        Does not affect verbosity of executed command.
00282 # @param [in] simulate   Whether to simulate command execution only.
00283 # @param [in] prefix     Common prefix of build targets belonging to this project.
00284 # @param [in] targets    Dictionary which maps build target names to
00285 #                        executable file paths. The code to initialize
00286 #                        this dictionary is generated by BASIS.
00287 # @param [in] base       Base directory for relative paths in @p targets.
00288 #
00289 # @return The exit code of the subprocess if @p stdout is false (the default).
00290 #         Otherwise, if @p stdout is true, a tuple consisting of exit code
00291 #         command output is returned. Note that if @p allow_fail is false,
00292 #         the returned exit code will always be 0.
00293 #
00294 # @throws SubprocessError If command execution failed. This exception is not
00295 #                         raised if the command executed with non-zero exit
00296 #                         code but @p allow_fail set to @c True.
00297 def execute(args, quiet=False, stdout=False, allow_fail=False, verbose=0, simulate=False,
00298                   prefix=_TARGET_UID_PREFIX, targets=_EXECUTABLE_TARGETS, base=_TARGETS_BASE):
00299     return utilities.execute(args, quiet=quiet,
00300                                    stdout=stdout,
00301                                    allow_fail=allow_fail,
00302                                    verbose=verbose,
00303                                    simulate=simulate,
00304                                    prefix=prefix,
00305                                    targets=targets,
00306                                    base=base)
00307 
00308 
00309 ## @}
00310 # end of Doxygen group