Ejemplo n.º 1
0
def get_cache_library():
    import platform
    name = platform.system()
    if name == 'Darwin':
        return cdll.LoadLibrary('libcache.dylib')
    elif name == 'Windows':
        if cindex.isWin64:
            return cdll.LoadLibrary("libcache_x64.dll")
        return cdll.LoadLibrary('libcache.dll')
    else:
        try:
            # Try loading with absolute path first

            return cdll.LoadLibrary('%s/libcache.so' % scriptpath)
        except:
            try:
                # See if there's one in the system path
                return cdll.LoadLibrary("libcache.so")
            except:
                import traceback
                traceback.print_exc()
                error_message("""\
It looks like libcache.so couldn't be loaded. On Linux you have to \
compile it yourself.

See http://github.com/quarnster/SublimeClang for more details.
""")
def get_cache_library():
    import platform
    name = platform.system()
    if name == 'Darwin':
        return cdll.LoadLibrary('libcache.dylib')
    elif name == 'Windows':
        if cindex.isWin64:
            return cdll.LoadLibrary("libcache_x64.dll")
        return cdll.LoadLibrary('libcache.dll')
    else:
        try:
            # Try loading with absolute path first

            return cdll.LoadLibrary('%s/libcache.so' % scriptpath)
        except:
            try:
                # See if there's one in the system path
                return cdll.LoadLibrary("libcache.so")
            except:
                import traceback
                traceback.print_exc()
                error_message("""\
It looks like libcache.so couldn't be loaded. On Linux you have to \
compile it yourself.

See http://github.com/quarnster/SublimeClang for more details.
""")
Ejemplo n.º 3
0
def list_attr(expect=EXPECT_SUCCESS):
    """List all supported attributes"""
    options = ["--command", "list", "--list", "attr"]

    output = exec_pwrcmd(options, expect=expect)

    if "attr_list" not in output:
        print error_message("attr_list not found", json.dumps(output,
                                                              indent=2))
        sys.exit(102)

    return output["attr_list"]
Ejemplo n.º 4
0
def set_attr(attr,
             obj_name,
             value,
             role,
             errduples=None,
             method=None,
             desc=None,
             expect=EXPECT_SUCCESS):
    """Set one or more attributes"""
    if not isinstance(attr, list):
        attr = [attr]
    if not isinstance(obj_name, list):
        obj_name = [obj_name]
    if not isinstance(value, list):
        value = [value]

    attr_cnt = len([x for x in attr if x != ''])
    obj_cnt = len([x for x in obj_name if x != ''])
    numvals = attr_cnt * obj_cnt

    attr = ','.join(attr)
    obj_name = ','.join(obj_name)
    value = ','.join([str(val) for val in value])

    options = [
        "--command", "set", "--attribute", attr, "--name", obj_name, "--value",
        value, "--role", role
    ]

    result = exec_pwrcmd(options, desc=desc, expect=expect)
    errmsg = check_status(result, numvals, errduples)
    if errmsg:
        print error_message("check_status failed: {}".format(errmsg),
                            json.dumps(result, indent=2))
        sys.exit(102)
    errmsg = check_method(result, method)
    if errmsg:
        print error_message("check_method failed: {}".format(errmsg),
                            json.dumps(result, indent=2))
        sys.exit(103)

    return result
Ejemplo n.º 5
0
def list_name(filter=None, expect=EXPECT_SUCCESS):
    """List all objects in hierarchy, filtered by regex"""
    options = ["--command", "list", "--list", "name"]

    output = exec_pwrcmd(options, expect=expect)

    if "name_list" not in output:
        print error_message("name_list not found", json.dumps(output,
                                                              indent=2))
        sys.exit(102)

    if filter is None:
        return output["name_list"]

    select = []
    for name in output["name_list"]:
        if re.search(filter, name):
            select.append(name)

    return select
