def os_pwd(echo_out=True):
    """ Perform an simple pwd shell command and dump output to screen.

        Args:
            echo_out (bool): echo output to console [True]

        Returns:
            (str) Output from pwd command
    """
    cmd = "pwd"
    rtn = OSCommand(cmd).run()
    if echo_out:
        log("{0}".format(rtn.output()))
    return rtn.output()
def os_cat(filepath, echo_out=True):
    """ Perform an simple cat shell command and dump output to screen.

        Args:
            filepath (str): Path to file to cat

        Returns:
            (str) Output from cat command
    """
    check_param_type("filepath", filepath, str)
    cmd = "cat {0}".format(filepath)
    rtn = OSCommand(cmd).run()
    if echo_out:
        log("{0}".format(rtn.output()))
    return rtn.output()
def os_ls(directory=".", echo_out=True):
    """ Perform an simple ls -lia shell command and dump output to screen.

        Args:
            directory (str): Directory to run in [.]
            echo_out (bool): echo output to console [True]

        Returns:
            (str) Output from ls command
    """
    check_param_type("directory", directory, str)
    cmd = "ls -lia {0}".format(directory)
    rtn = OSCommand(cmd).run()
    if echo_out:
        log("{0}".format(rtn.output()))
    return rtn.output()
def os_simple_command(os_cmd, run_dir=None):
    """ Perform an simple os command and return a tuple of the (rtncode, rtnoutput).

        NOTE: Simple command cannot have pipes or redirects

        Args:
            os_cmd (str): Command to run
            run_dir (str): Directory where to run the command; if None (defaut)
                           current working directory is used.

        Returns:
            (tuple) Returns a tuple of the (rtncode, rtnoutput) of types (int, str)
    """
    check_param_type("os_cmd", os_cmd, str)
    if run_dir is not None:
        check_param_type("run_dir", run_dir, str)
    rtn = OSCommand(os_cmd, set_cwd=run_dir).run()
    rtn_data = (rtn.result(), rtn.output())
    return rtn_data
def os_test_file(file_path, expression="-e"):
    """ Run a shell 'test' command on a file.

        Args:
            file_path (str): Path to the file to be tested
            expression (str): Test expression [-e = exists]

        Returns:
            (bool) True if test is successful.
    """
    check_param_type("file_path", file_path, str)
    check_param_type("expression", expression, str)
    if os.path.exists(file_path):
        cmd = "test {0} {1}".format(expression, file_path)
        rtn = OSCommand(cmd).run()
        log_debug("Test cmd = {0}; rtn = {1}".format(cmd, rtn.result()))
        return rtn.result() == 0
    else:
        log_error("File {0} does not exist".format(file_path))
        return False
