Exemple #1
0
class ClientManager(CommandBoxWindow):
    def __init__(self, conn, name='ClientManager'):
        CommandBoxWindow.__init__(self)
        self.set_title('Client Manager')
        self.conn = conn
        self.cfg = PaellaConfig()
        client_cmds = ['import', 'export', 'remove']
        self.add_menu(client_cmds, 'client', self.menu_command)
        self.client_view = ScrollCList()
        self.vbox.add(self.client_view)
        self.client_path = self.cfg.get('management_gui', 'client_path')
        self.client_cfg = Configuration(
            files=[os.path.join(self.client_path, 'config')])
        self.clients = self.client_cfg.sections()
        self.client_view.set_rows(self.clients, ['client'])
        self.dialogs = {}.fromkeys(client_cmds)

    def menu_command(self, menuitem, name):
        client = self.client_view.get_selected_data()[0][0]
        if name == 'export':
            self.export_client(client)
        elif name == 'import':
            self.import_client(client)
        elif name == 'remove':
            self.remove_client(client)
        else:
            dialogs.Message('%s %s' % (name, client))

    def _cpaths_(self, client):
        cpath = os.path.join(self.client_path, client)
        ppath = os.path.join(cpath, 'profiles')
        fpath = os.path.join(cpath, 'families')
        tpath = os.path.join(cpath, 'traits')
        return [cpath, ppath, fpath, tpath]

    def _client_schema(self, client):
        profiles = self.client_cfg.get_list('profiles', client)
        families = self.client_cfg.get_list('families', client)
        traits = self.client_cfg.get_list('traits', client)
        return [profiles, families, traits]

    def _client_mdata(self, client):
        disks = self.client_cfg.get_list('disks', client)
        mtypes = self.client_cfg.get_list('machine_types', client)
        machines = self.client_cfg.get_list('machines', client)
        return [disks, mtypes, machines]

    def export_client(self, client):
        cpath, ppath, fpath, tpath = self._cpaths_(client)
        makepaths(cpath)
        profiles, families, traits = self._client_schema(client)
        disks, mtypes, machines = self._client_mdata(client)
        if not disks:
            disks = None
        if not mtypes:
            mtypes = None
        if not machines:
            machines = None
        element = ClientMachineDatabaseElement(self.conn, disks, mtypes,
                                               machines)
        mdbpath = join(cpath, 'machine_database.xml')
        mdfile = file(mdbpath, 'w')
        mdfile.write(element.toprettyxml())
        mdfile.close()
        if profiles:
            makepaths(ppath)
            pp = PaellaProfiles(self.conn)
            for profile in profiles:
                pp.write_profile(profile, ppath)
        if families:
            makepaths(fpath)
            f = Family(self.conn)
            for family in families:
                f.write_family(family, fpath)
        if traits:
            makepaths(tpath)

    def import_client(self, client):
        cpath, ppath, fpath, tpath = self._cpaths_(client)
        profiles, families, traits = self._client_schema(client)
        mdbpath = join(cpath, 'machine_database.xml')
        if families:
            f = Family(self.conn)
            f.import_families(fpath)
        if profiles:
            pp = PaellaProcessor(self.conn)
            pp.main_path = cpath
            pp.insert_profiles()
        mh = MachineHandler(self.conn)
        md = mh.parse_xmlfile(mdbpath)
        mh.insert_parsed_element(md)

    def remove_client(self, client):
        profiles, families, traits = self._client_schema(client)
        disks, mtypes, machines = self._client_mdata(client)
        cursor = StatementCursor(self.conn)
        if machines:
            cursor.delete(table='machines', clause=In('machine', machines))
        for mtype in mtypes:
            cursor.execute("select * from delete_mtype('%s')" % mtype)
        for disk in disks:
            cursor.execute("select * from delete_disk('%s')" % disk)
        for profile in profiles:
            cursor.execute("select * from delete_profile('%s')" % profile)
        for family in families:
            cursor.execute("select * from delete_family('%s')" % family)
