Exemple #1
0
def run_studymanager_command(_c, _log, pythondir=None):
    """
    Run command with arguments.
    Redirection of the stdout or stderr of the command.
    """
    assert type(_c) == str or type(_c) == unicode

    log.debug("run_studymanager_command: %s" % _c)

    try:
        _log.seek(0, os.SEEK_END)
    except:
        _log.seek(0, 2)

    def __text(_t):
        return "\n\nExecution failed --> %s: %s" \
                "\n - command: %s"                \
                "\n - directory: %s\n\n" %        \
                (_t, str(retcode), _c, os.getcwd())

    _l = ""

    cmd = separate_args(_c)

    env = os.environ.copy()

    if pythondir:
        pythondir = enquote_arg(pythondir)
        pythonpath = pythondir + ':' + env.get("PYTHONPATH", '')
        env.update([("PYTHONPATH", pythonpath)])

    try:
        t1 = time.time()
        retcode = run_command(cmd, stdout=_log, stderr=_log, env=env)
        t2 = time.time()

        if retcode < 0:
            _l = __text("command was terminated by signal")
        elif retcode > 0:
            _l = __text("command return")

    except OSError:
        import traceback
        import sys
        exc_info = sys.exc_info()
        bt = traceback.format_exception(*exc_info)
        for l in bt:
            _log.write(l)
        retcode = 0
        del exc_info
        _l = __text("unknown command")
        t1 = 0.
        t2 = 0.

    _log.flush()
    if _l:
        _log.write(_l)

    return retcode, "%.2f" % (t2 - t1)
Exemple #2
0
def main(argv, pkg):
    """
    Main function.
    """

    options, args = process_cmd_line(argv, pkg)

    if not args:
        return 1

    cmd = [pkg.get_io_dump()]
    cmd += argv

    # Set environment modules if present

    cs_exec_environment.set_modules(pkg)
    cs_exec_environment.source_rcfile(pkg)

    # Run executable

    return cs_exec_environment.run_command(cmd)
Exemple #3
0
    def link_obj(self,
                 exec_name,
                 obj_files=None,
                 opt_libs=None,
                 stdout=sys.stdout,
                 stderr=sys.stderr):
        """
        Link function.
        """
        retval = 0

        # Short names

        pkg = self.pkg

        # Directories

        call_dir = None
        temp_dir = None

        o_files = obj_files

        p_libs = self.get_flags('libs')

        # Special handling for some linkers (such as Mac OS X), for which
        # no multiple definitions are allowable in static mode;
        # in this case, extract archive, then overwrite with user files.

        if pkg.config.special_user_link == 'ar_x':

            call_dir = os.getcwd()
            temp_dir = tempfile.mkdtemp(suffix=".cs_link")
            os.chdir(temp_dir)

            lib0 = os.path.join(self.get_ar_lib_dir(),
                                'lib' + p_libs[0][2:] + '.a')
            p_libs = p_libs[1:]
            cmd = ['ar', 'x', lib0]
            if run_command(
                    cmd, pkg=pkg, echo=True, stdout=stdout,
                    stderr=stderr) != 0:
                retval = 1

            if obj_files:
                import shutil
                for f in obj_files:
                    if os.path.isabs(f):
                        f_src = f
                    else:
                        f_src = os.path.join(call_dir, f)
                    shutil.copy2(f_src, temp_dir)

            dir_files = os.listdir(os.getcwd())
            o_files = fnmatch.filter(dir_files, '*.o')

        # Prepare link command

        cmd = [self.get_compiler('ld')]
        cmd += ["-o", exec_name]
        if o_files:
            cmd += o_files

        # If present, address sanitizer needs to come first

        if '-lasan' in p_libs:
            p_libs.remove('-lasan')
            cmd += ['-lasan']

        if os.path.basename(exec_name) in self.pkg.config.exec_libs:
            cmd += [self.pkg.config.exec_libs[os.path.basename(exec_name)]]

        if opt_libs != None:
            if len(opt_libs) > 0:
                cmd += separate_args(opt_libs)
        cmd += self.get_flags('ldflags')
        cmd += p_libs
        if pkg.config.rpath != "":
            cmd += self.so_dirs_path(cmd)

        # Clean paths in link flags
        cmd_new = []
        for c in cmd:
            if c[:2] == '-L':
                c = '-L' + os.path.normpath(c[2:])
                if cmd_new.count(c) < 1:
                    cmd_new.append(c)
            else:
                cmd_new.append(c)
        cmd = cmd_new

        # Call linker

        if retval == 0:
            if run_command(
                    cmd, pkg=pkg, echo=True, stdout=stdout,
                    stderr=stderr) != 0:
                retval = 1

        # Cleanup for special cases

        if temp_dir:
            if not os.path.isabs(exec_name):
                import shutil
                shutil.copy2(exec_name, os.path.join(call_dir, exec_name))
            for f in os.listdir(temp_dir):
                os.remove(os.path.join(temp_dir, f))
            os.chdir(call_dir)
            os.rmdir(temp_dir)

        return retval
