Changeset 552


Ignore:
Timestamp:
Jun 1, 2008, 6:57:43 PM (16 years ago)
Author:
broder
Message:

And round 3 of using YAML

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-remote-server/files/usr/sbin/sipb-xen-remote-listvms

    r549 r552  
    99from subprocess import CalledProcessError, PIPE, Popen
    1010from sys import argv, stdout
    11 from cPickle import dump, loads
    12 
    13 ###
    14 
    15 #import compiler
    16 #
    17 #class Unsafe_Source_Error(Exception):
    18 #    def __init__(self,error,descr = None,node = None):
    19 #        self.error = error
    20 #        self.descr = descr
    21 #        self.node = node
    22 #        self.lineno = getattr(node,"lineno",None)
    23 #       
    24 #    def __repr__(self):
    25 #        return "Line %d.  %s: %s" % (self.lineno, self.error, self.descr)
    26 #    __str__ = __repr__   
    27 #           
    28 #class SafeEval(object):
    29 #   
    30 #    def visit(self, node,**kw):
    31 #        cls = node.__class__
    32 #        meth = getattr(self,'visit'+cls.__name__,self.default)
    33 #        return meth(node, **kw)
    34 #           
    35 #    def default(self, node, **kw):
    36 #        for child in node.getChildNodes():
    37 #            return self.visit(child, **kw)
    38 #           
    39 #    visitExpression = default
    40 #   
    41 #    def visitConst(self, node, **kw):
    42 #        return node.value
    43 #
    44 #    def visitDict(self,node,**kw):
    45 #        return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
    46 #       
    47 #    def visitTuple(self,node, **kw):
    48 #        return tuple(self.visit(i) for i in node.nodes)
    49 #       
    50 #    def visitList(self,node, **kw):
    51 #        return [self.visit(i) for i in node.nodes]
    52 #
    53 #class SafeEvalWithErrors(SafeEval):
    54 #
    55 #    def default(self, node, **kw):
    56 #        raise Unsafe_Source_Error("Unsupported source construct",
    57 #                                node.__class__,node)
    58 #           
    59 #    def visitName(self,node, **kw):
    60 #        if node.name == 'None': return None
    61 #        raise Unsafe_Source_Error("Strings must be quoted",
    62 #                                 node.name, node)
    63 #                                 
    64 #    # Add more specific errors if desired
    65 #           
    66 #def safe_eval(source, fail_on_error = True):
    67 #    if source.strip() == '': return None
    68 #    walker = fail_on_error and SafeEvalWithErrors() or SafeEval()
    69 #    try:
    70 #        ast = compiler.parse(source,"eval")
    71 #    except SyntaxError, err:
    72 #        raise
    73 #    try:
    74 #        return walker.visit(ast)
    75 #    except Unsafe_Source_Error, err:
    76 #        raise
     11from yaml import safe_dump, safe_load
    7712
    7813###
     
    9126def main(argv):
    9227  # Query each of the server for their VMs.
    93   # run('kinit -k host/sipb-vm-58.mit.edu'.split())
    9428  # TODO get `servers` from a real list of all the VM hosts (instead of
    9529  # hardcoding the list here)
    9630  servers = [ 'black-mesa.mit.edu', 'sx-blade-2.mit.edu' ]
    9731  # XXX
    98   results = [ loads(run(['remctl', server, 'remote', 'web', 'listvms', '--pickle']))
     32  results = [ safe_load(run(['remctl', server, 'remote', 'web', 'listvms']))
    9933              for server in servers ]
    10034  results = filter( lambda x: x is not None, results )
     
    10337  merged = {}
    10438  for result in results: merged.update(result)
    105   if argv[1:] == ['--pickle']:
    106     dump(merged, stdout)
    107   else:
    108     print merged
    109     print '.'
     39  print safe_dump(merged, default_flow_style=False)
    11040
    11141if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.