class ClientManager(CommandBoxWindow):
    def __init__(self, conn, name='ClientManager'):
        CommandBoxWindow.__init__(self)
        self.set_title('Client Manager')
        self.conn = conn
        self.cfg = PaellaConfig()
        client_cmds = ['import', 'export', 'remove']
        self.add_menu(client_cmds, 'client', self.menu_command)
        self.client_view = ScrollCList()
        self.vbox.add(self.client_view)
        self.client_path = self.cfg.get('management_gui', 'client_path')
        self.client_cfg = Configuration(files=[os.path.join(self.client_path, 'config')])
        self.clients = self.client_cfg.sections()
        self.client_view.set_rows(self.clients, ['client'])
        self.dialogs = {}.fromkeys(client_cmds)
        
    def menu_command(self, menuitem, name):
        client = self.client_view.get_selected_data()[0][0]
        if name == 'export':
            self.export_client(client)
        elif name == 'import':
            self.import_client(client)
        elif name == 'remove':
            self.remove_client(client)
        else:
            dialogs.Message('%s %s' % (name, client))
            
    def _cpaths_(self, client):
        cpath = os.path.join(self.client_path, client)
        ppath = os.path.join(cpath, 'profiles')
        fpath = os.path.join(cpath, 'families')
        tpath = os.path.join(cpath, 'traits')
        return [cpath, ppath, fpath, tpath]

    def _client_schema(self, client):
        profiles = self.client_cfg.get_list('profiles', client)
        families = self.client_cfg.get_list('families', client)
        traits = self.client_cfg.get_list('traits', client)
        return [profiles, families, traits]
    
    def _client_mdata(self, client):
        disks = self.client_cfg.get_list('disks', client)
        mtypes = self.client_cfg.get_list('machine_types', client)
        machines = self.client_cfg.get_list('machines', client)
        return [disks, mtypes, machines]
    
    def export_client(self, client):
        cpath, ppath, fpath, tpath = self._cpaths_(client)
        makepaths(cpath)
        profiles, families, traits = self._client_schema(client) 
        disks, mtypes, machines = self._client_mdata(client)
        if not disks:
            disks = None
        if not mtypes:
            mtypes = None
        if not machines:
            machines = None
        element = ClientMachineDatabaseElement(self.conn, disks, mtypes, machines)
        mdbpath = join(cpath, 'machine_database.xml')
        mdfile = file(mdbpath, 'w')
        mdfile.write(element.toprettyxml())
        mdfile.close()
        if profiles:
            makepaths(ppath)
            pp = PaellaProfiles(self.conn)
            for profile in profiles:
                pp.write_profile(profile, ppath)
        if families:
            makepaths(fpath)
            f = Family(self.conn)
            for family in families:
                f.write_family(family, fpath)
        if traits:
            makepaths(tpath)

    def import_client(self, client):
        cpath, ppath, fpath, tpath = self._cpaths_(client)
        profiles, families, traits = self._client_schema(client)
        mdbpath = join(cpath, 'machine_database.xml')
        if families:
            f = Family(self.conn)
            f.import_families(fpath)
        if profiles:
            pp = PaellaProcessor(self.conn)
            pp.main_path = cpath
            pp.insert_profiles()
        mh = MachineHandler(self.conn)
        md = mh.parse_xmlfile(mdbpath)
        mh.insert_parsed_element(md)    
        
            
    def remove_client(self, client):
        profiles, families, traits = self._client_schema(client)
        disks, mtypes, machines = self._client_mdata(client)
        cursor = StatementCursor(self.conn)
        if machines:
            cursor.delete(table='machines', clause=In('machine', machines))
        for mtype in mtypes:
            cursor.execute("select * from delete_mtype('%s')" % mtype)
        for disk in disks:
            cursor.execute("select * from delete_disk('%s')" % disk)
        for profile in profiles:
            cursor.execute("select * from delete_profile('%s')" % profile)
        for family in families:
            cursor.execute("select * from delete_family('%s')" % family)