Ejemplo n.º 6
0
    def post(self):
        user = users.get_current_user()
        self.response.headers['Content-Type'] = 'application/json'
        rawdata = self.request.get('data', None)
        if not rawdata:
            self.response.out.write(common.error_message(user,
                                                         None,
                                                         message = "No data"))
            return

        data = common.decode_and_validate(rawdata)
        if not data:
            self.response.out.write(common.error_message(user,
                                                         None,
                                                         message = "Bad data"))
            return
        ip = self.request.remote_addr
        self.response.out.write(common.process_command(data,
                                                       user,
                                                       ip=ip))
        return
Ejemplo n.º 7
0
def exec_pwrcmd(options, desc=None, expect=EXPECT_SUCCESS):
    """Execute a 'pwrcmd' command"""
    pwrcmd_cmd = ["/opt/cray/powerapi/default/bin/pwrcmd"]
    pwrcmd_cmd.extend(options)

    set_command(pwrcmd_cmd)
    set_description(desc)

    # Execute the command
    p = Popen(pwrcmd_cmd, stdout=PIPE, stderr=STDOUT, bufsize=-1)
    pwrcmd_output = p.communicate()

    # pwrcmd itself should not fail -- if it does, it's fatal
    if p.returncode != 0:
        print error_message("Popen() returncode = {}".format(p.returncode),
                            pwrcmd_output[0])
        sys.exit(100)

    # All output should be JSON -- if not, it's fatal
    try:
        output = json.loads(pwrcmd_output[0])
    except:
        print error_message("json.loads() failed", pwrcmd_output[0])
        sys.exit(101)

    # All output should include PWR_ReturnCode
    if "PWR_ReturnCode" not in output:
        print error_message("PWR_ReturnCode not found", pwrcmd_output[0])
        sys.exit(102)

    # Evaluate PWR_ReturnCode against expectations
    actcode = int(output["PWR_ReturnCode"])
    if expect == EXPECT_SUCCESS and actcode != 0:
        failtxt = "{} expected success, saw retcode {}".format(
            "PWR_ReturnCode", actcode)
    elif expect == EXPECT_FAILURE and actcode == 0:
        failtxt = "{} expected failure, saw success".format("PWR_ReturnCode")
    elif expect < EXPECT_SUCCESS and actcode != expect:
        failtxt = "{} expected retcode {}, saw retcode {}".format(
            "PWR_ReturnCode", expect, actcode)
    else:
        failtxt = None
    if failtxt:
        print error_message(failtxt, pwrcmd_output[0])
        sys.exit(103)

    # Return the dict structure parsed from JSON
    return output
Ejemplo n.º 8
0
def get_meta(meta, obj_name, attr=None, desc=None, expect=EXPECT_SUCCESS):
    """Get one metadata value associated with an object/attribute.

    Args:
        meta:
            a string metadata name.
        obj_name:
            a string object name.
        attr:
            a string name of an attribute (unnecessary if object metadata not
            associated with an attribute).
        desc:
            a text description.
        expect:
            the expected return code of the pwrcmd call.
    Returns:
        The output from pwrcmd (JSON-format).

    Raises:
        This method raises no exceptions, but will exit() with a non-zero
        exit code on error.
    """
    if meta is None:
        print error_message("No meta value supplied to get_meta()")
        sys.exit(101)
    elif obj_name is None:
        print error_message("No obj_name value supplied to get_meta()")
        sys.exit(101)

    options = ["--command", "get", "--metadata", meta, "--name", obj_name]

    if attr is not None:
        options.extend(["--attribute", attr])

    result = exec_pwrcmd(options, desc=desc, expect=expect)
    errmsg = check_meta_result(result)
    if errmsg:
        print error_message("check_meta_result failed: {}".format(errmsg),
                            json.dumps(result, indent=2))
        sys.exit(101)
    return result