Exemple #4
0
    def compile_src(self,
                    src_list=None,
                    opt_cflags=None,
                    opt_cxxflags=None,
                    opt_fcflags=None,
                    keep_going=False,
                    stdout=sys.stdout,
                    stderr=sys.stderr):
        """
        Compilation function.
        """
        retval = 0

        # Short names

        pkg = self.pkg

        # Find files to compile in source path

        c_files = fnmatch.filter(src_list, '*.c')
        h_files = fnmatch.filter(src_list, '*.h')
        cxx_files = fnmatch.filter(src_list, '*.cxx') + fnmatch.filter(
            src_list, '*.cpp')
        hxx_files = fnmatch.filter(src_list, '*.hxx') + fnmatch.filter(
            src_list, '*.hpp')
        f_files = fnmatch.filter(src_list, '*.[fF]90')
        o_files = fnmatch.filter(src_list, '*.o')

        # Determine additional include directories

        c_include_dirs = []
        for f in h_files:
            c_include_dirs.append(os.path.dirname(f))
        c_include_dirs = sorted(set(c_include_dirs))

        cxx_include_dirs = []
        for f in hxx_files:
            cxx_include_dirs.append(os.path.dirname(f))
        cxx_include_dirs = sorted(set(cxx_include_dirs))

        f_include_dirs = []
        for f in f_files:
            f_include_dirs.append(os.path.dirname(f))
        f_include_dirs = sorted(set(cxx_include_dirs))

        # Compile files

        for f in c_files:
            if (retval != 0 and not keep_going):
                break
            cmd = [self.get_compiler('cc')]
            if opt_cflags != None:
                cmd += separate_args(opt_cflags)
            for d in c_include_dirs:
                cmd += ["-I", d]
            cmd.append('-DHAVE_CONFIG_H')
            if os.path.basename(f) == 'cs_base.c':
                cmd += ['-DLOCALEDIR=\\"' + pkg.get_dir('localedir') + '\\"', \
                        '-DPKGDATADIR=\\"' + pkg.get_dir('pkgdatadir') + '\\"']
            cmd += self.get_flags('cppflags')
            cmd += separate_args(pkg.config.flags['cflags'])
            cmd += ["-c", f]
            if run_command(
                    cmd, pkg=pkg, echo=True, stdout=stdout,
                    stderr=stderr) != 0:
                retval = 1
            o_files.append(self.obj_name(f))

        for f in cxx_files:
            if (retval != 0 and not keep_going):
                break
            cmd = [self.get_compiler('cxx')]
            if opt_cxxflags != None:
                cmd += separate_args(opt_cxxflags)
            for d in cxx_include_dirs:
                cmd += ["-I", d]
            for d in c_include_dirs:
                cmd += ["-I", d]
            cmd.append('-DHAVE_CONFIG_H')
            cmd += self.get_flags('cppflags')
            cmd += separate_args(pkg.config.flags['cxxflags'])
            cmd += ["-c", f]
            if run_command(
                    cmd, pkg=pkg, echo=True, stdout=stdout,
                    stderr=stderr) != 0:
                retval = 1
            o_files.append(self.obj_name(f))

        for f in f_files:
            if (retval != 0 and not keep_going):
                break
            cmd = [self.get_compiler('fc')]
            f_base = os.path.basename(f)
            o_name = self.obj_name(f)
            if f_base == 'cs_user_boundary_conditions.f90':
                o_name = "cs_f_user_boundary_conditions.o"
                cmd += ["-o", o_name]
            if f_base == 'cs_user_parameters.f90':
                o_name = "cs_f_user_parameters.o"
                cmd += ["-o", o_name]
            if f_base == 'cs_user_extra_operations.f90':
                o_name = "cs_f_user_extra_operations.o"
                cmd += ["-o", o_name]
            if f_base == 'cs_user_initialization.f90':
                o_name = "cs_f_user_initialization.o"
                cmd += ["-o", o_name]
            if f_base == 'cs_user_physical_properties.f90':
                o_name = "cs_f_user_physical_properties.o"
                cmd += ["-o", o_name]
            if f_base == 'cs_user_porosity.f90':
                o_name = "cs_f_user_porosity.o"
                cmd += ["-o", o_name]
            if opt_fcflags != None:
                cmd += separate_args(opt_fcflags)
            for d in f_include_dirs:
                cmd += ["-I", d]
            if pkg.config.fcmodinclude != "-I":
                cmd += [pkg.config.fcmodinclude, srcdir]
            cmd += ["-I", pkg.get_dir('pkgincludedir')]
            if pkg.config.fcmodinclude != "-I":
                cmd += [pkg.config.fcmodinclude, pkg.get_dir('pkgincludedir')]
            cmd += separate_args(pkg.config.flags['fcflags'])
            cmd += ["-c", f]
            if run_command(
                    cmd, pkg=pkg, echo=True, stdout=stdout,
                    stderr=stderr) != 0:
                retval = 1
            o_files.append(o_name)

        return retval, o_files