def os_wc(in_file, fields_index_list=[]):
    """ Run a wc (equivalent) command on a file and then extract specific
        fields of the result as a string.

        Args:
            in_file (str): Input string to parse
            fields_index_list (list of ints): A list of ints and in the order of
                                             fields to be returned

        Returns:
            (str) Space separated string of extracted fields.
    """
    check_param_type("in_file", in_file, str)
    check_param_type("fields_index_list", fields_index_list, list)
    for index, field_index in enumerate(fields_index_list):
        check_param_type("field_index - {0}".format(index), field_index, int)
    cmd = "wc {0}".format(in_file)
    rtn = OSCommand(cmd).run()
    wc_out = rtn.output()
    if fields_index_list:
        wc_out = os_awk_print(wc_out, fields_index_list)
    return wc_out
    def run_sst(self, sdl_file, out_file, err_file=None, set_cwd=None, mpi_out_files="",
                other_args="", num_ranks=None, num_threads=None, global_args=None,
                timeout_sec=60):
        """ Launch sst with with the command line and send output to the
            output file.  The SST execution will be monitored for result errors and
            timeouts.  On an error or timeout, a SSTTestCase.assert() will be generated
            indicating the details of the failure.

            Args:
                sdl_file (str): The FilePath to the test SDL (python) file.
                out_file (str): The FilePath to the finalized output file.
                err_file (str): The FilePath to the finalized error file.
                                Default = same directory as the output file.
                mpi_out_files (str): The FilePath to the mpi run output files.
                                     These will be merged into the out_file at
                                     the end of a multi-rank run.
                other_args (str): Any other arguments used in the SST cmd
                                   that the caller wishes to use.
                num_ranks (int): The number of ranks to run SST with.
                num_threads (int): The number of threads to run SST with.
                global_args (str): Global Arguments provided from test engine args
                timeout_sec (int): Allowed runtime in seconds
        """
        # NOTE: We cannot set the default of param to the global variable due to
        # oddities on how this class loads, so we do it here.
        if num_ranks is None:
            num_ranks = test_engine_globals.TESTENGINE_SSTRUN_NUMRANKS
        if num_threads is None:
            num_threads = test_engine_globals.TESTENGINE_SSTRUN_NUMTHREADS
        if global_args is None:
            global_args = test_engine_globals.TESTENGINE_SSTRUN_GLOBALARGS

        # Make sure arguments are of valid types
        check_param_type("sdl_file", sdl_file, str)
        check_param_type("out_file", out_file, str)
        if err_file is not None:
            check_param_type("err_file", out_file, str)
        if set_cwd is not None:
            check_param_type("set_cwd", set_cwd, str)
        check_param_type("mpi_out_files", mpi_out_files, str)
        check_param_type("other_args", other_args, str)
        if num_ranks is not None:
            check_param_type("num_ranks", num_ranks, int)
        if num_threads is not None:
            check_param_type("num_threads", num_threads, int)
        if global_args is not None:
            check_param_type("global_args", global_args, str)
        if not (isinstance(timeout_sec, (int, float)) and not isinstance(timeout_sec, bool)):
            raise ValueError("ERROR: Timeout_sec must be a postive int or a float")

        # Make sure sdl file is exists and is a file
        if not os.path.exists(sdl_file) or not os.path.isfile(sdl_file):
            log_error("sdl_file {0} does not exist".format(sdl_file))

        # Figure out a name for the mpi_output files if the default is provided
        if mpi_out_files == "":
            mpiout_filename = "{0}.testfile".format(out_file)
        else:
            mpiout_filename = mpi_out_files

        # Set the initial os launch command for sst.
        # If multi-threaded, include the number of threads
        if num_threads > 1:
            oscmd = "sst -n {0} {1} {2} {3}".format(num_threads,
                                                    global_args,
                                                    other_args,
                                                    sdl_file)
        else:
            oscmd = "sst {0} {1} {2}".format(global_args,
                                             other_args,
                                             sdl_file)

        # Update the os launch command if we are running multi-rank
        num_cores = host_os_get_num_cores_on_system()

        # Perform any multi-rank checks/setup
        mpi_avail = False
        numa_param = ""
        if num_ranks > 1:
            # Check to see if mpirun is available
            rtn = os.system("which mpirun > /dev/null 2>&1")
            if rtn == 0:
                mpi_avail = True

            numa_param = "-map-by numa:PE={0}".format(num_threads)

            oscmd = "mpirun -np {0} {1} -output-filename {2} {3}".format(num_ranks,
                                                                         numa_param,
                                                                         mpiout_filename,
                                                                         oscmd)

        # Identify the working directory that we are launching SST from
        final_wd = os.getcwd()
        if set_cwd is not None:
            final_wd = os.path.abspath(set_cwd)

        # Log some debug info on the launch of SST
        log_debug((("-- SST Launch Command (In Dir={0}; Ranks:{1}; Threads:{2};") +
                   (" Num Cores:{3}) = {4}")).format(final_wd, num_ranks, num_threads,
                                                     num_cores, oscmd))

        # Check for OpenMPI
        if num_ranks > 1 and mpi_avail is False:
            log_fatal("OpenMPI IS NOT FOUND/AVAILABLE")

        # Launch SST
        rtn = OSCommand(oscmd, output_file_path = out_file,
                        error_file_path = err_file,
                        set_cwd = set_cwd).run(timeout_sec=timeout_sec)
        if num_ranks > 1:
            testing_merge_mpi_files("{0}*".format(mpiout_filename), mpiout_filename, out_file)

        # Look for runtime error conditions
        err_str = "SST Timed-Out ({0} secs) while running {1}".format(timeout_sec, oscmd)
        self.assertFalse(rtn.timeout(), err_str)
        err_str = "SST returned {0}; while running {1}".format(rtn.result(), oscmd)
        self.assertEqual(rtn.result(), 0, err_str)