Ejemplo n.º 9
0
def set_meta_and_check(meta,
                       obj_name,
                       value,
                       attr=None,
                       role="PWR_ROLE_RM",
                       method=None,
                       desc=None,
                       expect=EXPECT_SUCCESS):
    """Set one metadata value associated with an object/attribute, verify that
    the change was made, and restore the metadata value to its prior value.

    Args:
        meta:
            a string metadata name.
        obj_name:
            a string object name.
        attr:
            a string name of an attribute (unnecessary if object metadata not
            associated with an attribute).
        role:
            a string role name (e.g., "PWR_ROLE_RM")
        method:
            a string method name to check against (e.g, "PWR_ObjAttrSetMeta")
        desc:
            a text description.
        expect:
            the expected return code of the pwrcmd call.
    Returns:
        True if the set-verify-reset test is successful; False, otherwise.

    Raises:
        This method raises no exceptions, but will exit() with a non-zero
        exit code on error.
    """
    print "  set    value = {}".format(value)

    # Get the original value
    result = get_meta(meta, obj_name, attr=attr, desc=desc + " get orig")
    orig_value = result['value']
    print "  orig   value = {}".format(orig_value)

    expect_value = value
    print "  expect value = {}".format(expect_value)

    # Perform the set
    set_meta(meta,
             obj_name,
             value=value,
             attr=attr,
             role=role,
             method=method,
             desc=desc + " set new")
    # Record this command and description for errors
    cmd = get_command()
    dsc = get_description()

    # Get the new metadatum value. This should be the same as expect_value
    result = get_meta(meta, obj_name, attr=attr, desc=desc + " get check new")
    actual_value = result['value']
    print "  actual value = {}".format(actual_value)

    # Set the metadatum to the original value
    set_meta(meta,
             obj_name,
             value=orig_value,
             attr=attr,
             role=role,
             method=method,
             desc=desc + " reset orig")

    # Read back after reset
    result = get_meta(meta, obj_name, attr=attr, desc=desc + " get check orig")
    final_value = result['value']
    print "  final  value = {}".format(final_value)

    # Want some indication of what went wrong
    text = "\n".join([
        "  set    value = {}".format(value),
        "  orig   value = {}".format(orig_value),
        "  expect value = {}".format(expect_value),
        "  actual value = {}".format(actual_value),
        "  final  value = {}".format(final_value)
    ])
    if actual_value != expect_value:
        print error_message("new value != expected value",
                            output=text,
                            desc=dsc,
                            command=cmd)
        return False
    if orig_value != final_value:
        print error_message("final value != orig value",
                            output=text,
                            desc=dsc,
                            command=cmd)
        return False
    return True
Ejemplo n.º 10
0
def set_meta(meta,
             obj_name,
             value,
             attr=None,
             role="PWR_ROLE_RM",
             method=None,
             desc=None,
             expect=EXPECT_SUCCESS):
    """Set one metadata value associated with an object/attribute.

    Args:
        meta:
            a string metadata name.
        obj_name:
            a string object name.
        attr:
            a string name of an attribute (unnecessary if object metadata not
            associated with an attribute).
        role:
            a string role name (e.g., "PWR_ROLE_RM")
        method:
            a string method name to check against (e.g, "PWR_ObjAttrSetMeta")
        desc:
            a text description.
        expect:
            the expected return code of the pwrcmd call.
    Returns:
        The output from pwrcmd (JSON-format).

    Raises:
        This method raises no exceptions, but will exit() with a non-zero
        exit code on error.
    """
    if meta is None:
        print error_message("No meta value supplied")
        sys.exit(101)
    elif obj_name is None:
        print error_message("No obj_name value supplied")
        sys.exit(101)
    elif value is None:
        print error_message("No value parameter supplied")
        sys.exit(101)

    if not isinstance(meta, str):
        try:
            meta = str(meta)
        except TypeError:
            print error_message("meta value is not stringable")
            sys.exit(102)
    if not isinstance(attr, str) and attr is not None:
        try:
            attr = str(attr)
        except TypeError:
            print error_message("attr value is not stringable")
            sys.exit(102)
    if not isinstance(obj_name, str):
        try:
            obj_name = str(obj_name)
        except TypeError:
            print error_message("obj_name value is not stringable")
            sys.exit(102)
    if not isinstance(value, str):
        try:
            value = str(value)
        except TypeError:
            print error_message("value parameter is not stringable")
            sys.exit(102)

    options = [
        "--command", "set", "--meta", meta, "--name", obj_name, "--value",
        value, "--role", role
    ]

    if attr is not None:
        options.extend(["--attribute", attr])

    result = exec_pwrcmd(options, desc=desc, expect=expect)
    errmsg = check_method(result, method)
    if errmsg:
        print error_message("check_method failed: {}".format(errmsg),
                            json.dumps(result, indent=2))
        sys.exit(103)

    return result
