/usr/lib/python2.7/dist-packages/crmsh/idmgmt.py is in crmsh 3.0.1-3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | # Copyright (C) 2008-2011 Dejan Muhamedagic <dmuhamedagic@suse.de>
# See COPYING for license information.
#
# Make sure that ids are unique.
from . import constants
import copy
from .msg import common_error, id_used_err
from . import xmlutil
_id_store = {}
_state = []
ok = True # error var
def push_state():
_state.append(copy.deepcopy(_id_store))
def pop_state():
try:
global _id_store
_id_store = _state.pop()
return True
except IndexError:
return False
def drop_state():
try:
_state.pop()
except KeyError:
pass
def clean_state():
global _state
_state = []
def new(node, pfx):
'''
Create a unique id for the xml node.
'''
name = node.get("name")
if node.tag == "nvpair":
node_id = "%s-%s" % (pfx, name)
elif node.tag == "op":
interval = node.get("interval")
if interval:
node_id = "%s-%s-%s" % (pfx, name, interval)
else:
node_id = "%s-%s" % (pfx, name)
else:
subpfx = constants.subpfx_list.get(node.tag, '')
if subpfx:
node_id = "%s-%s" % (pfx, subpfx)
else:
node_id = pfx
if is_used(node_id):
node_id = _gen_free_id(node_id)
save(node_id)
return node_id
def _gen_free_id(node_id):
"generate a unique id"
# shouldn't really get here
for cnt in range(99):
try_id = "%s-%d" % (node_id, cnt)
if not is_used(try_id):
node_id = try_id
break
return node_id
def check_node(node, lvl):
global ok
node_id = node.get("id")
if not node_id:
return
if id_in_use(node_id):
common_error("id_store: id %s is in use" % node_id)
ok = False
return
def _store_node(node, lvl):
save(node.get("id"))
def _drop_node(node, lvl):
remove(node.get("id"))
def check_xml(node):
global ok
ok = True
xmlutil.xmltraverse_thin(node, check_node)
return ok
def store_xml(node):
if not check_xml(node):
return False
xmlutil.xmltraverse_thin(node, _store_node)
return True
def remove_xml(node):
xmlutil.xmltraverse_thin(node, _drop_node)
def replace_xml(oldnode, newnode):
remove_xml(oldnode)
if not store_xml(newnode):
store_xml(oldnode)
return False
return True
def is_used(node_id):
return node_id in _id_store
def id_in_use(obj_id):
if is_used(obj_id):
id_used_err(obj_id)
return True
return False
def save(node_id):
if not node_id:
return
_id_store[node_id] = 1
def rename(old_id, new_id):
if not old_id or not new_id:
return
if not is_used(old_id):
return
if is_used(new_id):
return
remove(old_id)
save(new_id)
def remove(node_id):
if not node_id:
return
try:
del _id_store[node_id]
except KeyError:
pass
def clear():
global _id_store
global _state
_id_store = {}
_state = []
def set_id(node, oldnode, id_hint, id_required=True):
'''
Set the id attribute for the node.
- if the node already contains "id", keep it
- if the old node contains "id", copy that
- if the node contains "uname", copy that
- else if required, create a new one using id_hint
- save the new id in idmgmt.
'''
old_id = oldnode.get("id") if oldnode is not None else None
new_id = node.get("id") or old_id or node.get("uname")
if new_id:
save(new_id)
elif id_required:
new_id = new(node, id_hint)
if new_id:
node.set("id", new_id)
if oldnode is not None and old_id == new_id:
xmlutil.set_id_used_attr(oldnode)
# vim:ts=4:sw=4:et:
|