def print_global_vars():

  # This function prints out all of the variables in global_vars along with
  # their values. Note that Python dictionaries do not maintain any sort of
  # order. If we want these to appear in a non-aribtrary order we can do that,
  # but performance will be impacted.

  for var, value in g.global_vars.iteritems():
    g.print_var(var,value)

  return 0
def print_global_vars():

    # This function prints out all of the variables in global_vars along with
    # their values. Note that Python dictionaries do not maintain any sort of
    # order. If we want these to appear in a non-aribtrary order we can do that,
    # but performance will be impacted.

    for var, value in g.global_vars.iteritems():
        g.print_var(var, value)

    return 0
def print_func_name():

  # This function will print out the name of whatever function it's being
  # called from, along with its parameters. We get this information by
  # inspecting the program stack.

  # inspect.stack()[1] indicates the parent frame. [0] would be this frame.
  caller_frame = inspect.stack()[1]
  caller_name = caller_frame[3]
  caller_locals = inspect.getargvalues(caller_frame[0])[3]

  print_time("Executing: " + g.program_name + "::" + caller_name)

  for var, value in caller_locals.iteritems():
    g.print_var(var,value)

  return 0
def print_func_name():

    # This function will print out the name of whatever function it's being
    # called from, along with its parameters. We get this information by
    # inspecting the program stack.

    # inspect.stack()[1] indicates the parent frame. [0] would be this frame.
    caller_frame = inspect.stack()[1]
    caller_name = caller_frame[3]
    caller_locals = inspect.getargvalues(caller_frame[0])[3]

    print_time("Executing: " + g.program_name + "::" + caller_name)

    for var, value in caller_locals.iteritems():
        g.print_var(var, value)

    return 0
def print_runtime():

    if g.quiet:
        return

    # This function is designed to be called at the end of your program.  It will display the run_time of your program.

    # This function relies on global variable start_time being set by the caller at the start of program execution.

    end_time = "%9.9f" % time.time()

    math_string = str(end_time) + " - " + "%9.9f" % g.start_time
    run_time_secs = eval(math_string)

    run_time_secs = "%i" % round(run_time_secs)

    # We've made a Python function to do this, above. No more Bash needed.
    run_time = gdate.convert_secs_to_dhms(run_time_secs)[0]

    g.print_var("run_time", run_time)

    return
def print_runtime():

  if g.quiet:
    return

  # This function is designed to be called at the end of your program.  It will display the run_time of your program.

  # This function relies on global variable start_time being set by the caller at the start of program execution.

  end_time = "%9.9f" % time.time()

  math_string = str(end_time) + " - " + "%9.9f" % g.start_time
  run_time_secs = eval(math_string)


  run_time_secs = "%i" % round(run_time_secs)

  # We've made a Python function to do this, above. No more Bash needed.
  run_time = gdate.convert_secs_to_dhms(run_time_secs)[0]

  g.print_var("run_time", run_time)

  return
def print_system_parms():

  # This prints out some basic system information for gen_print_header().

  # File name.
  g.print_var("command_line",g.program_path)
  # Process ID.
  g.print_var(g.program_name + "_pid",g.system_parms["pid"])
  # PGID.
  g.print_var(g.program_name + "_pgid",g.system_parms["pgid"])
  # User ID.
  g.print_var("uid",str(g.system_parms["userid"]) + " (" + g.system_parms["username"] + ")")
  # Group ID.
  g.print_var("gid",str(g.system_parms["gid"]) + " (" + g.system_parms["username"] + ")")
  # Host name.
  g.print_var("hostname",g.system_parms["hostname"])
  # Display.
  g.print_var("DISPLAY",g.system_parms["display"])

  return 0
