def add_mount_to_filesystem(conn, mnt_name, filesystem, ord, partition): cursor = StatementCursor(conn) data = dict(mnt_name=mnt_name, filesystem=filesystem, ord=str(ord), partition=partition) cursor.insert(table='filesystem_mounts', data=data)
class TableEditor(ListWin): def __init__(self, conn, table, pkey=None, fields=[], command_data=dict(new='new entry', edit='edit entry')): ListWin.__init__(self) self.conn = conn self.cursor = StatementCursor(self.conn) self.cursor.set_table(table) self._cdata = command_data self.tbar.add_button('new', self._cdata['new'], self.toolbar_button_pressed) self.tbar.add_button('edit', self._cdata['edit'], self.toolbar_button_pressed) if pkey is None: print get_pkey_info(StatementCursor(conn), table) self._pkey = pkey self.reset_rows() self._fields = fields self.fields = [pkey] + self._fields self.dialogs = {}.fromkeys(self._cdata.keys()) def reset_rows(self): self.set_rows(self.cursor.select(order=self._pkey)) def toolbar_button_pressed(self, button, data): row = None if data == 'new': if self.dialogs['new'] is None: row = self.cursor.select()[0] self._make_dialog('new', row, self.add_new_record) elif data == 'edit': if self.dialogs['edit'] is None: row = get_single_row(self.listbox, self._pkey) if row is not None: self._make_dialog('edit', row, self.edit_record, row[self._pkey]) def _make_dialog(self, name, row, okfun, pkey=None): if name == 'edit': fields = self._fields else: fields = self.fields make_record_dialog(self, name, row, okfun, pkey, fields, self._cdata) def add_new_record(self, *args): dialog = self.dialogs['new'] self.cursor.insert(data=dict(dialog.items())) self.destroy_dialog(dialog) self.reset_rows() def edit_record(self, *args): dialog = self.dialogs['edit'] clause = Eq(self._pkey, dialog.pkey) data = dict(dialog.items()) self.cursor.update(data=data, clause=clause) self.destroy_dialog(dialog) self.reset_rows()
def make_a_machine(conn, machine, mtype, profile, fs): cursor = StatementCursor(conn) data = dict(machine=machine, machine_type=mtype, profile=profile, filesystem=fs) cursor.insert(table='machines', data=data)
def add_new_mount(conn, name, mtpt, fstype, opts, dump='0', pass_='0'): cursor = StatementCursor(conn) data = dict(mnt_name=name, mnt_point=mtpt, fstype=fstype, mnt_opts=opts, dump=dump) data['pass'] = pass_ cursor.insert(table='mounts', data=data)
class TextFileManager(object): def __init__(self, conn): object.__init__(self) self.conn = conn self.cursor = StatementCursor(self.conn) self.cursor.set_table('textfiles') def insert_file(self, datafile): md5 = self._md5sum(datafile) data = datafile.read() md5size = '_'.join([md5, str(len(data))]) return self._insert_data(md5size, data) def insert_data(self, data): md5 = md5sum(strfile(data)) md5size = '_'.join([md5, str(len(data))]) return self._insert_data(md5size, data) def _insert_data(self, md5size, data): clause=Eq('md5size', md5size) try: row = self.cursor.select_row(clause=clause) except NoExistError: row = None if not row: self.cursor.insert(data={'md5size' : md5size, 'data' : data}) row = self.cursor.select_row(clause=clause) return row.fileid def get_data(self, id): row = self.cursor.select_row(clause=Eq('fileid', id)) return row.data def get_strfile(self, id): return strfile(self.get_data(id)) def _md5sum(self, datafile): datafile.seek(0) md5 = md5sum(datafile) datafile.seek(0) return md5
class TextFileManager(object): def __init__(self, conn): object.__init__(self) self.conn = conn self.cursor = StatementCursor(self.conn) self.cursor.set_table('textfiles') def insert_file(self, datafile): md5 = self._md5sum(datafile) data = datafile.read() md5size = '_'.join([md5, str(len(data))]) return self._insert_data(md5size, data) def insert_data(self, data): md5 = md5sum(strfile(data)) md5size = '_'.join([md5, str(len(data))]) return self._insert_data(md5size, data) def _insert_data(self, md5size, data): clause = Eq('md5size', md5size) try: row = self.cursor.select_row(clause=clause) except NoExistError: row = None if not row: self.cursor.insert(data={'md5size': md5size, 'data': data}) row = self.cursor.select_row(clause=clause) return row.fileid def get_data(self, id): row = self.cursor.select_row(clause=Eq('fileid', id)) return row.data def get_strfile(self, id): return strfile(self.get_data(id)) def _md5sum(self, datafile): datafile.seek(0) md5 = md5sum(datafile) datafile.seek(0) return md5
class PaellaProfiles(Element): def __init__(self, conn): Element.__init__(self, 'profiles') self.conn = conn self.stmt = StatementCursor(self.conn) self.env = ProfileEnvironment(self.conn) self.profiletraits = ProfileTrait(self.conn) self._profiles = {} for row in self.stmt.select(table='profiles', order='profile'): self._append_profile(row.profile, row.suite) def _append_profile(self, profile, suite): element = self.export_profile(profile, suite) self._profiles[profile] = element self.appendChild(self._profiles[profile]) def export_profile(self, profile, suite=None): if suite is None: row = self.stmt.select_row(table='profiles',clause=Eq('profile', profile)) suite = row['suite'] suite = str(suite) profile = str(profile) self.env.set_profile(profile) element = ProfileElement(profile, suite) element.append_traits(self.profiletraits.trait_rows(profile)) element.append_variables(self.env.get_rows()) return element def insert_profile(self, profile): idata = {'profile' : profile.name, 'suite' : profile.suite} self.stmt.insert(table='profiles', data=idata) idata = {'profile' : profile.name, 'trait' : None, 'ord' : 0} for trait, ord in profile.traits: print trait, ord idata['trait'] = trait idata['ord'] = ord #str(ord) self.stmt.insert(table='profile_trait', data=idata) idata = {'profile' : profile.name, 'trait' : None, 'name' : None, 'value': None} for trait, name, value in profile.vars: idata['trait'] = trait idata['name'] = name idata['value'] = value self.stmt.insert(table='profile_variables', data=idata) def export_profiles(self, path): rows = self.stmt.select(fields='profile', table='profiles', clause=None) for row in rows: self.write_profile(row.profile, path) def write_profile(self, profile, path): xmlfile = file(join(path, '%s.xml' % profile), 'w') data = self.export_profile(profile) data.writexml(xmlfile, indent='\t', newl='\n', addindent='\t') xmlfile.close()
class VariablesConfig(RawConfigParser): def __init__(self, conn, table, section, mainfield=None, mainvalue=None, option='name', value='value'): self.conn = conn self.cursor = StatementCursor(self.conn) self.cursor.set_table(table) self._secfield = section bothnone = mainfield is None and mainvalue is None bothset = mainfield and mainvalue if not bothnone and not bothset: raise Error, 'both mainfield and mainvalue need to be set/unset' self._mainclause = None if bothset: self._mainclause = Eq(mainfield, mainvalue) self._fields = [self._secfield, option, value] RawConfigParser.__init__(self) for row in self.cursor.select(fields=self._fields, clause=self._mainclause): if row[0] not in self.sections(): self.add_section(row[0]) self.set(*row) def write(self, cfile): sections = self.sections() sections.sort() for section in sections: cfile.write('[%s]\n' % section) keys = self.options(section) keys.sort() for k in keys: if k != '__name__': v = str(self.get(section, k)).replace('\n', '\n\t') cfile.write('%s:\t%s\n' % (k, v)) cfile.write('\n') def edit(self): tmp, path = tempfile.mkstemp('variables', 'config') tmp = file(path, 'w') self.write(tmp) tmp.close() os.system('$EDITOR %s' % path) tmp = file(path, 'r') newconfig = RawConfigParser() newconfig.readfp(tmp) tmp.close() os.remove(path) return newconfig def diff(self, other): ltmp, lpath = tempfile.mkstemp('variables', 'config') ltmp = file(lpath, 'w') self.write(ltmp) ltmp.close() rtmp, rpath = tempfile.mkstemp('variables', 'config') rtmp = file(rpath, 'w') other.write(rtmp) rtmp.close() os.system('xxdiff %s %s' % (lpath, rpath)) ltmp, rtmp = file(lpath, 'r'), file(rpath, 'r') lcfg, rcfg = RawConfigParser(), RawConfigParser() lcfg.readfp(ltmp) rcfg.readfp(rtmp) ltmp.close() rtmp.close() self.update(lcfg) other.update(rcfg) def update(self, newconfig): removed = [x for x in self.sections() if x not in newconfig.sections()] for section in removed: print 'removing', section sclause = Eq(self._secfield, section) if self._mainclause: sclause &= self._mainclause self.cursor.delete(clause=sclause) for section in newconfig.sections(): print section sclause = Eq(self._secfield, section) if self._mainclause: sclause = self._mainclause & Eq(self._secfield, section) if not self.has_section(section): for k, v in newconfig.items(section): idata = dict(zip(self._fields, [section, k, v])) if self._mainclause: idata[self._mainclause.left] = self._mainclause.right print idata self.cursor.insert(data=idata) else: for name, value in newconfig.items(section): nclause = sclause & Eq(self._fields[1], name) #print 'nclause', nclause #print 'value', self.get(section, name) if self.has_option(section, name): if value != self.get(section, name): #print 'updating' self.cursor.update(data={self._fields[2]: value}, clause=nclause) else: idata = dict(zip(self._fields, [section, name, value])) if self._mainclause: idata[ self._mainclause.left] = self._mainclause.right self.cursor.insert(data=idata) if self.has_section(section): for name, value in self.items(section): if not newconfig.has_option(section, name): clause = sclause & Eq(self._fields[1], name) #print 'dclause', clause self.cursor.delete(clause=clause)
class RepositoryManager(object): def __init__(self, conn): self.conn = conn self.main = StatementCursor(self.conn, 'RepositoryMain') self.repos = StatementCursor(self.conn, 'Repository') self.repos.set_table('repository') self.sources = StatementCursor(self.conn, 'Sources') self.sources.set_table('sources') self.release = ReleaseCursor(self.conn) self.repsections = StatementCursor(self.conn, 'repos_section') self.repsections.set_table('repos_section') self.__init_db__() def __init_db__(self): if not len(self.main.tables()): map(self.main.create_table, primary_tables()) def drop_source(self, name, type): nameclause = Eq('name', name) clause = nameclause & Eq('type', type) for section in self.get_sections(name): self.main.drop(list_tablename(name, type, section)) self.sources.delete(clause=clause) sources = self.sources.select(clause=clause) if not len(sources): self.repsections.delete(clause=nameclause) self.repos.delete(clause=nameclause) def add_source(self, name, source): source = make_source(source) if name not in [x.name for x in self.repos.select()]: self.repos.insert(data=dict(name=name)) clause = Eq('name', name) & Eq('type', source.type) count = int(self.sources.select(fields=['count(name)'], clause=clause)[0][0]) if count == 0: if islocaluri(source.uri): data = dict(name=name, type=source.type, uri=source.uri, suite=source.suite) self.sources.insert(data=data) current_sections = self.get_sections(name) sdata = dict(name=name, section=None) for section in source.sections: if section not in current_sections: sdata['section'] = section self.repsections.insert(data=sdata) fullparse = FullParseTable(name, source.type, section) if fullparse.name not in self.main.tables(): self.main.create_table(fullparse) listtable = ListTable(name, source.type, section) if listtable.name not in self.main.tables(): self.main.create_table(listtable) else: raise Error, 'local uris first' else: if not islocaluri(source.uri): data = dict(remote=source.uri) self.sources.update(data=data, clause=clause) else: raise ExistsError, 'already there' def get_sections(self, name): clause = Eq('name', name) return [x.section for x in self.repsections.iselect(fields=['section'], clause=clause)] def make_source_line(self, name, type, remote=False): repsrc = self.make_source(name, type, remote) return str(repsrc) def make_source(self, name, type, remote=False): clause = Eq('name', name) & Eq('type', type) source = self.sources.select_row(clause=clause) repsrc = RepositorySource() repsrc.type = source.type if remote: repsrc.uri = source.remote else: repsrc.uri = source.uri repsrc.suite = source.suite repsrc.sections = self.get_sections(name) repsrc.set_path() return repsrc def listfile(self, name, type, section=None): source = self.make_source(name, type) release = Release(source) print 'Need to pull from database!!!' if source.has_release(): return join(source.distpath, release.path(section)) else: return join(source.distpath, source.listfile()) def get_remote(self, name, type, remote=True): return self.make_source(name, type, remote=True) def parse_section(self, name, type, section=None): listfile = self.listfile(name, type, section) debug(listfile) if not isfile(listfile): raise NoFileError, 'file not there' if type == 'deb': return full_parse(listfile) elif type == 'deb-src': return parse_sources(listfile) else: raise Error, 'bad source type' def add_repository(self, name): self.repos.insert(data=dict(name=name))
class Family(object): def __init__(self, conn): object.__init__(self) self.conn = conn self.suites = Suites(conn).list() self.cursor = StatementCursor(self.conn) self.current = None self.parent = SimpleRelation(self.conn, 'family_parent', 'family') self.env = FamilyEnvironment(self.conn) def set_family(self, family): self.current = family self.parent.set_current(family) self.env.set_family(family) def add_family(self, family, type='general'): pass def get_related_families(self, families=[]): rows = self.cursor.select(table='family_parent') graph = kjGraph([(r.family, r.parent) for r in rows]) dfamilies = Set() for fam in families: dfamilies |= Set([fam]) | Set(graph.reachable(fam).items()) return dfamilies def parent_rows(self, family=None): if family is None: family = self.current self.parent.set_clause(family) rows = self.parent.cmd.select(fields=['parent'], order='parent') self.parent.reset_clause() return rows def parents(self, family=None): rows = self.parent_rows(family) return [x.parent for x in rows] def environment_rows(self, family=None): if family is None: family = self.current clause = Eq('family', family) args = dict(fields=['trait', 'name', 'value'], clause=clause, order=['trait', 'name']) return self.env.cursor.select(**args) def family_rows(self): return self.cursor.select(fields=['family'], table='families', order='family') def all_families(self): return [r.family for r in self.family_rows()] def get_all_defaults(self): stmt = select_multisuite_union(self.suites, 'variables') print stmt self.cursor.execute(stmt) return self.cursor.fetchall() def create_family(self, family): if family not in self.all_families(): self.cursor.insert(table='families', data=dict(family=family)) else: raise ExistsError, '%s already exists' % family def insert_parents(self, parents): self.parent.insert('parent', parents) def FamilyData(self, families=[]): if families is None: families = [self.current] all = self.make_familylist(families) superdict = {} for f in all: superdict.update(self.env.make_tagdict(f)) return superdict def make_familylist(self, families): deps = families all = list(self.get_related_families(families)) setfun = self.set_family parfun = self.parents return make_deplist(deps, all, setfun, parfun) def export_family(self, family=None): if family is None: family = self.current element = FamilyElement(family) element.append_parents(self.parents(family)) element.append_variables(self.environment_rows(family)) return element def write_family(self, family, path): fxml = file(join(path, '%s.xml' % family), 'w') data = self.export_family(family) data.writexml(fxml, indent='\t', newl='\n', addindent='\t') fxml.close() def export_families(self, path): families = self.all_families() for f in families: self.write_family(f, path) def import_family(self, element): parsed = FamilyParser(element) print 'inserting family', parsed.name all = self.all_families() for p in parsed.parents: if p not in all: print 'insertion failed for', parsed.name raise UnbornError self.create_family(parsed.name) self.set_family(parsed.name) self.insert_parents(parsed.parents) row = dict(family=parsed.name) for var in parsed.environ: row.update(var) self.cursor.insert(table='family_environment', data=row) def import_families(self, path): xmlfiles = [ join(path, x) for x in os.listdir(path) if x[-4:] == '.xml' ] families = [] for f in xmlfiles: xml = parse_file(f) elements = xml.getElementsByTagName('family') if len(elements) != 1: raise Error, 'bad number of family tags %s' % len(elements) element = elements[0] families.append(element) while len(families): f = families[0] try: self.import_family(f) except UnbornError: families.append(f) del families[0] print len(families), 'families inserted'
class ProfileGenWin(CommandBoxWindow): def __init__(self, conn, name='ProfileGenWin'): actions = ['create', 'copy', 'export', 'import'] CommandBoxWindow.__init__(self, name=name) self.set_title(name) self.conn = conn self.cmd = StatementCursor(conn, name) self.suites = [x.suite for x in self.cmd.select(table='suites')] self.profiles = StatementCursor(conn, 'profiles') self.profiles.set_table('profiles') self.menu_bar = SimpleMenuBar() self.vbox.pack_start(self.menu_bar, 0, 0, 0) self.dialogs = {}.fromkeys(actions) self.add_menu(actions, 'main', self.ask_dialog) self.add_menu(self.suites, 'traits', self.show_traits) self.add_menu(self.suites, 'traitgen', self.show_traitgen) self.browser = ProfileBrowser(self.conn, self.suites) self.vbox.add(self.browser) self.set_size_request(400, 300) def ask_dialog(self, button, data): if not self.dialogs[data]: if data == 'create': self.dialogs[data] = dialogs.Entry('create profile', name='create') self.dialogs[data].set_ok(self.create_profile) elif data == 'copy': self.dialogs[data] = dialogs.CList('copy profile', name='copy') dialog = self.dialogs[data] dialog.set_rows( self.profiles.select(fields='profile', order='profile')) dialog.set_ok(self.src_profile_selected) elif data == 'export': pdb = PaellaDatabase(self.conn) profiles = pdb.profiles self.dialogs[data].set_cancel(self.destroy_dialog) def destroy_dialog(self, dying): name = dying.get_name() self.dialogs[name] = None dying.destroy() def create_profile(self, button): name = button.get_name() debug(name) if name == 'create': profile = self.dialogs[name].get() if profile not in [p.profile for p in self.profiles.select()]: self.profiles.insert(data={ 'profile': profile, 'suite': 'woody' }) self.destroy_dialog(self.dialogs[name]) self.browser.reset_rows() else: dialogs.Dialog('there was a problem') def src_profile_selected(self, button): row = get_single_row(self.dialogs['copy'], 'profile') def show_packages(self, button, data): PackagesWindow(self.conn, data) def show_traits(self, button, data): TraitsWindow(self.conn, data) def show_traitgen(self, button, data): TraitGenWin(self.conn, data)
class Family(object): def __init__(self, conn): object.__init__(self) self.conn = conn self.suites = Suites(conn).list() self.cursor = StatementCursor(self.conn) self.current = None self.parent = SimpleRelation(self.conn, 'family_parent', 'family') self.env = FamilyEnvironment(self.conn) def set_family(self, family): self.current = family self.parent.set_current(family) self.env.set_family(family) def add_family(self, family, type='general'): pass def get_related_families(self, families=[]): rows = self.cursor.select(table='family_parent') graph = kjGraph([(r.family, r.parent) for r in rows]) dfamilies = Set() for fam in families: dfamilies |= Set([fam]) | Set(graph.reachable(fam).items()) return dfamilies def parent_rows(self, family=None): if family is None: family = self.current self.parent.set_clause(family) rows = self.parent.cmd.select(fields=['parent'], order='parent') self.parent.reset_clause() return rows def parents(self, family=None): rows = self.parent_rows(family) return [x.parent for x in rows] def environment_rows(self, family=None): if family is None: family = self.current clause = Eq('family', family) args = dict(fields=['trait', 'name', 'value'], clause=clause, order=['trait', 'name']) return self.env.cursor.select(**args) def family_rows(self): return self.cursor.select(fields=['family'], table='families', order='family') def all_families(self): return [r.family for r in self.family_rows()] def get_all_defaults(self): stmt = select_multisuite_union(self.suites, 'variables') print stmt self.cursor.execute(stmt) return self.cursor.fetchall() def create_family(self, family): if family not in self.all_families(): self.cursor.insert(table='families', data=dict(family=family)) else: raise ExistsError, '%s already exists' % family def insert_parents(self, parents): self.parent.insert('parent', parents) def FamilyData(self, families=[]): if families is None: families = [self.current] all = self.make_familylist(families) superdict = {} for f in all: superdict.update(self.env.make_tagdict(f)) return superdict def make_familylist(self, families): deps = families all = list(self.get_related_families(families)) setfun = self.set_family parfun = self.parents return make_deplist(deps, all, setfun, parfun) def export_family(self, family=None): if family is None: family = self.current element = FamilyElement(family) element.append_parents(self.parents(family)) element.append_variables(self.environment_rows(family)) return element def export_families(self, path): families = self.all_families() for f in families: fxml = file(join(path, '%s.xml' % f), 'w') data = self.export_family(f) data.writexml(fxml, indent='\t', newl='\n', addindent='\t') fxml.close() def import_family(self, element): parsed = FamilyParser(element) print 'inserting family', parsed.name all = self.all_families() for p in parsed.parents: if p not in all: print 'insertion failed for', parsed.name raise UnbornError self.create_family(parsed.name) self.set_family(parsed.name) self.insert_parents(parsed.parents) row = dict(family=parsed.name) for var in parsed.environ: row.update(var) self.cursor.insert(table='family_environment', data=row) def import_families(self, path): xmlfiles = [join(path, x) for x in os.listdir(path) if x[-4:] == '.xml'] families = [] for f in xmlfiles: xml = parse_file(f) elements = xml.getElementsByTagName('family') if len(elements) != 1: raise Error, 'bad number of family tags %s' % len(elements) element = elements[0] families.append(element) while len(families): f = families[0] try: self.import_family(f) except UnbornError: families.append(f) del families[0] print len(families), 'families inserted'
class Profile(StatementCursor): def __init__(self, conn): StatementCursor.__init__(self, conn) self.conn = conn self.set_table('profiles') self._traits = ProfileTrait(conn) self._env = ProfileEnvironment(conn) self._pfam = StatementCursor(conn) self._pfam.set_table('profile_family') self._fam = Family(conn) def drop_profile(self, profile): self.delete(clause=Eq('profile', profile)) def set_profile(self, profile): self.clause = Eq('profile', profile) self.current = self.select_row(clause=self.clause) self._traits.set_profile(profile) self._env.set_profile(profile) def get_profile_data(self): return self._env.ProfileData() def get_family_data(self): families = self.get_families() return self._fam.FamilyData(families) def make_traitlist(self, log=None): tp = TraitParent(self.conn, self.current.suite) listed = [x.trait for x in self._traits.trait_rows()] all = list(tp.get_traitset(listed)) setfun = tp.set_trait parfun = tp.parents return make_deplist(listed, all, setfun, parfun, log) def family_rows(self, profile=None): if profile is None: profile = self.current.profile return self._pfam.select(clause=Eq('profile', profile), order='family') def get_families(self, profile=None): return [r.family for r in self.family_rows(profile)] def get_trait_rows(self): return self._traits.trait_rows() def append_family(self, family): if family not in self.get_families(): self._pfam.insert( data=dict(profile=self.current.profile, family=family)) def append_trait(self, trait, ord): self._traits.insert_trait(trait, ord) def set_suite(self, suite): clause = Eq('profile', self.current.profile) self.update(data=dict(suite=suite), clause=clause) self.set_profile(self.current.profile) def copy_profile(self, src, dest): current = self.current self.set_profile(src) pfield = "'%s' as profile" % dest pclause = Eq('profile', src) pcursor = self tcursor = self._traits.cmd fcusor = self._pfam vcursor = self._env.env.cursor cursors = [pcursor, tcursor, fcusor, vcursor] for cursor in cursors: sel = str( cursor.stmt.select(fields=[pfield] + cursor.fields()[1:], clause=pclause)) cursor.execute('insert into %s (%s)' % (cursor.stmt.table, sel)) self.set_profile(current.profile)
class BaseDatabase(QSqlDatabase): def __init__(self, dsn, name, parent=None, objname=None): QSqlDatabase.__init__(self, "QPSQL7", name, parent, objname) self.conn = BasicConnection(**dsn) self.setDatabaseName(dsn["dbname"]) self.setHostName(dsn["host"]) self.dbuser = dsn["user"] self.setUserName(self.dbuser) self.stmt = Statement() self.mcursor = StatementCursor(self.conn) def set_table(self, table): self.stmt.table = table def set_clause(self, items, cmp="=", join="and"): self.stmt.set_clause(items, cmp=cmp, join=join) def set_data(self, data): self.stmt.set(data) def set_fields(self, fields): self.stmt.fields = fields def qdelete(self, table=None, clause=None): query = self.stmt.delete(table=table, clause=clause) return self.execStatement(query) def qinsert(self, table=None, data=None): query = self.stmt.insert(table=table, data=data) return self.execStatement(query) def qupdate(self, table=None, data=None, clause=None): query = self.stmt.update(table=table, data=data, clause=clause) return self.execStatement(query) def qselect(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) return self.execStatement(query) def delete(self, table=None, clause=None): query = self.stmt.delete(table=table, clause=clause) return self.mcursor.execute(query) def insert(self, table=None, data=None): query = self.stmt.insert(table=table, data=data) return self.mcursor.execute(query) def update(self, table=None, data=None, clause=None): query = self.stmt.update(table=table, data=data, clause=clause) return self.mcursor.execute(query) def select(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) return self.mcursor.fetchall() def select_row(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) rows = len(self.mcursor) if rows == 1: return self.mcursor.next() elif rows == 0: raise NoExistError else: raise Error, "bad row count %s" % rows def clear(self, **args): self.stmt.clear(**args) def stdclause(self, data): return reduce(and_, [Eq(k, v) for k, v in data.items()]) def insertData(self, idcol, table, data, commit=True): clause = self.stdclause(data) try: self.mcursor.select_row(fields=[idcol], table=table, clause=clause) except NoExistError: self.mcursor.insert(table=table, data=data) if commit: self.conn.commit() def identifyData(self, idcol, table, data, commit=True): clause = self.stdclause(data) self.insertData(idcol, table, data, commit=commit) return self.mcursor.select_row(fields=["*"], table=table, clause=clause)
class ProfileBrowser(ListNoteBook, HasDialogs): def __init__(self, conn, suites, name='ProfileBrowser'): self.menu = self.__make_mainmenu_(suites) ListNoteBook.__init__(self, name=name) self.conn = conn self.profiles = Profile(self.conn) self.profiletrait = ProfileTrait(self.conn) self.family = Family(self.conn) self.pfamily = StatementCursor(self.conn) self.pfamily.set_table('profile_family') self.trait_menu = make_menu(['drop', 'order'], self.trait_command) self.pdata_menu = make_menu(['edit', 'drop'], self.variable_command) self.family_menu = make_menu(['drop'], self.family_command) self.reset_rows() self.append_page(ScrollCList(rcmenu=self.trait_menu), 'traits') self.append_page(ScrollCList(rcmenu=self.pdata_menu), 'variables') self.append_page(ScrollCList(rcmenu=self.family_menu), 'families') self.dialogs = {}.fromkeys(['order']) def __make_mainmenu_(self, suites): suite_commands = ['change to %s' % suite for suite in suites] profile_commands = ['drop', 'set defaults', 'append defaults'] commands = suite_commands + profile_commands return make_menu(commands, self.profile_command) def reset_rows(self): self.set_rows( self.profiles.select(fields=['profile', 'suite'], order='profile')) self.set_row_select(self.profile_selected) def profile_selected(self, listbox, row, column, event): row = listbox.get_selected_data()[0] self.current = row self.select_profile(self.current.profile) def select_profile(self, profile): self.variables = ProfileEnvironment(self.conn, profile) self.profiletrait.set_profile(profile) self.__set_pages(profile) def __set_pages(self, profile): pages = dict(self.pages) #set traits for profile pages['traits'].set_rows(self.profiletrait.trait_rows()) pages['traits'].set_select_mode('multi') clause = Eq('profile', self.current.profile) cursor = self.variables.env.cursor pages['variables'].set_rows(cursor.select(clause=clause), ['trait']) pages['variables'].set_select_mode('multi') pfrows = self.pfamily.select(fields=['family'], clause=clause) pages['families'].set_rows(pfrows) pages['families'].set_select_mode('multi') self.__set_droptargets__(pages) def __set_droptargets__(self, pages): set_receive_targets(pages['traits'].listbox, self.drop_trait, TARGETS.get('trait', self.current.suite)) set_receive_targets(pages['families'].listbox, self.drop_families, FTargets.get('family', 'flavor')) def trait_command(self, menuitem, action): traits = self._get_listbox('traits', 'trait') if action == 'drop': clause = In('trait', traits) & Eq('profile', self.current.profile) self.profiletrait.cmd.delete(clause=clause) self.__set_pages(self.current.profile) elif action == 'order': if not self.dialogs['order']: self.dialogs['order'] = dialogs.Entry('enter order', name='order') self.dialogs['order'].set_ok(self.set_order) self.dialogs['order'].set_cancel(self.destroy_dialog) def variable_command(self, menuitem, action): rows = self.pages['variables'].get_selected_data() cursor = self.variables.env.cursor if action == 'drop': for row in rows: clause = Eq('profile', self.current.profile) & Eq( 'trait', row.trait) clause &= Eq('name', row.name) cursor.delete(clause=clause) elif action == 'edit': self.edit_profilevars() def family_command(self, menuitem, action): print action families = [x[0] for x in self.pages['families'].get_selected_data()] if action == 'drop': clause = Eq('profile', self.current.profile) & In( 'family', families) self.pfamily.delete(clause=clause) def edit_profilevars(self): config = ProfileVariablesConfig(self.conn, self.current.profile) newconfig = config.edit() config.update(newconfig) self.select_profile(self.current.profile) def set_order(self, button): dialog = self.dialogs['order'] ord = dialog.get() rows = self.pages['traits'].get_selected_data() pclause = Eq('profile', self.current.profile) for row in rows: clause = pclause & Eq('trait', row.trait) self.profiletrait.update(data=dict(ord=ord), clause=clause) def drop_trait(self, listbox, context, x, y, selection, targettype, time): traits = keysplitter(selection) self.profiletrait.insert_traits(traits) self.__set_pages(self.current.profile) def drop_families(self, listbox, context, x, y, selection, targettype, time): families = keysplitter(selection) clause = Eq('profile', self.current.profile) data = dict(profile=self.current.profile) current = [x.family for x in self.pfamily.select(clause=clause)] for f in families: if f not in current: data['family'] = f self.pfamily.insert(data=data) self.__set_pages(self.current.profile) def _get_listbox(self, page, field): pages = dict(self.pages) return [row[field] for row in pages[page].listbox.get_selected_data()] def profile_command(self, menu, command): if command[:10] == 'change to ': self.change_suite(command[10:]) elif command == 'drop': self.profiletrait.drop_profile(self.current.profile) self.profiles.drop_profile(self.current.profile) self.current = None self.reset_rows() elif command == 'set defaults': self.variables.set_defaults() elif command == 'append defaults': self.variables.append_defaults() else: raise Error, 'bad command %s' % command def change_suite(self, suite): clause = Eq('profile', self.current.profile) self.profiles.update(data={'suite': suite}, clause=clause) print 'changing suite to ', suite self.current_suite = suite self.reset_rows()
class ProfileBrowser(ListNoteBook, HasDialogs): def __init__(self, conn, suites, name='ProfileBrowser'): self.menu = self.__make_mainmenu_(suites) ListNoteBook.__init__(self, name=name) self.conn = conn self.profiles = Profile(self.conn) self.profiletrait = ProfileTrait(self.conn) self.family = Family(self.conn) self.pfamily = StatementCursor(self.conn) self.pfamily.set_table('profile_family') self.trait_menu = make_menu(['drop', 'order'], self.trait_command) self.pdata_menu = make_menu(['edit', 'drop'], self.variable_command) self.family_menu = make_menu(['drop'], self.family_command) self.reset_rows() self.append_page(ScrollCList(rcmenu=self.trait_menu), 'traits') self.append_page(ScrollCList(rcmenu=self.pdata_menu), 'variables') self.append_page(ScrollCList(rcmenu=self.family_menu), 'families') self.dialogs = {}.fromkeys(['order']) def __make_mainmenu_(self, suites): suite_commands = ['change to %s' %suite for suite in suites] profile_commands = ['drop', 'set defaults', 'append defaults'] commands = suite_commands + profile_commands return make_menu(commands, self.profile_command) def reset_rows(self): self.set_rows(self.profiles.select(fields=['profile', 'suite'], order='profile')) self.set_row_select(self.profile_selected) def profile_selected(self, listbox, row, column, event): row = listbox.get_selected_data()[0] self.current = row self.select_profile(self.current.profile) def select_profile(self, profile): self.variables = ProfileEnvironment(self.conn, profile) self.profiletrait.set_profile(profile) self.__set_pages(profile) def __set_pages(self, profile): pages = dict(self.pages) #set traits for profile pages['traits'].set_rows(self.profiletrait.trait_rows()) pages['traits'].set_select_mode('multi') clause = Eq('profile', self.current.profile) cursor = self.variables.env.cursor pages['variables'].set_rows(cursor.select(clause=clause), ['trait']) pages['variables'].set_select_mode('multi') pfrows = self.pfamily.select(fields=['family'], clause=clause) pages['families'].set_rows(pfrows) pages['families'].set_select_mode('multi') self.__set_droptargets__(pages) def __set_droptargets__(self, pages): set_receive_targets(pages['traits'].listbox, self.drop_trait, TARGETS.get('trait', self.current.suite)) set_receive_targets(pages['families'].listbox, self.drop_families, FTargets.get('family', 'flavor')) def trait_command(self, menuitem, action): traits = self._get_listbox('traits', 'trait') if action == 'drop': clause = In('trait', traits) & Eq('profile', self.current.profile) self.profiletrait.cmd.delete(clause=clause) self.__set_pages(self.current.profile) elif action == 'order': if not self.dialogs['order']: self.dialogs['order'] = dialogs.Entry('enter order', name='order') self.dialogs['order'].set_ok(self.set_order) self.dialogs['order'].set_cancel(self.destroy_dialog) def variable_command(self, menuitem, action): rows = self.pages['variables'].get_selected_data() cursor = self.variables.env.cursor if action == 'drop': for row in rows: clause = Eq('profile', self.current.profile) & Eq('trait', row.trait) clause &= Eq('name', row.name) cursor.delete(clause=clause) elif action == 'edit': self.edit_profilevars() def family_command(self, menuitem, action): print action families = [x[0] for x in self.pages['families'].get_selected_data()] if action == 'drop': clause = Eq('profile', self.current.profile) & In('family', families) self.pfamily.delete(clause=clause) def edit_profilevars(self): config = ProfileVariablesConfig(self.conn, self.current.profile) newconfig = config.edit() config.update(newconfig) self.select_profile(self.current.profile) def set_order(self, button): dialog = self.dialogs['order'] ord = dialog.get() rows = self.pages['traits'].get_selected_data() pclause = Eq('profile', self.current.profile) for row in rows: clause = pclause & Eq('trait', row.trait) self.profiletrait.update(data=dict(ord=ord), clause=clause) def drop_trait(self, listbox, context, x, y, selection, targettype, time): traits = keysplitter(selection) self.profiletrait.insert_traits(traits) self.__set_pages(self.current.profile) def drop_families(self, listbox, context, x, y, selection, targettype, time): families = keysplitter(selection) clause = Eq('profile', self.current.profile) data = dict(profile=self.current.profile) current = [x.family for x in self.pfamily.select(clause=clause)] for f in families: if f not in current: data['family'] = f self.pfamily.insert(data=data) self.__set_pages(self.current.profile) def _get_listbox(self, page, field): pages = dict(self.pages) return [row[field] for row in pages[page].listbox.get_selected_data()] def profile_command(self, menu, command): if command[:10] == 'change to ': self.change_suite(command[10:]) elif command == 'drop': self.profiletrait.drop_profile(self.current.profile) self.profiles.drop_profile(self.current.profile) self.current = None self.reset_rows() elif command == 'set defaults': self.variables.set_defaults() elif command == 'append defaults': self.variables.append_defaults() else: raise Error, 'bad command %s' %command def change_suite(self, suite): clause = Eq('profile', self.current.profile) self.profiles.update(data={'suite' : suite}, clause=clause) print 'changing suite to ', suite self.current_suite = suite self.reset_rows()
class RelationalBrowser(ListNoteBook, HasDialogs): def __init__(self, conn, maintable, reltable, pkey, fields): self.menu = make_menu(["insert", "update", "done"], self.pkey_command) ListNoteBook.__init__(self) self.conn = conn self.main = StatementCursor(self.conn) self.main.set_table(maintable) self.rel = StatementCursor(self.conn) self.rel.set_table(reltable) self.pkey = pkey self._fields = fields self.fields = [self.pkey] + self._fields self.reset_rows() self.dialogs = {}.fromkeys(["insert", "update", "delete"]) self.relmenu = make_menu(["insert", "update", "delete"], self.relmenu_command) def reset_rows(self): self.set_rows(self.main.select(order=self.pkey)) self.set_row_select(self.pkey_selected) def pkey_selected(self, listbox, row, column, event): value = listbox.get_selected_data()[0][0] self.current_value = value if value not in self.pages: rows = self.rel.select(clause=Eq(self.pkey, value), order=self.pkey) rtable = ScrollCList(rcmenu=self.relmenu) rtable.set_rows(rows) self.append_page(rtable, value) self.set_current_page(value) def pkey_insert_ok(self, *args): dialog = self.dialogs["insert"] value = dialog.get() print value try: self.main.insert(data={self.pkey: value}) inserted = True except OperationalError: dialogs.Message("bad query\n%s is not allowed" % value) if inserted: self.destroy_dialog(dialog) self.reset_rows() def relmenu_command(self, menuitem, command): print command if command in ["insert", "update", "delete"]: if self.dialogs[command] is None: clause = Eq(self.pkey, self.current_value) try: row = self.rel.select(clause=clause)[0] except IndexError: row = dict.fromkeys(self.fields) row[self.pkey] = self.current_value if command in ["insert", "delete"]: fields = self.fields clause = None else: fields = self._fields clause = Eq(self.pkey, self.current_value) make_record_dialog( self, command, row, self.relmenu_command_selected, self.pkey, fields, {command: command} ) print self.dialogs.items() def relmenu_command_selected(self, button, *data): command = button.get_name() dialog = self.dialogs[command] items = dialog.items() if command == "insert": self.rel.insert(data=dict(items)) elif command == "delete": print "delete delete delete delete" clause = SimpleClause(items, "=", "and") dialogs.Message(self.rel.stmt.delete(clause=clause)) if False: dialog = self.dialogs["insert"] print dialog items = dialog.items() print items self.rel.insert(data=dict(items)) self.destroy_dialog(dialog) self.reset_rows()
def add_new_mount(conn, name, mtpt, fstype, opts, dump="0", pass_="0"): cursor = StatementCursor(conn) data = dict(mnt_name=name, mnt_point=mtpt, fstype=fstype, mnt_opts=opts, dump=dump) data["pass"] = pass_ cursor.insert(table="mounts", data=data)
class RelationalBrowser(ListNoteBook, HasDialogs): def __init__(self, conn, maintable, reltable, pkey, fields): self.menu = make_menu(['insert', 'update', 'done'], self.pkey_command) ListNoteBook.__init__(self) self.conn = conn self.main = StatementCursor(self.conn) self.main.set_table(maintable) self.rel = StatementCursor(self.conn) self.rel.set_table(reltable) self.pkey = pkey self._fields = fields self.fields = [self.pkey] + self._fields self.reset_rows() self.dialogs = {}.fromkeys(['insert', 'update', 'delete']) self.relmenu = make_menu(['insert', 'update', 'delete'], self.relmenu_command) def reset_rows(self): self.set_rows(self.main.select(order=self.pkey)) self.set_row_select(self.pkey_selected) def pkey_selected(self, listbox, row, column, event): value = listbox.get_selected_data()[0][0] self.current_value = value if value not in self.pages: rows = self.rel.select(clause=Eq(self.pkey, value), order=self.pkey) rtable = ScrollCList(rcmenu=self.relmenu) rtable.set_rows(rows) self.append_page(rtable, value) self.set_current_page(value) def pkey_command(self, menuitem, command): if command == 'insert': if self.dialogs['insert'] is None: dialog = dialogs.Entry('insert a %s' % self.pkey, name='insert') dialog.set_ok(self.pkey_insert_ok) dialog.set_cancel(self.destroy_dialog) self.dialogs['insert'] = dialog elif command == 'update': dialogs.Message('need to set update to cascade in db') elif command == 'done': value = None try: value = self.listbox.get_selected_data()[0][0] except IndexError: dialogs.Message('need to select %s first' % self.pkey) if value is not None: self.remove_page(value) def pkey_insert_ok(self, *args): dialog = self.dialogs['insert'] value = dialog.get() print value try: self.main.insert(data={self.pkey : value}) inserted = True except OperationalError: dialogs.Message('bad query\n%s is not allowed' % value) if inserted: self.destroy_dialog(dialog) self.reset_rows() def relmenu_command(self, menuitem, command): print command if command in ['insert', 'update', 'delete']: if self.dialogs[command] is None: clause = Eq(self.pkey, self.current_value) try: row = self.rel.select(clause=clause)[0] except IndexError: row = dict.fromkeys(self.fields) row[self.pkey] = self.current_value if command in ['insert', 'delete']: fields = self.fields clause = None else: fields = self._fields clause = Eq(self.pkey, self.current_value) make_record_dialog(self, command, row, self.relmenu_command_selected, self.pkey, fields, {command:command}) print self.dialogs.items() def relmenu_command_selected(self, button, *data): command = button.get_name() dialog = self.dialogs[command] items = dialog.items() if command == 'insert': self.rel.insert(data=dict(items)) elif command == 'delete': print 'delete delete delete delete' clause = SimpleClause(items, '=', 'and') dialogs.Message(self.rel.stmt.delete(clause=clause)) if False: dialog = self.dialogs['insert'] print dialog items = dialog.items() print items self.rel.insert(data=dict(items)) self.destroy_dialog(dialog) self.reset_rows()
class DiskManager(object): def __init__(self, conn): self.conn = conn self.parser = PartitionParser() self.cursor = StatementCursor(self.conn) def _quick_partition(self, device, data): i, o = os.popen2('sfdisk %s' % device) i.write(data) i.close() def get_partition_info(self, device, parser=None): if parser is None: parser = self.parser command = 'bash -c "sfdisk -d %s | grep %s"' % (device, device) part_info = commands.getoutput(command) return self._parse_diskconfig(device, part_info) def _parse_diskconfig(self, device, astring): parsed = self.parser.parseString(astring) partitions = [] for p in parsed: pnum = p[0].split(device)[1] pdict = dict(partition=pnum, start=p[1], size=p[2], Id=p[3]) partitions.append(pdict) return partitions def _submit_diskconfig(self, diskname, device, astring): workspace = 'partition_workspace' self.cursor.delete(table=workspace, clause=(Eq('diskname', diskname))) row = dict(diskname=diskname) for partition in self._parse_diskconfig(device, astring): print 'submitting', partition row.update(partition) self.cursor.insert(table=workspace, data=row) def submit_partitions(self, diskname, device): self.cursor.set_table('partition_workspace') self.cursor.delete(clause=(Eq('diskname', diskname))) row = dict(diskname=diskname) print 'submitting', device for partition in self.get_partition_info(device): print 'submitting', partition row.update(partition) self.cursor.insert(data=row) def approve_disk(self, diskname): clause = Eq('diskname', diskname) workspace = 'partition_workspace' sql = Statement('select') sql.table = workspace sql.clause = clause new_rows = sql.select(order='partition') if diskname not in [ r.diskname for r in self.cursor.select(table='disks') ]: self.cursor.insert(table='disks', data=dict(diskname=diskname)) else: self.cursor.delete(table='partitions', clause=clause) self.cursor.execute('insert into partitions %s' % new_rows) def get_partitions_by_name(self, diskname): return self.cursor.select(table='partitions', clause=Eq('diskname', diskname), order='partition') def make_partition_dump(self, device, partitions): output = '# partition table of %s\n' output += 'unit: sectors\n' for p in partitions: line = '%s%s : start= %8d, size= %8d, Id=%2d' % \ (device, p.partition, p.start, p.size, p.id) output += line + '\n' return output def partition_disk(self, diskname, device): partitions = self.get_partitions_by_name(diskname) data = self.make_partition_dump(device, partitions) self._quick_partition(device, data) def clear_partition_table(self, device): command = 'dd if=/dev/zero of=%s count=1 bs=512' % device os.system(command)
def add_disk_to_machine_type(conn, diskname, mtype, device='/dev/hda'): cursor = StatementCursor(conn) data = dict(machine_type=mtype, diskname=diskname, device=device) cursor.insert(table='machine_disks', data=data)
class ProfileGenWin(CommandBoxWindow): def __init__(self, conn, name='ProfileGenWin'): actions = ['create', 'copy', 'export', 'import'] CommandBoxWindow.__init__(self, name=name) self.set_title(name) self.conn = conn self.cmd = StatementCursor(conn, name) self.suites = [x.suite for x in self.cmd.select(table='suites')] self.profiles = StatementCursor(conn, 'profiles') self.profiles.set_table('profiles') self.menu_bar = SimpleMenuBar() self.vbox.pack_start(self.menu_bar, 0, 0, 0) self.dialogs = {}.fromkeys(actions) self.add_menu(actions, 'main', self.ask_dialog) self.add_menu(self.suites, 'traits', self.show_traits) self.add_menu(self.suites, 'traitgen', self.show_traitgen) self.browser = ProfileBrowser(self.conn, self.suites) self.vbox.add(self.browser) self.set_size_request(400, 300) def ask_dialog(self, button, data): if not self.dialogs[data]: if data == 'create': self.dialogs[data] = dialogs.Entry('create profile', name='create') self.dialogs[data].set_ok(self.create_profile) elif data == 'copy': self.dialogs[data] = dialogs.CList('copy profile', name='copy') dialog = self.dialogs[data] dialog.set_rows(self.profiles.select(fields='profile', order='profile')) dialog.set_ok(self.src_profile_selected) elif data == 'export': pdb = PaellaDatabase(self.conn) profiles = pdb.profiles self.dialogs[data].set_cancel(self.destroy_dialog) def destroy_dialog(self, dying): name = dying.get_name() self.dialogs[name] = None dying.destroy() def create_profile(self, button): name = button.get_name() debug(name) if name == 'create': profile = self.dialogs[name].get() if profile not in [p.profile for p in self.profiles.select()]: self.profiles.insert(data={'profile' : profile, 'suite' : 'woody'}) self.destroy_dialog(self.dialogs[name]) self.browser.reset_rows() else: dialogs.Dialog('there was a problem') def src_profile_selected(self, button): row = get_single_row(self.dialogs['copy'], 'profile') def show_packages(self, button, data): PackagesWindow(self.conn, data) def show_traits(self, button, data): TraitsWindow(self.conn, data) def show_traitgen(self, button, data): TraitGenWin(self.conn, data)
def add_new_filesystem(conn, filesystem): cursor = StatementCursor(conn) data = dict(filesystem=filesystem) cursor.insert(table='filesystems', data=data)
class RelationalBrowser(ListNoteBook, HasDialogs): def __init__(self, conn, maintable, reltable, pkey, fields): self.menu = make_menu(['insert', 'update', 'done'], self.pkey_command) ListNoteBook.__init__(self) self.conn = conn self.main = StatementCursor(self.conn) self.main.set_table(maintable) self.rel = StatementCursor(self.conn) self.rel.set_table(reltable) self.pkey = pkey self._fields = fields self.fields = [self.pkey] + self._fields self.reset_rows() self.dialogs = {}.fromkeys(['insert', 'update', 'delete']) self.relmenu = make_menu(['insert', 'update', 'delete'], self.relmenu_command) def reset_rows(self): self.set_rows(self.main.select(order=self.pkey)) self.set_row_select(self.pkey_selected) def pkey_selected(self, listbox, row, column, event): value = listbox.get_selected_data()[0][0] self.current_value = value if value not in self.pages: rows = self.rel.select(clause=Eq(self.pkey, value), order=self.pkey) rtable = ScrollCList(rcmenu=self.relmenu) rtable.set_rows(rows) self.append_page(rtable, value) self.set_current_page(value) def pkey_insert_ok(self, *args): dialog = self.dialogs['insert'] value = dialog.get() print value try: self.main.insert(data={self.pkey: value}) inserted = True except OperationalError: dialogs.Message('bad query\n%s is not allowed' % value) if inserted: self.destroy_dialog(dialog) self.reset_rows() def relmenu_command(self, menuitem, command): print command if command in ['insert', 'update', 'delete']: if self.dialogs[command] is None: clause = Eq(self.pkey, self.current_value) try: row = self.rel.select(clause=clause)[0] except IndexError: row = dict.fromkeys(self.fields) row[self.pkey] = self.current_value if command in ['insert', 'delete']: fields = self.fields clause = None else: fields = self._fields clause = Eq(self.pkey, self.current_value) make_record_dialog(self, command, row, self.relmenu_command_selected, self.pkey, fields, {command: command}) print self.dialogs.items() def relmenu_command_selected(self, button, *data): command = button.get_name() dialog = self.dialogs[command] items = dialog.items() if command == 'insert': self.rel.insert(data=dict(items)) elif command == 'delete': print 'delete delete delete delete' clause = SimpleClause(items, '=', 'and') dialogs.Message(self.rel.stmt.delete(clause=clause)) if False: dialog = self.dialogs['insert'] print dialog items = dialog.items() print items self.rel.insert(data=dict(items)) self.destroy_dialog(dialog) self.reset_rows()
class DiskManager(object): def __init__(self, conn): self.conn = conn self.parser = PartitionParser() self.cursor = StatementCursor(self.conn) def _quick_partition(self, device, data): i, o = os.popen2('sfdisk %s' % device) i.write(data) i.close() def get_partition_info(self, device, parser=None): if parser is None: parser = self.parser command = 'bash -c "sfdisk -d %s | grep %s"' % (device, device) part_info = commands.getoutput(command) return self._parse_diskconfig(device, part_info) def _parse_diskconfig(self, device, astring): parsed = self.parser.parseString(astring) partitions = [] for p in parsed: pnum = p[0].split(device)[1] pdict = dict(partition=pnum, start=p[1], size=p[2], Id=p[3]) partitions.append(pdict) return partitions def _submit_diskconfig(self, diskname, device, astring): workspace = 'partition_workspace' self.cursor.delete(table=workspace, clause=(Eq('diskname', diskname))) row = dict(diskname=diskname) for partition in self._parse_diskconfig(device, astring): print 'submitting', partition row.update(partition) self.cursor.insert(table=workspace, data=row) def submit_partitions(self, diskname, device): self.cursor.set_table('partition_workspace') self.cursor.delete(clause=(Eq('diskname', diskname))) row = dict(diskname=diskname) print 'submitting', device for partition in self.get_partition_info(device): print 'submitting', partition row.update(partition) self.cursor.insert(data=row) def approve_disk(self, diskname): clause = Eq('diskname', diskname) workspace = 'partition_workspace' sql = Statement('select') sql.table = workspace sql.clause = clause new_rows = sql.select(order='partition') if diskname not in [r.diskname for r in self.cursor.select(table='disks')]: self.cursor.insert(table='disks', data=dict(diskname=diskname)) else: self.cursor.delete(table='partitions', clause=clause) self.cursor.execute('insert into partitions %s' % new_rows) def get_partitions_by_name(self, diskname): return self.cursor.select(table='partitions', clause=Eq('diskname', diskname), order='partition') def make_partition_dump(self, device, partitions): output = '# partition table of %s\n' output += 'unit: sectors\n' for p in partitions: line = '%s%s : start= %8d, size= %8d, Id=%2d' % \ (device, p.partition, p.start, p.size, p.id) output += line + '\n' return output def partition_disk(self, diskname, device): partitions = self.get_partitions_by_name(diskname) data = self.make_partition_dump(device, partitions) self._quick_partition(device, data) def clear_partition_table(self, device): command = 'dd if=/dev/zero of=%s count=1 bs=512' % device os.system(command)
class PaellaProfiles(Element): def __init__(self, conn): Element.__init__(self, 'profiles') self.conn = conn self.stmt = StatementCursor(self.conn) self.env = ProfileEnvironment(self.conn) self.profiletraits = ProfileTrait(self.conn) self._profiles = {} self._profile = Profile(self.conn) for row in self.stmt.select(table='profiles', order='profile'): self._append_profile(row.profile, row.suite) def _append_profile(self, profile, suite): element = self.export_profile(profile, suite) self._profiles[profile] = element self.appendChild(self._profiles[profile]) def export_profile(self, profile, suite=None): if suite is None: row = self.stmt.select_row(table='profiles', clause=Eq('profile', profile)) suite = row['suite'] suite = str(suite) profile = str(profile) self.env.set_profile(profile) element = ProfileElement(profile, suite) element.append_traits(self.profiletraits.trait_rows(profile)) element.append_families(self._profile.family_rows(profile)) element.append_variables(self.env.get_rows()) return element def insert_profile(self, profile): idata = {'profile': profile.name, 'suite': profile.suite} self.stmt.insert(table='profiles', data=idata) idata = {'profile': profile.name, 'trait': None, 'ord': 0} for trait, ord in profile.traits: print trait, ord idata['trait'] = trait idata['ord'] = ord #str(ord) self.stmt.insert(table='profile_trait', data=idata) idata = { 'profile': profile.name, 'trait': None, 'name': None, 'value': None } idata = dict(profile=profile.name) for family in profile.families: idata['family'] = family self.stmt.insert(table='profile_family', data=idata) idata = dict(profile=profile.name) for trait, name, value in profile.vars: idata['trait'] = trait idata['name'] = name idata['value'] = value self.stmt.insert(table='profile_variables', data=idata) def export_profiles(self, path): rows = self.stmt.select(fields='profile', table='profiles', clause=None) for row in rows: self.write_profile(row.profile, path) def write_profile(self, profile, path): xmlfile = file(join(path, '%s.xml' % profile), 'w') data = self.export_profile(profile) data.writexml(xmlfile, indent='\t', newl='\n', addindent='\t') xmlfile.close()
class ColorThingyWindow(CommandBoxWindow): def __init__(self, rgb, conn, name='ColorThingyWindow'): CommandBoxWindow.__init__(self, name=name) self.browser = ColorThingy(rgb) self.rgb = rgb self.cmd = StatementCursor(conn, 'themer') self.theme = None self.vbox.add(self.browser) self.tbar.add_button('import', 'import', self.__ask_import__) self.tbar.add_button('insert', 'insert', self.__ask_insert__) self.tbar.add_button('update', 'update', self.__update_theme__) self.tbar.add_button('save', 'save', self.__save_files__) self._insert_box = None self._import_box = None def __ask_import__(self, *args): if not self._import_box: #print args #print '__import__' themes = self.cmd.getall(['theme'], 'themes') self._import_box = CList('hello') self._import_box.set_rows(themes) self._import_box.set_ok(self.__import_ok__) def __import_ok__(self, *args): theme = self._import_box.listbox.get_selected_data()[0]['theme'] self._import_box.destroy() self._import_box = None rows = self.cmd.select(table=theme) b = self.browser b._style.quick_set(rows) for e, sb in b._elements.items(): for s in sb.keys(): color = b._style.elements[e][s] sb[s] = color.name _bg_widget(sb[s].button, color) self.theme = theme def __ask_insert__(self, *args): if not self._import_box: self._insert_box = EntryDialog('please insert name') self._insert_box.set_ok(self.__insert_ok__) def __insert_ok__(self, *args): style = self.browser._style tname = self._insert_box.get() self.theme = tname table = ThemeTable(tname) self.cmd.create_table(table) self.cmd.insert('themes', {'theme':tname}) for row in style.to_irows(): self.cmd.insert(tname, row) def __update_theme__(self, *args): if self.theme: for element, states in self.browser._style.to_urows(): self.cmd.update(self.theme, states, "element = '%s'" %element) def __save_files__(self, *args): colordict = Rgbdb() tmpl = GtkrcTemplate(self.theme, theme_base, self.browser._style) tmpl.write_files()
class ColorThingyWindow(CommandBoxWindow): def __init__(self, rgb, conn, name='ColorThingyWindow'): CommandBoxWindow.__init__(self, name=name) self.browser = ColorThingy(rgb) self.rgb = rgb self.cmd = StatementCursor(conn, 'themer') self.theme = None self.vbox.add(self.browser) self.tbar.add_button('import', 'import', self.__ask_import__) self.tbar.add_button('insert', 'insert', self.__ask_insert__) self.tbar.add_button('update', 'update', self.__update_theme__) self.tbar.add_button('save', 'save', self.__save_files__) self._insert_box = None self._import_box = None def __ask_import__(self, *args): if not self._import_box: #print args #print '__import__' themes = self.cmd.getall(['theme'], 'themes') self._import_box = CList('hello') self._import_box.set_rows(themes) self._import_box.set_ok(self.__import_ok__) def __import_ok__(self, *args): theme = self._import_box.listbox.get_selected_data()[0]['theme'] self._import_box.destroy() self._import_box = None rows = self.cmd.select(table=theme) b = self.browser b._style.quick_set(rows) for e, sb in b._elements.items(): for s in sb.keys(): color = b._style.elements[e][s] sb[s] = color.name _bg_widget(sb[s].button, color) self.theme = theme def __ask_insert__(self, *args): if not self._import_box: self._insert_box = EntryDialog('please insert name') self._insert_box.set_ok(self.__insert_ok__) def __insert_ok__(self, *args): style = self.browser._style tname = self._insert_box.get() self.theme = tname table = ThemeTable(tname) self.cmd.create_table(table) self.cmd.insert('themes', {'theme': tname}) for row in style.to_irows(): self.cmd.insert(tname, row) def __update_theme__(self, *args): if self.theme: for element, states in self.browser._style.to_urows(): self.cmd.update(self.theme, states, "element = '%s'" % element) def __save_files__(self, *args): colordict = Rgbdb() tmpl = GtkrcTemplate(self.theme, theme_base, self.browser._style) tmpl.write_files()
def add_new_kernel(conn, kernel): cursor = StatementCursor(conn) data = dict(kernel=kernel) cursor.insert(table='kernels', data=data)
class VariablesConfig(RawConfigParser): def __init__(self, conn, table, section, mainfield=None, mainvalue=None, option='name', value='value'): self.conn = conn self.cursor = StatementCursor(self.conn) self.cursor.set_table(table) self._secfield = section bothnone = mainfield is None and mainvalue is None bothset = mainfield and mainvalue if not bothnone and not bothset: raise Error, 'both mainfield and mainvalue need to be set/unset' self._mainclause = None if bothset: self._mainclause = Eq(mainfield, mainvalue) self._fields = [self._secfield, option, value] RawConfigParser.__init__(self) for row in self.cursor.select(fields=self._fields, clause=self._mainclause): if row[0] not in self.sections(): self.add_section(row[0]) self.set(*row) def write(self, cfile): sections = self.sections() sections.sort() for section in sections: cfile.write('[%s]\n' % section) keys = self.options(section) keys.sort() for k in keys: if k != '__name__': v = str(self.get(section, k)).replace('\n', '\n\t') cfile.write('%s:\t%s\n' % (k, v)) cfile.write('\n') def edit(self): tmp, path = tempfile.mkstemp('variables', 'config') tmp = file(path, 'w') self.write(tmp) tmp.close() os.system('$EDITOR %s' % path) tmp = file(path, 'r') newconfig = RawConfigParser() newconfig.readfp(tmp) tmp.close() os.remove(path) return newconfig def diff(self, other): ltmp, lpath = tempfile.mkstemp('variables', 'config') ltmp = file(lpath, 'w') self.write(ltmp) ltmp.close() rtmp, rpath = tempfile.mkstemp('variables', 'config') rtmp = file(rpath, 'w') other.write(rtmp) rtmp.close() os.system('xxdiff %s %s' % (lpath, rpath)) ltmp, rtmp = file(lpath, 'r'), file(rpath, 'r') lcfg, rcfg = RawConfigParser(), RawConfigParser() lcfg.readfp(ltmp) rcfg.readfp(rtmp) ltmp.close() rtmp.close() self.update(lcfg) other.update(rcfg) def update(self, newconfig): removed = [x for x in self.sections() if x not in newconfig.sections()] for section in removed: print 'removing', section sclause = Eq(self._secfield, section) if self._mainclause: sclause &= self._mainclause self.cursor.delete(clause=sclause) for section in newconfig.sections(): print section sclause = Eq(self._secfield, section) if self._mainclause: sclause = self._mainclause & Eq(self._secfield, section) if not self.has_section(section): for k,v in newconfig.items(section): idata = dict(zip(self._fields, [section, k, v])) if self._mainclause: idata[self._mainclause.left] = self._mainclause.right print idata self.cursor.insert(data=idata) else: for name, value in newconfig.items(section): nclause = sclause & Eq(self._fields[1], name) #print 'nclause', nclause #print 'value', self.get(section, name) if self.has_option(section, name): if value != self.get(section, name): #print 'updating' self.cursor.update(data={self._fields[2] : value}, clause=nclause) else: idata = dict(zip(self._fields, [section, name, value])) if self._mainclause: idata[self._mainclause.left] = self._mainclause.right self.cursor.insert(data=idata) if self.has_section(section): for name, value in self.items(section): if not newconfig.has_option(section, name): clause = sclause & Eq(self._fields[1], name) #print 'dclause', clause self.cursor.delete(clause=clause)
class RepositoryManager(object): def __init__(self, conn): self.conn = conn self.main = StatementCursor(self.conn, 'RepositoryMain') self.repos = StatementCursor(self.conn, 'Repository') self.repos.set_table('repository') self.sources = StatementCursor(self.conn, 'Sources') self.sources.set_table('sources') self.release = ReleaseCursor(self.conn) self.repsections = StatementCursor(self.conn, 'repos_section') self.repsections.set_table('repos_section') self.__init_db__() def __init_db__(self): if not len(self.main.tables()): map(self.main.create_table, primary_tables()) def drop_source(self, name, type): nameclause = Eq('name', name) clause = nameclause & Eq('type', type) for section in self.get_sections(name): self.main.drop(list_tablename(name, type, section)) self.sources.delete(clause=clause) sources = self.sources.select(clause=clause) if not len(sources): self.repsections.delete(clause=nameclause) self.repos.delete(clause=nameclause) def add_source(self, name, source): source = make_source(source) if name not in [x.name for x in self.repos.select()]: self.repos.insert(data=dict(name=name)) clause = Eq('name', name) & Eq('type', source.type) count = int( self.sources.select(fields=['count(name)'], clause=clause)[0][0]) if count == 0: if islocaluri(source.uri): data = dict(name=name, type=source.type, uri=source.uri, suite=source.suite) self.sources.insert(data=data) current_sections = self.get_sections(name) sdata = dict(name=name, section=None) for section in source.sections: if section not in current_sections: sdata['section'] = section self.repsections.insert(data=sdata) fullparse = FullParseTable(name, source.type, section) if fullparse.name not in self.main.tables(): self.main.create_table(fullparse) listtable = ListTable(name, source.type, section) if listtable.name not in self.main.tables(): self.main.create_table(listtable) else: raise Error, 'local uris first' else: if not islocaluri(source.uri): data = dict(remote=source.uri) self.sources.update(data=data, clause=clause) else: raise ExistsError, 'already there' def get_sections(self, name): clause = Eq('name', name) return [ x.section for x in self.repsections.iselect(fields=['section'], clause=clause) ] def make_source_line(self, name, type, remote=False): repsrc = self.make_source(name, type, remote) return str(repsrc) def make_source(self, name, type, remote=False): clause = Eq('name', name) & Eq('type', type) source = self.sources.select_row(clause=clause) repsrc = RepositorySource() repsrc.type = source.type if remote: repsrc.uri = source.remote else: repsrc.uri = source.uri repsrc.suite = source.suite repsrc.sections = self.get_sections(name) repsrc.set_path() return repsrc def listfile(self, name, type, section=None): source = self.make_source(name, type) release = Release(source) print 'Need to pull from database!!!' if source.has_release(): return join(source.distpath, release.path(section)) else: return join(source.distpath, source.listfile()) def get_remote(self, name, type, remote=True): return self.make_source(name, type, remote=True) def parse_section(self, name, type, section=None): listfile = self.listfile(name, type, section) debug(listfile) if not isfile(listfile): raise NoFileError, 'file not there' if type == 'deb': return full_parse(listfile) elif type == 'deb-src': return parse_sources(listfile) else: raise Error, 'bad source type' def add_repository(self, name): self.repos.insert(data=dict(name=name))
def add_machine_type(conn, mtype): cursor = StatementCursor(conn) data = dict(machine_type=mtype) cursor.insert(table='machine_types', data=data)
class Profile(StatementCursor): def __init__(self, conn): StatementCursor.__init__(self, conn) self.conn = conn self.set_table('profiles') self._traits = ProfileTrait(conn) self._env = ProfileEnvironment(conn) self._pfam = StatementCursor(conn) self._pfam.set_table('profile_family') self._fam = Family(conn) def drop_profile(self, profile): self.delete(clause=Eq('profile', profile)) def set_profile(self, profile): self.clause = Eq('profile', profile) self.current = self.select_row(clause=self.clause) self._traits.set_profile(profile) self._env.set_profile(profile) def get_profile_data(self): return self._env.ProfileData() def get_family_data(self): families = self.get_families() return self._fam.FamilyData(families) def make_traitlist(self, log=None): tp = TraitParent(self.conn, self.current.suite) listed = [x.trait for x in self._traits.trait_rows()] all = list(tp.get_traitset(listed)) setfun = tp.set_trait parfun = tp.parents return make_deplist(listed, all, setfun, parfun, log) def family_rows(self, profile=None): if profile is None: profile = self.current.profile return self._pfam.select(clause=Eq('profile', profile), order='family') def get_families(self, profile=None): return [r.family for r in self.family_rows(profile)] def get_trait_rows(self): return self._traits.trait_rows() def append_family(self, family): if family not in self.get_families(): self._pfam.insert(data=dict(profile=self.current.profile, family=family)) def append_trait(self, trait, ord): self._traits.insert_trait(trait, ord) def set_suite(self, suite): clause = Eq('profile', self.current.profile) self.update(data=dict(suite=suite), clause=clause) self.set_profile(self.current.profile) def copy_profile(self, src, dest): current = self.current self.set_profile(src) pfield = "'%s' as profile" % dest pclause = Eq('profile', src) pcursor = self tcursor = self._traits.cmd fcusor = self._pfam vcursor = self._env.env.cursor cursors = [ pcursor, tcursor, fcusor, vcursor] for cursor in cursors: sel = str(cursor.stmt.select(fields = [pfield] + cursor.fields()[1:], clause=pclause)) cursor.execute('insert into %s (%s)' % (cursor.stmt.table, sel)) self.set_profile(current.profile)