Exemple #3
0
class Manager(CommandBoxWindow):
    def __init__(self, name='Manager'):
        CommandBoxWindow.__init__(self)
        self.cfg = PaellaConfig('database')
        self.dialogs = {}.fromkeys(['dbname', 'suitemanager'])
        self.workspace = {}.fromkeys(
            ['profiles', 'suitemanager', 'traitmanager', 'machines'])
        self.add_menu(dbcommands, 'database', self.database_command)
        self.add_menu(self.workspace.keys(), 'edit', self.edit_command)
        self.set_size_request(150, 200)
        self.conn = None
        self.dbname = None
        self.dblist = ScrollCList()
        self.vbox.add(self.dblist)
        conn = PaellaConnection(self.cfg)
        cursor = StatementCursor(conn, 'quicky')
        self.dblist.set_rows(cursor.select(table='pg_database'))
        cursor.close()
        conn.close()

    def edit_command(self, menuitem, name):
        if self.conn is None:
            dialogs.Message('Not Connected')
        else:
            if name == 'profiles':
                self.workspace['profiles'] = ProfileGenWin(
                    self.conn, self.dbname)
            elif name in ['suitemanager']:
                if not self.dialogs[name]:
                    msg = 'select a suite'
                    self.dialogs[name] = dialogs.CList(msg, name=name)
                    lbox = self.dialogs[name]
                    lbox.set_rows(self.main.select(table='suites'))
                    lbox.set_ok(self.suite_selected)
                    lbox.set_cancel(self.destroy_dialog)
            elif name == 'traitmanager':
                self.workspace['traitmanager'] = TraitManagerWin(self.conn)
            elif name == 'machines':
                self.workspace['machines'] = MainMachineWin(self.conn)

            else:
                raise Error, 'bad edit_command'

    def database_command(self, menuitem, name):
        if name == 'connect':
            if self.conn is None and not self.dialogs['dbname']:
                msg = 'connect to which database?'
                dbname = self.dblist.get_selected_data()[0].datname
                self.dialogs['dbname'] = dialogs.Entry(msg, name='dbname')
                entry = self.dialogs['dbname']
                entry.set_ok(self.ok_dialog)
                entry.set_cancel(self.destroy_dialog)
                entry.set(dbname)
            else:
                dialogs.Message('no multiple connections yet')
        elif name == 'disconnect':
            if self.conn is not None:
                self.main.close()
                self.conn.close()
                self.conn = None
                self.dbname = None
            else:
                dialogs.Message('no connection to leave')
        elif name in ['backup', 'restore']:
            filesel = FileSelection(title='%s database' % name)
            filesel.cancel_button.connect('clicked',
                                          lambda x: filesel.destroy())
            filesel.show()
            bkup_path = self.cfg['import_path']
            filesel.set_filename(self._filepath_(bkup_path))
            filesel.ok_button.connect('clicked', self.ok_file, filesel)
            filesel.set_data('action', name)
        else:
            dialogs.Message('%s unimplemented' % name)

    def ok_file(self, button, filesel):
        path = filesel.get_filename()
        mdpath = join(dirname(path), 'machine_database.xml')
        action = filesel.get_data('action')
        filesel.destroy()
        dir = dirname(path)
        dbm = DatabaseManager(self.conn)
        if action == 'backup':
            dbm.backup(dir)
        elif action == 'restore':
            dbm.restore(dir)

    def suite_selected(self, button):
        name = button.get_name()
        row = self.dialogs[name].get_selected_data()[0]
        debug(row.suite)
        self.destroy_dialog(self.dialogs[name])
        SuiteManager(self.conn, row.suite)

    def _filepath_(self, path):
        return join(path, self.dbname + '.xml')

    def ok_dialog(self, button):
        name = button.get_name()
        if name == 'dbname':
            dbname = self.dialogs[name].get()
            self.dbconnect(dbname)
        self.destroy_dialog(self.dialogs[name])

    def dbconnect(self, dbname):
        self.cfg.change('database')
        dsn = self.cfg.get_dsn()
        dsn['dbname'] = dbname
        self.conn = PaellaConnection(dsn)
        self.main = StatementCursor(self.conn, 'mainManager')
        self.dbname = dbname
        dialogs.Message('connected to database %s' % dbname)

    def destroy_dialog(self, dying):
        name = dying.get_name()
        self.dialogs[name] = None
        dying.destroy()