Example #8
0
def exec_cmd(cmd_buf,
             stat=False,
             out=False,
             err=False,
             time_out=0,
             max_attempts=1,
             sleep_time=1,
             debug=False,
             bash=False):

    # This is an advanced function for executing a string as a command. It has
    # the capacity to save the output and return it, time the command out, retry
    # the function and sleep between attempts. The rmt_cmd option has been
    # removed, as the "rcmd" function now has a "-rrc" flag that will set the
    # return code automatically. This is less typing for the programmer, so just
    # do that.

    # stat: if this is set, an "issuing" message will be printed prior to
    #   execution.
    # out: if this is set, the output from the command will be printed to the
    #   screen.
    # err: if this is set, a failure message will be printed if the command fails.
    # time_out: if this is non-zero, the function will be timed out after the
    #   specified number of seconds.
    # max_attempts: the total number of times to attempt the command.
    # sleep_time: the number of seconds to wait between each attempt.
    # debug: if this is set, debug information will be printed.
    # bash: if this is set, the function will execute a Bash command instead of
    #   a Python command.

    # This function doesn't use parms in the exact same way as the bash version,
    # but just writing "param=value" works equally well in the function call,
    # i.e. exec_cmd(cmd_buf, param1=value1, param2=value2)

    stat = gopt.bool_convert(stat)
    out = gopt.bool_convert(out)
    err = gopt.bool_convert(err)
    debug = gopt.bool_convert(debug)
    current_attempt = 0

    # Print debug information, if necessary.
    if debug:
        gprint.print_time("Printing debug information for exec_cmd.")
        g.print_var("cmd_buf", cmd_buf)
        g.print_var("stat", stat)
        g.print_var("out", out)
        g.print_var("err", err)
        g.print_var("time_out", time_out)
        g.print_var("max_attempts", max_attempts)
        g.print_var("sleep_time", sleep_time)
        g.print_var("debug", debug)

    # If we're using a time-out variable, then we need to set up a time-out
    # signal handler. No need to fork processes.
    if time_out != 0:
        original_handler = signal.signal(signal.SIGALRM,
                                         gsig.gen_timeout_handler)

    # Print out an issuing message if needed.
    if stat and bash:
        gprint.issuing(cmd_buf)
        if max_attempts == 0:
            gprint.print_time(
                "max_attempts is zero so the preceding command will not actually be executed."
            )

    # Now let's begin the execution.
    status = 0
    output = ""
    while current_attempt < max_attempts:
        status = 0
        if time_out == 0:
            # If we're not timing it out, just run the command.

            status, output = commands.getstatusoutput(cmd_buf)
            if out:
                print output
        else:
            # Otherwise, we need to set an alarm and catch a time-out.
            # We'll actually catch all exceptions, since it's possible we can
            # be executing a Python command.
            signal.alarm(time_out)
            try:
                # In the Bash case.
                if bash:
                    status, output = commands.getstatusoutput(cmd_buf)
                    if out:
                        print output
                # In the Python case.
                else:
                    if out:
                        exec(cmd_buf)
                    else:
                        sys.stdout = os.devnull
                        exec(cmd_buf)
                        sys.stdout = sys.__stdout__
            except:
                # Restore stdout just in case.
                sys.stdout = sys.__stdout__
                status = 1
            signal.alarm(0)

        # We need to check the status of the command to see if it failed.
        # Note that status is set to 1 upon time-out.
        if status == 0:
            break
        else:
            current_attempt += 1
            if sleep_time != 0 and current_attempt < max_attempts:
                time.sleep(sleep_time)

    # Check to see if we ultimately failed the command.
    if status != 0:
        if err:
            g.print_error("The following command failed to execute: " +
                          cmd_buf)
            if output != "":
                print "Command output:"
                print output

    # Reset the alarm handler.
    if time_out != 0:
        signal.signal(signal.SIGALRM, original_handler)

    return status