def main(argv, pkg):
    """
    Main function.
    """

    # Save PYTHONPATH as CS_SALOME_TOP_PYTHONPATH to avoid issues
    # with launch of code due to PYTHONPATH with extra (cumulative) entries
    # encountered with Python 3.6 (i.e. post Python 3.3, with current
    # import logic).
    pythonpath = os.getenv('PYTHONPATH')
    if not pythonpath:
        pythonpath = ""
    os.environ['CS_SALOME_TOP_PYTHONPATH'] = pythonpath

    template = """\
%(salomeenv)s
CFDSTUDY_ROOT_DIR=%(prefix)s
PYTHONPATH=%(pythondir)s/salome:%(pkgpythondir)s${PYTHONPATH:+:$PYTHONPATH}
export CFDSTUDY_ROOT_DIR PYTHONPATH
%(runsalome)s --modules=%(modules)s
"""

    if pkg.config.have_salome == "no":
        sys.stderr.write("SALOME is not available in this installation.\n")
        sys.exit(1)

    default_modules = "GEOM,SHAPER,SMESH,FIELDS,CFDSTUDY,PARAVIS,YACS,JOBMANAGER,HOMARD,OPENTURNS"

    run_cmd = pkg.config.salome_run
    if not run_cmd:
        run_cmd = "${KERNEL_ROOT_DIR}/bin/salome/envSalome.py python ${KERNEL_ROOT_DIR}/bin/salome/runSalome.py"

    path = pkg.get_dir('pkgpythondir')
    path = path + ":" + pkg.get_dir('pythondir')

    # Test if EOS modules could be imported
    if pkg.config.libs['eos'].have == "yes":
        eosprefix = pkg.config.libs['eos'].prefix
        try:
            from distutils import sysconfig
            eospath = os.path.join(
                sysconfig.get_python_lib(0, 0, prefix=eosprefix), 'eos')
        except Exception:
            eospath = ''
        if eospath:
            if os.path.isdir(eospath) and not eospath in sys.path:
                path = path + ":" + eospath

    prefix = pkg.get_dir('prefix')
    pythondir = pkg.get_dir('pythondir')
    cmd = template % {
        'salomeenv': pkg.config.salome_env,
        'prefix': prefix,
        'pythondir': pythondir,
        'pkgpythondir': path,
        'runsalome': run_cmd,
        'modules': default_modules
    }

    process_cmd_line(argv, pkg)

    retcode = cs_exec_environment.run_command(cmd, stdout=None, stderr=None)