読者です 読者をやめる 読者になる 読者になる

めもめも

このブログに記載の内容は個人の見解であり、必ずしも所属組織の立場、戦略、意見を代表するものではありません。

cgroupsの設定をGUIでぐりぐりしてみました

どっかのデモに使えるかなぁ。。。ということで。

ソースはこちら。RHEL6.1で作りました。エラーチェックとかサニタイズは真剣にはやっていないので、本気で利用される方はそのあたりは手を加えてください。tkinterパッケージが必要です。

# yum install tkinter
#!/usr/bin/python

from Tix import *
import sys, os, re, commands, stat

class ConfWindow( Frame ):
    def __init__( self, param, gpath ):
        self.param = param
        self.gpath = gpath
        self.win = Toplevel()
        self.win.title( "New Parameter" )
        Frame.__init__( self, self.win )

        cmd = "cgget -nv -r " + param + " " + gpath
        current = commands.getoutput( cmd )
        self.entry = Entry( self )
        self.entry.insert( 0, current )

        gfont = ( "Sans", 12 )
        pfont = ( "Courier", 12, "bold" )
        Label( self, text="Group: " + gpath, font=gfont ).pack( side=TOP )
        Label( self, text=param, font=pfont ).pack( side=TOP )
        self.entry.pack( side=TOP )
        Button( self, text="OK", command=self.onOK ).pack( side=LEFT )
        Button( self, text="Cancel", command=self.onCancel ).pack( side=LEFT )

        self.pack( fill=BOTH, expand=YES )
        self.win.focus_set()
        self.entry.focus_set()
        while True:
            try: self.win.grab_set()
            except TclError: pass
            else: break
        self.win.wait_window()

    def onOK( self ):
        cmd = "cgset -r " + self.param + "=\'" + self.entry.get() + "\' " + self.gpath
        commands.getoutput( cmd )
        self.win.destroy()

    def onCancel( self ):
        self.win.destroy()

class ParamView( Frame ):
    def __init__( self, parent, **params ):
        Frame.__init__( self, parent, **params )
        self.parent = parent
        listfont = ( "Courier", 12 )
        labelfont = ( "Sans", 12 )
        self.paramview = ScrolledListBox( self )
        self.paramview.listbox.config( font=listfont )
        self.paramview.listbox.bind( "<Double-Button-1>", self.modparam )
        self.subsys, self.gpath = "", ""
        self.glabel = Label( self, text="Subsystem:Group",
                             font=labelfont, bd=2, relief=RIDGE )

        self.glabel.pack( side=TOP, fill=X, expand=NO )
        self.paramview.pack( side=TOP, fill=BOTH, expand=YES )

    def refresh( self, subsys, gpath, params ):
        self.subsys, self.gpath = subsys, gpath
        self.glabel.config( text = subsys + ":" + gpath )
        self.paramview.listbox.delete( 0, END )
        self.paramview.listbox.insert( END, *params )

    def modparam( self, event ):
        match = re.match( "^([^:]+):", self.paramview.listbox.get( ACTIVE ) )
        if match:
            param = match.group( 1 )
            parampath = "/cgroup/" + self.subsys + self.gpath + "/" + param
            if stat.S_IWUSR & os.stat( parampath )[stat.ST_MODE]:
                cw = ConfWindow( param, self.gpath )
                self.cgtree.selection( None )

class CgTree( Frame ):
    def __init__( self, parent, grouplist, **params ):
        Frame.__init__( self, parent, **params )
        self.parent = parent
        self.glist = grouplist

        treefont = ( "Sans", 12 )
        self.cgtree = Tree( self, options="hlist.separator /",
                            browsecmd=self.selection )
        self.cgtree.hlist.config(
            font = treefont, selectmod=SINGLE,
            selectbackground = "black" )

        subsyses = self.glist.keys()
        subsyses.sort()
        for subsys in subsyses:
            self.cgtree.hlist.add( subsys ,text=subsys )
            group = self.glist[ subsys ]
            items = group.keys()
            items.sort()
            for item in items:
                self.cgtree.hlist.add( subsys + item, text=group[ item ] )
                self.cgtree.hlist.hide_entry( subsys + item )
        self.cgtree.autosetmode()
        self.cgtree.pack( side=LEFT, fill=BOTH, expand=YES )

    def selection( self, event ):
        entry, = self.cgtree.hlist.info_selection()
        match = re.match( r"^([^/]+)(/.+)?", entry )
        subsys, gpath = match.group( 1 ), match.group( 2 )
        if not gpath: gpath = "/"
        cmd = "cgget -g " + subsys + " " + gpath
        params = []
        entries = commands.getoutput( cmd ).split("\n")
        entries.pop( 0 )
        for line in entries:
            params.append( line )
        self.paramview.refresh( subsys, gpath, params )

def get_grouplist():
    grouplist = {}
    for line in commands.getoutput( "lscgroup" ).split("\n"):
        match = re.match( r"^([^:]+):(/.+)", line )
        if match:
            subsys, gpath = match.group( 1 ), match.group( 2 )
            if not grouplist.has_key( subsys ): grouplist[ subsys ] = {}

            match = re.match( r"^.*/([^/]+)", gpath )
            grouplist[ subsys ][ gpath ] = match.group( 1 )

    return grouplist

class MainPanel():
    def __init__( self, parent, **params ):
        grouplist = get_grouplist()
        self.parent = parent
        self.params = params

        self.paramview = ParamView( self.parent )
        self.cgtree = CgTree( self.parent, grouplist )

        self.paramview.cgtree = self.cgtree 
        self.cgtree.paramview = self.paramview

        self.cgtree.pack( side=LEFT, fill=Y, expand=NO )
        self.paramview.pack( side=LEFT, fill=BOTH, expand=YES )

if __name__ == "__main__":
    root = Tk()
    root.title ( "Controll Groups Viewer" )
    root.geometry( "600x400" )
    mainPanel = MainPanel( root )
    root.mainloop()

# vi:ts=4