def exec_cmd(
    cmd_buf, stat=False, out=False, err=False, time_out=0, max_attempts=1, sleep_time=1, debug=False, bash=False
):

    # This is an advanced function for executing a string as a command. It has
    # the capacity to save the output and return it, time the command out, retry
    # the function and sleep between attempts. The rmt_cmd option has been
    # removed, as the "rcmd" function now has a "-rrc" flag that will set the
    # return code automatically. This is less typing for the programmer, so just
    # do that.

    # stat: if this is set, an "issuing" message will be printed prior to
    #   execution.
    # out: if this is set, the output from the command will be printed to the
    #   screen.
    # err: if this is set, a failure message will be printed if the command fails.
    # time_out: if this is non-zero, the function will be timed out after the
    #   specified number of seconds.
    # max_attempts: the total number of times to attempt the command.
    # sleep_time: the number of seconds to wait between each attempt.
    # debug: if this is set, debug information will be printed.
    # bash: if this is set, the function will execute a Bash command instead of
    #   a Python command.

    # This function doesn't use parms in the exact same way as the bash version,
    # but just writing "param=value" works equally well in the function call,
    # i.e. exec_cmd(cmd_buf, param1=value1, param2=value2)

    stat = gopt.bool_convert(stat)
    out = gopt.bool_convert(out)
    err = gopt.bool_convert(err)
    debug = gopt.bool_convert(debug)
    current_attempt = 0

    # Print debug information, if necessary.
    if debug:
        gprint.print_time("Printing debug information for exec_cmd.")
        g.print_var("cmd_buf", cmd_buf)
        g.print_var("stat", stat)
        g.print_var("out", out)
        g.print_var("err", err)
        g.print_var("time_out", time_out)
        g.print_var("max_attempts", max_attempts)
        g.print_var("sleep_time", sleep_time)
        g.print_var("debug", debug)

    # If we're using a time-out variable, then we need to set up a time-out
    # signal handler. No need to fork processes.
    if time_out != 0:
        original_handler = signal.signal(signal.SIGALRM, gsig.gen_timeout_handler)

    # Print out an issuing message if needed.
    if stat and bash:
        gprint.issuing(cmd_buf)
        if max_attempts == 0:
            gprint.print_time("max_attempts is zero so the preceding command will not actually be executed.")

    # Now let's begin the execution.
    status = 0
    output = ""
    while current_attempt < max_attempts:
        status = 0
        if time_out == 0:
            # If we're not timing it out, just run the command.

            status, output = commands.getstatusoutput(cmd_buf)
            if out:
                print output
        else:
            # Otherwise, we need to set an alarm and catch a time-out.
            # We'll actually catch all exceptions, since it's possible we can
            # be executing a Python command.
            signal.alarm(time_out)
            try:
                # In the Bash case.
                if bash:
                    status, output = commands.getstatusoutput(cmd_buf)
                    if out:
                        print output
                # In the Python case.
                else:
                    if out:
                        exec (cmd_buf)
                    else:
                        sys.stdout = os.devnull
                        exec (cmd_buf)
                        sys.stdout = sys.__stdout__
            except:
                # Restore stdout just in case.
                sys.stdout = sys.__stdout__
                status = 1
            signal.alarm(0)

        # We need to check the status of the command to see if it failed.
        # Note that status is set to 1 upon time-out.
        if status == 0:
            break
        else:
            current_attempt += 1
            if sleep_time != 0 and current_attempt < max_attempts:
                time.sleep(sleep_time)

    # Check to see if we ultimately failed the command.
    if status != 0:
        if err:
            g.print_error("The following command failed to execute: " + cmd_buf)
            if output != "":
                print "Command output:"
                print output

    # Reset the alarm handler.
    if time_out != 0:
        signal.signal(signal.SIGALRM, original_handler)

    return status
def print_system_parms():

    # This prints out some basic system information for gen_print_header().

    # File name.
    g.print_var("command_line", g.program_path)
    # Process ID.
    g.print_var(g.program_name + "_pid", g.system_parms["pid"])
    # PGID.
    g.print_var(g.program_name + "_pgid", g.system_parms["pgid"])
    # User ID.
    g.print_var(
        "uid",
        str(g.system_parms["userid"]) + " (" + g.system_parms["username"] +
        ")")
    # Group ID.
    g.print_var(
        "gid",
        str(g.system_parms["gid"]) + " (" + g.system_parms["username"] + ")")
    # Host name.
    g.print_var("hostname", g.system_parms["hostname"])
    # Display.
    g.print_var("DISPLAY", g.system_parms["display"])

    return 0