예제 #1
0
def ParseCpuMask(cpu_mask):
    """Parse a CPU mask definition and return the list of CPU IDs.

  CPU mask format: comma-separated list of CPU IDs
  or dash-separated ID ranges
  Example: "0-2,5" -> "0,1,2,5"

  @type cpu_mask: str
  @param cpu_mask: CPU mask definition
  @rtype: list of int
  @return: list of CPU IDs

  """
    if not cpu_mask:
        return []
    cpu_list = []
    for range_def in cpu_mask.split(","):
        boundaries = range_def.split("-")
        n_elements = len(boundaries)
        if n_elements > 2:
            raise errors.ParseError("Invalid CPU ID range definition"
                                    " (only one hyphen allowed): %s" %
                                    range_def)
        try:
            lower = int(boundaries[0])
        except (ValueError, TypeError), err:
            raise errors.ParseError(
                "Invalid CPU ID value for lower boundary of"
                " CPU ID range: %s" % str(err))
        try:
            higher = int(boundaries[-1])
        except (ValueError, TypeError), err:
            raise errors.ParseError(
                "Invalid CPU ID value for higher boundary of"
                " CPU ID range: %s" % str(err))
예제 #2
0
def LoadAndVerifyJson(raw, verify_fn):
    """Parses and verifies JSON data.

  @type raw: string
  @param raw: Input data in JSON format
  @type verify_fn: callable
  @param verify_fn: Verification function, usually from L{ht}
  @return: De-serialized data

  """
    try:
        data = LoadJson(raw)
    except Exception as err:
        raise errors.ParseError("Can't parse input data: %s" % err)

    if not verify_fn(data):
        raise errors.ParseError("Data does not match expected format: %s" %
                                verify_fn)

    return data
예제 #3
0
                                    range_def)
        try:
            lower = int(boundaries[0])
        except (ValueError, TypeError), err:
            raise errors.ParseError(
                "Invalid CPU ID value for lower boundary of"
                " CPU ID range: %s" % str(err))
        try:
            higher = int(boundaries[-1])
        except (ValueError, TypeError), err:
            raise errors.ParseError(
                "Invalid CPU ID value for higher boundary of"
                " CPU ID range: %s" % str(err))
        if lower > higher:
            raise errors.ParseError("Invalid CPU ID range definition"
                                    " (%d > %d): %s" %
                                    (lower, higher, range_def))
        cpu_list.extend(range(lower, higher + 1))
    return cpu_list


def ParseMultiCpuMask(cpu_mask):
    """Parse a multiple CPU mask definition and return the list of CPU IDs.

  CPU mask format: colon-separated list of comma-separated list of CPU IDs
  or dash-separated ID ranges, with optional "all" as CPU value
  Example: "0-2,5:all:1,5,6:2" -> [ [ 0,1,2,5 ], [ -1 ], [ 1, 5, 6 ], [ 2 ] ]

  @type cpu_mask: str
  @param cpu_mask: multiple CPU mask definition
  @rtype: list of lists of int
예제 #4
0
    """Parses and verifies JSON data.

  @type raw: string
  @param raw: Input data in JSON format
  @type verify_fn: callable
  @param verify_fn: Verification function, usually from L{ht}
  @return: De-serialized data

  """
    try:
        data = LoadJson(raw)
    except Exception, err:
        raise errors.ParseError("Can't parse input data: %s" % err)

    if not verify_fn(data):
        raise errors.ParseError("Data does not match expected format: %s" %
                                verify_fn)

    return data


Dump = DumpJson
Load = LoadJson
DumpSigned = DumpSignedJson
LoadSigned = LoadSignedJson


class Private(object):
    """Wrap a value so it is hard to leak it accidentally.

  >>> x = Private("foo")
  >>> print "Value: %s" % x