Пример #1
0
def fetch_script_details(script_path):
    """
    Given the path to an executable script, execute the script in a
    special way to retrieve metadata, and return a
    `netsa.script.model.Script` object.  Note that this may be
    dangerous if the path pointed to by *script_path* is not actually
    a `netsa.script` script.

    Raises a `ScriptError` if the path does not point to an executable
    file, or if the executable file does not produce proper metadata.
    """
    # Check first if the script file exists.
    if not os.path.isfile(script_path):
        script_error = ScriptError(
            "Script %s is not a regular file" % repr(script_path))
        raise script_error
    try:
        # Okay, now let's try
        sc_out = ""
        sc_err = ""
        (sc_out, sc_err) = run_collect(
            "%(python_path)s %(script_path)s --netsa-script-get-metadata",
            vars={'python_path': sys.executable,
                  'script_path': script_path})
        script = model.parse_script_metadata(sc_out)
        return script
    except ScriptError:
        raise
    except:
        script_error = ScriptError(
            "Script %s failed to return metadata--not a framework script?\n"
            "%s\n%s%s"
            % (repr(script_path), sys.exc_info()[1], sc_out, sc_err))
        raise script_error
Пример #2
0
def fetch_script_details(script_path):
    """
    Given the path to an executable script, execute the script in a
    special way to retrieve metadata, and return a
    `netsa.script.model.Script` object.  Note that this may be
    dangerous if the path pointed to by *script_path* is not actually
    a `netsa.script` script.

    Raises a `ScriptError` if the path does not point to an executable
    file, or if the executable file does not produce proper metadata.
    """
    # Check first if the script file exists.
    if os.path.exists(script_path):
        if not os.path.isfile(script_path):
            script_error = ScriptError(
                "Script %s is not a regular file" % repr(script_path))
            raise script_error
    else:
        script_error = ScriptError(
            "Script %s does not exist" % repr(script_path))
        raise script_error
    def _error_info():
        return repr(script_path), sys.exc_info()[1]
    try:
        # Okay, now let's try
        sc_out = ""
        sc_err = ""
        (sc_out, sc_err) = shell.run_collect(
            "%(python_path)s %(script_path)s --netsa-script-get-metadata",
            vars={'python_path': sys.executable,
                  'script_path': script_path})
        return model.parse_script_metadata(sc_out)
    except ScriptError:
        raise
    except shell.PipelineException, e:
        msg = "Script %s failed to execute:\n%s" % _error_info()
        msg += "\nstderr[[[%s]]]" % e
        script_error = ScriptError(msg)
        raise script_error