class Manager(CommandBoxWindow):
    def __init__(self, name='Manager'):
        CommandBoxWindow.__init__(self)
        self.cfg = PaellaConfig('database')
        self.dialogs = {}.fromkeys(['dbname', 'suitemanager'])
        apps = ['profiles', 'families', 'suitemanager', 'traitmanager', 'machines',
                'traits', 'tdiff', 'sdiff', 'fdiff', 'default_environment', 'clients']
        self.workspace = {}.fromkeys(apps)
        self.add_menu(dbcommands, 'database', self.database_command)
        self.add_menu(self.workspace.keys(), 'edit', self.edit_command)
        self.set_size_request(150,200)
        self.conn = None
        self.dbname = None
        self.dblist = ScrollCList()
        self.vbox.add(self.dblist)
        conn = PaellaConnection(self.cfg)
        cursor = StatementCursor(conn, 'quicky')
        self.dblist.set_rows(cursor.select(table='pg_database'))
        cursor.close()
        conn.close()
        self.tbar.add_button('profiles', 'profile manager', self.run_tbar)
        self.tbar.add_button('families', 'family manager', self.run_tbar)
        self.tbar.add_button('machines', 'machine manager', self.run_tbar)
        self.tbar.add_button('traits', 'trait manager', self.run_tbar)
        self.tbar.add_button('tdiff', 'template differ', self.run_tbar)
        self.tbar.add_button('sdiff', 'script differ', self.run_tbar)
        self.tbar.add_button('fdiff', 'family differ', self.run_tbar)
        
    def edit_command(self, menuitem, name):
        if self.conn is None:
            dialogs.Message('Not Connected')
        else:
            if name == 'profiles':
                self.workspace['profiles'] = ProfileGenWin(self.conn, self.dbname)
            elif name in ['suitemanager']:
                if not self.dialogs[name]:
                    msg = 'select a suite'
                    self.dialogs[name] = dialogs.CList(msg, name=name)
                    lbox = self.dialogs[name]
                    lbox.set_rows(self.main.select(table='suites'))
                    lbox.set_ok(self.suite_selected)
                    lbox.set_cancel(self.destroy_dialog)
            else: self.run_tbar(None, name)
            

    def run_tbar(self, button=None, data=None):
        if data == 'profiles':
            self.workspace[data] = ProfileGenWin(self.conn, self.dbname)
        elif data == 'machines':
            self.workspace[data] = MainMachineWin(self.conn)
        elif data == 'traits':
            self.workspace[data] = TraitManagerWin(self.conn)
        elif data == 'families':
            self.workspace[data] = FamilyWin(self.conn)
        elif data == 'tdiff':
            self.workspace[data] = DifferWin(self.conn, 'template')
        elif data == 'sdiff':
            self.workspace[data] = DifferWin(self.conn, 'script')
        elif data == 'fdiff':
            self.workspace[data] = DifferWin(self.conn, 'family')
        elif data == 'default_environment':
            self.workspace[data] = DefEnvEditor(self.conn)
        elif data == 'clients':
            self.workspace[data] = ClientManager(self.conn)
        else:
            dialogs.Message('bad command %s' % data)
        
    def database_command(self, menuitem, name):
        if name == 'connect':
            if self.conn is None and not self.dialogs['dbname']:
                msg = 'connect to which database?'
                dbname = self.dblist.get_selected_data()[0].datname
                self.dialogs['dbname']  = dialogs.Entry(msg, name='dbname')
                entry = self.dialogs['dbname']
                entry.set_ok(self.ok_dialog)
                entry.set_cancel(self.destroy_dialog)
                entry.set(dbname)
            else:
                dialogs.Message('no multiple connections yet')
        elif name == 'disconnect':
            if self.conn is not None:
                self.main.close()
                self.conn.close()
                self.conn = None
                self.dbname = None
            else:
                dialogs.Message('no connection to leave')
        elif name in ['backup', 'restore']:
            filesel = FileSelection(title='%s database' %name)
            filesel.cancel_button.connect('clicked',
                                          lambda x: filesel.destroy())
            filesel.show()
            bkup_path = self.cfg['import_path']
            filesel.set_filename(self._filepath_(bkup_path))
            filesel.ok_button.connect('clicked', self.ok_file, filesel)
            filesel.set_data('action', name)
        else:
            dialogs.Message('%s unimplemented'%name)

    def ok_file(self, button, filesel):
        path = filesel.get_filename()
        mdpath = join(dirname(path), 'machine_database.xml')
        action = filesel.get_data('action')
        filesel.destroy()
        dir = dirname(path)
        dbm = DatabaseManager(self.conn)
        if action == 'backup':
            dbm.backup(dir)
        elif action == 'restore':
            dbm.restore(dir)
            
    def suite_selected(self, button):
        name = button.get_name()
        row = self.dialogs[name].get_selected_data()[0]
        debug(row.suite)
        self.destroy_dialog(self.dialogs[name])
        SuiteManager(self.conn, row.suite)
        
        
    def _filepath_(self, path):
        return join(path, self.dbname + '.xml')
    
    def ok_dialog(self, button):
        name = button.get_name()
        if name == 'dbname':
            dbname = self.dialogs[name].get()
            self.dbconnect(dbname)
        self.destroy_dialog(self.dialogs[name])
        
    def dbconnect(self, dbname):
        self.cfg.change('database')
        dsn = self.cfg.get_dsn()
        dsn['dbname'] = dbname
        self.conn = PaellaConnection(dsn)
        self.main = StatementCursor(self.conn, 'mainManager')
        self.dbname = dbname
        dialogs.Message('connected to database %s' %dbname)

        
    def destroy_dialog(self, dying):
        name = dying.get_name()
        self.dialogs[name] = None
        dying.destroy()