From fa939fbd9fc90f165ce63a9a25c21bab6bb5375e Mon Sep 17 00:00:00 2001 From: lizhi-rocky Date: Mon, 18 Oct 2010 10:46:34 +0800 Subject: [PATCH] incorporate Ling Fen's fix --- interface/mine/install_xml.py | 107 ---------------------------------- interface/ri_data.py | 45 ++++++++------ 2 files changed, 27 insertions(+), 125 deletions(-) delete mode 100644 interface/mine/install_xml.py diff --git a/interface/mine/install_xml.py b/interface/mine/install_xml.py deleted file mode 100644 index 85b28f7..0000000 --- a/interface/mine/install_xml.py +++ /dev/null @@ -1,107 +0,0 @@ -#!/usr/bin/python - -import sys -from xml.dom import minidom -import Tkinter - -def attr_to_dict(attr): - ''' convert (widget or grid_location) attributes into a dict ''' - dict = {} - for a in attr.values(): - if a.name == 'image': - dict[a.name] = Tkinter.PhotoImage(file=a.value) - elif a.name == 'command': - module = sys.modules[__name__] - dict[a.name] = getattr(module, a.value) - else: - dict[a.name] = a.value - return dict - -def construct_widget_dict(): - ''' Go through all widget xml nodes on first level, I mean, those widgets defined -inside other widget will not be checked. If the widget has a name, it will -be recorded in the widget_dict.''' - widgets = [e for e in xmldoc.firstChild.childNodes - if e.nodeType == e.ELEMENT_NODE and e.nodeName == "widget"] - for w in widgets: - if "name" in w.attributes.keys(): - widget_dict[w.attributes["name"].value] = w - -def widget(w_xml, p_win): - ''' create a widget. - w_xml is a xml node describing the widget. - p_win is the parent widget.''' - func = getattr(Tkinter, w_xml.attributes["type"].value) - # convert attributes into dictionary - wa_list = [wa for wa in w_xml.childNodes - if wa.nodeType == wa.ELEMENT_NODE and wa.nodeName =="widget_attribute"] - if wa_list: - # only process the first element, and there should be only one element. - attr_dict = attr_to_dict(wa_list[0].attributes) - w_win = func(p_win, attr_dict) - else: - w_win = func(p_win) - - gm_list = [m for m in w_xml.childNodes - if m.nodeType == m.ELEMENT_NODE and m.nodeName == "grid_management"] - gl_list = [l for l in w_xml.childNodes - if l.nodeType == l.ELEMENT_NODE and l.nodeName == "grid_location"] - w_list = [ w for w in w_xml.childNodes - if w.nodeType == w.ELEMENT_NODE and w.nodeName == "widget" ] - - for w in w_list: - widget(w, w_win) - - for m in gm_list: - for cf in m.childNodes: - if cf.nodeType != m.ELEMENT_NODE or cf.nodeName != "configure": - continue - if "row" in cf.attributes.keys(): - cf_func = getattr(w_win, "rowconfigure") - n = cf.attributes["row"].value - elif "column" in cf.attributes.keys(): - cf_func = getattr(w_win, "columnconfigure") - n = cf.attributes["column"].value - else: - continue - cf_func(n, weight=cf.attributes["weight"].value) - # only process the first item - break - - for l in gl_list: - ld = attr_to_dict(l.attributes) - w_win.grid(ld) - # only process the first item - break - -def display(): - ''' Display a series of widgets ''' - root_window = Tkinter.Tk() - - # first display the main frame - widget(widget_dict["main"], root_window) - root_window.columnconfigure(0, weight=1) - root_window.rowconfigure(0, weight=1) - root_window.geometry('800x600+0+0') - root_window.mainloop() - -# commands for buttons -def quit(): - print "quit" - -def previous_step(): - print "previous step" - -def next_step(): - - print "next step" - -xmldoc = minidom.parse("../../xml/interface_t.xml") - -widget_dict={} -construct_widget_dict() - -sequence_dict={} -construct_sequence_dict() - -display() diff --git a/interface/ri_data.py b/interface/ri_data.py index eab7177..8d8c22c 100644 --- a/interface/ri_data.py +++ b/interface/ri_data.py @@ -55,7 +55,6 @@ class Partition: @staticmethod def init_from_os(): ''' create a Partition instance from hardware info''' - device_list=[] Partition.unit='GB' cmd_cat = 'cat /proc/partitions' @@ -82,7 +81,7 @@ class Partition: l = [ s.split()[-1] for s in o.splitlines() if re.match(r'\s*\d+', s)] # devices : 1) sda, sdb(first if ), or md0 (second if) for d in [ i for i in l if not re.search(r'\d+', i) - or re.search(r'([a-zA-Z]+)', i).group(1) not in l] + or re.search(r'([a-zA-Z]+)', i).group(1) not in l]: st,o=commands.getstatusoutput('parted /dev/%s unit %s print'%(d,Partition.unit)) if st: print "Error parted execute error" @@ -114,14 +113,14 @@ class Partition: flg_i= s.find('Flags') if tp_i < 0: tp_i=fs_i located = True - - if located: + elif located: Partition(d+s[nm_i:st_i].strip(), # device name - s[st_i:end_i].strip(), # start + s[st_i:end_i].strip().rstrip(Partition.unit), # start s[sz_i:tp_i].strip().rstrip(Partition.unit), # size s[tp_i:fs_i].strip(), # type re.search('swap', s[fs_i:flg_i]) and 'swap' or s[fs_i:flg_i].strip(), # file system - s[flg_i:].strip() # flags + s[flg_i:].strip(), # flags + 'yes' ) @staticmethod @@ -148,7 +147,8 @@ p_node - xml node (parent node)''' unit_attr = doc.createAttribute('unit') unit_attr.value = Partition.unit pts.setAttributeNode(unit_attr) - for p in Partition.list: + for k in Partition.dict.keys().sort(): + p = Partition.dict[k] pt = doc.createElement('partition') dev_attr = doc.createAttribute('device') start_attr = doc.createAttribute('start') @@ -173,10 +173,13 @@ p_node - xml node (parent node)''' pt.setAttributeNode(from_attr) pts.appendChild(pt) p_node.appendChild(pts) - # ri_tk_cmd.py use + @staticmethod def get_size(dev): - return Partition.dict[dev].size + try: + return Partition.dict[dev].size + except: + return None class Raid: ''' raid information ''' @@ -252,7 +255,8 @@ l - list doc - xml document instance p_node - xml node (parent node) ''' raids = doc.createElement('raids') - for r in Raid.list: + for k in Raid.dict.keys().sort(): + r = Raid.dict[k] rd = doc.createElement('raid') rd_dev_attr = doc.createAttribute('device') @@ -289,8 +293,12 @@ p_node - xml node (parent node) ''' #ri_tk_cmd.py use @staticmethod def get_size(dev): - # need fill - pass + ''' calculate raid device size ''' + rd = Raid.dict[dev] + sz = min([ float(Partition.dict[d].size) for d in rd.active_components]) + if rd.level == '0': return sz*len(rd.active_components) + elif rd.level == '1': return sz*len(rd.active_components)/2 + elif rd.level == '5': return sz*(len(rd.active_components)-1) @staticmethod def get_next_device(): @@ -306,7 +314,7 @@ p_node - xml node (parent node) ''' def dev_in_raid(): """if the device in raid or not""" devices_in_raid=set() - for r in Raid.list: + for r in Raid.dict.values(): devices_in_raid.update(r.active_components) devices_in_raid.update(r.spare_components) return devices_in_raid @@ -339,13 +347,13 @@ class MountPoint: MountPoint(dev, fs=Partition.dict[dev].filesystem) for dev in Raid.dict.keys(): - if MountPoint.dict.has_key(dev) - continue + if MountPoint.dict.has_key(dev): continue MountPoint(dev) - # now process whether a partition or raid was removed + # now delete unexist partition/raid mount point, or + # partition that used in raid. s1 = set(MountPoint.dict.keys()) - s2 = set(Partition.dict.keys() + Raid.dict.keys()) + s2 = set(Partition.dict.keys() + Raid.dict.keys()) - set(dev_in_raid) for dev in s1-s2: del MountPoint.dict[dev] @@ -366,7 +374,8 @@ class MountPoint: doc - xml document instance p_node - xml node (parent node)''' mps = doc.createElement('mount-points') - for m in MountPoint.list: + for k in MountPoint.dict.keys().sort(): + m = MountPoint.dict[k] mp = doc.createElement('mount-point') dev_attr = doc.createAttribute('device') dir_attr = doc.createAttribute('directory')