Ejemplo n.º 11
0
def set_attr_and_check(attrs,
                       objs,
                       values,
                       role,
                       errduples=None,
                       method=None,
                       desc=None,
                       expect=EXPECT_SUCCESS):
    """Perform a set/get/reset operation on one or more attributes"""

    # Build this up as we go

    # We want these as lists-of-one, not as scalars
    if not isinstance(attrs, list):
        attrs = [attrs]
    if not isinstance(objs, list):
        objs = [objs]
    if not isinstance(values, list):
        values = [values]
    print "  set    values = {}".format(values)

    # This will have attributes * objects values in it
    result = get_attr(attrs, objs, desc=desc + " get orig")
    orig_values = result['attr_vals']
    print "  orig   values = {}".format(orig_values)
    print get_command()

    # There is no API call to set a heterogeneous collection
    # of attribute values for multiple objects. Instead, we
    # have to set multiple attribute values for each object.
    # We need to slice this into
    # [ [o1v1,o1v2,o1v3,...],
    #   [o2v1,o2v2,o2v3,...], ... ]
    numattrs = len(attrs)
    numvals = len(orig_values)
    reset_values = [
        orig_values[i:i + numattrs] for i in range(0, numvals, numattrs)
    ]
    print "  reset  values = {}".format(reset_values)

    # This is what we expect actual_values to be
    # Values for set follow attribute count: 1 value for 1 attribute
    # Values for get expand as attributes * objects
    # This produces [o1v1,o1v2,o1v3,...o2v1,o2v2,o2v3,...]
    expect_values = [val for obj in objs for val in values]
    print "  expect values = {}".format(expect_values)

    # Perform the set
    set_attr(attrs,
             objs,
             values,
             role,
             errduples,
             desc=desc + " set new",
             method=method,
             expect=expect)
    # Record this command and description for errors
    cmd = get_command()
    dsc = get_description()

    # Read attributes * objects values after set
    result = get_attr(attrs, objs, desc=desc + " get check new")
    actual_values = result['attr_vals']
    print "  actual values = {}".format(actual_values)

    # To put the originals back, we need to walk through reset_values
    i = 0
    for obj in objs:
        set_attr(attrs, obj, reset_values[i], role, desc=desc + " reset orig")
        i += 1

    # Read back after reset, attributes * objects values
    result = get_attr(attrs, objs, desc=desc + " get check orig")
    final_values = result['attr_vals']
    print "  final  values = {}".format(final_values)

    # Want some indication of what went wrong
    text = "\n".join([
        "  set    values = {}".format(values),
        "  orig   values = {}".format(orig_values),
        "  expect values = {}".format(expect_values),
        "  actual values = {}".format(actual_values),
        "  final  values = {}".format(final_values)
    ])
    if actual_values != expect_values:
        print error_message("new values != expected values",
                            output=text,
                            desc=dsc,
                            command=cmd)
        return False
    if orig_values != final_values:
        print error_message("final values != orig values",
                            output=text,
                            desc=dsc,
                            command=cmd)
        return False

    return True