<s> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> import utils <EOL> import XenAPIPlugin <EOL> import pluginlib_nova as pluginlib <EOL> pluginlib . configure_logging ( "<STR_LIT>" ) <EOL> class XenstoreError ( pluginlib . PluginError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cmd , return_code , stderr , stdout ) : <EOL> msg = "<STR_LIT>" <EOL> msg = msg % ( cmd , return_code , stderr , stdout ) <EOL> self . cmd = cmd <EOL> self . return_code = return_code <EOL> self . stderr = stderr <EOL> self . stdout = stdout <EOL> pluginlib . PluginError . __init__ ( self , msg ) <EOL> def jsonify ( fnc ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> ret = fnc ( * args , ** kwargs ) <EOL> try : <EOL> json . loads ( ret ) <EOL> except ValueError : <EOL> ret = json . dumps ( ret ) <EOL> return ret <EOL> return wrapper <EOL> def record_exists ( arg_dict ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % arg_dict ] <EOL> try : <EOL> _run_command ( cmd ) <EOL> return True <EOL> except XenstoreError , e : <EOL> if e . stderr == '<STR_LIT>' : <EOL> return False <EOL> raise <EOL> @ jsonify <EOL> def read_record ( self , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % arg_dict ] <EOL> try : <EOL> result = _run_command ( cmd ) <EOL> return result . strip ( ) <EOL> except XenstoreError , e : <EOL> if not arg_dict . get ( "<STR_LIT>" , False ) : <EOL> raise <EOL> if not record_exists ( arg_dict ) : <EOL> return "<STR_LIT:None>" <EOL> result = _run_command ( cmd ) <EOL> return result . strip ( ) <EOL> @ jsonify <EOL> def write_record ( self , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ "<STR_LIT>" , <EOL> "<STR_LIT>" % arg_dict , <EOL> arg_dict [ "<STR_LIT:value>" ] ] <EOL> _run_command ( cmd ) <EOL> return arg_dict [ "<STR_LIT:value>" ] <EOL> @ jsonify <EOL> def list_records ( self , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> dirpath = "<STR_LIT>" % arg_dict <EOL> cmd = [ "<STR_LIT>" , dirpath . rstrip ( "<STR_LIT:/>" ) ] <EOL> try : <EOL> recs = _run_command ( cmd ) <EOL> except XenstoreError , e : <EOL> if not record_exists ( arg_dict ) : <EOL> return { } <EOL> recs = _run_command ( cmd ) <EOL> base_path = arg_dict [ "<STR_LIT:path>" ] <EOL> paths = _paths_from_ls ( recs ) <EOL> ret = { } <EOL> for path in paths : <EOL> if base_path : <EOL> arg_dict [ "<STR_LIT:path>" ] = "<STR_LIT>" % ( base_path , path ) <EOL> else : <EOL> arg_dict [ "<STR_LIT:path>" ] = path <EOL> rec = read_record ( self , arg_dict ) <EOL> try : <EOL> val = json . loads ( rec ) <EOL> except ValueError : <EOL> val = rec <EOL> ret [ path ] = val <EOL> return ret <EOL> @ jsonify <EOL> def delete_record ( self , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ "<STR_LIT>" , "<STR_LIT>" % arg_dict ] <EOL> try : <EOL> return _run_command ( cmd ) <EOL> except XenstoreError , e : <EOL> if '<STR_LIT>' in e . stderr : <EOL> return '<STR_LIT>' <EOL> raise <EOL> def _paths_from_ls ( recs ) : <EOL> """<STR_LIT>""" <EOL> last_nm = "<STR_LIT>" <EOL> level = <NUM_LIT:0> <EOL> path = [ ] <EOL> ret = [ ] <EOL> for ln in recs . splitlines ( ) : <EOL> nm , val = ln . rstrip ( ) . split ( "<STR_LIT>" ) <EOL> barename = nm . lstrip ( ) <EOL> this_level = len ( nm ) - len ( barename ) <EOL> if this_level == <NUM_LIT:0> : <EOL> ret . append ( barename ) <EOL> level = <NUM_LIT:0> <EOL> path = [ ] <EOL> elif this_level == level : <EOL> ret . append ( "<STR_LIT>" % ( "<STR_LIT:/>" . join ( path ) , barename ) ) <EOL> elif this_level > level : <EOL> path . append ( last_nm ) <EOL> ret . append ( "<STR_LIT>" % ( "<STR_LIT:/>" . join ( path ) , barename ) ) <EOL> level = this_level <EOL> elif this_level < level : <EOL> path = path [ : this_level ] <EOL> ret . append ( "<STR_LIT>" % ( "<STR_LIT:/>" . join ( path ) , barename ) ) <EOL> level = this_level <EOL> last_nm = barename <EOL> return ret <EOL> def _run_command ( cmd ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return utils . run_command ( cmd ) <EOL> except utils . SubprocessException , e : <EOL> raise XenstoreError ( e . cmdline , e . ret , e . err , e . out ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> XenAPIPlugin . dispatch ( <EOL> { "<STR_LIT>" : read_record , <EOL> "<STR_LIT>" : write_record , <EOL> "<STR_LIT>" : list_records , <EOL> "<STR_LIT>" : delete_record } ) </s>
