Esempio n. 1
0
 def addDbSchemaok(self):
     dlg = self.dialogs['new-schema']
     schema = str(dlg.grid.entries['schema'].text())
     self.manager.create_schema(schema)
     cursor = StatementCursor(self.db.conn)
     cursor.execute('set SESSION search_path to %s' % schema)
     self.db.conn.commit()
     cursor.execute('show search_path')
     print cursor.fetchall()
     kschema.create_schema(cursor)
     self.refreshlistView()
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 def remove_client(self, client):
     profiles, families, traits = self._client_schema(client)
     machines = self._client_mdata(client)
     cursor = StatementCursor(self.conn)
     for machine in machines:
         cursor.execute("select * from delete_machine('%s')" % machine)
     for profile in profiles:
         cursor.execute("select * from delete_profile('%s')" % profile)
     for family in families:
         cursor.execute("select * from delete_family('%s')" % family)
def create_database(cfg, default_traits):
    dsn = cfg.get_dsn()
    dsn['dbname'] = 'mishmash'
    conn = QuickConn(dsn)
    cmd = StatementCursor(conn, 'create_database')
    for table in cmd.tables():
        cmd.execute('drop table %s' %table)
    start_schema(conn, default_traits)
    make_suites(conn)
    cmd.execute(grant_public(cmd.tables()))
    cmd.execute(grant_public(['current_environment'], 'ALL'))
Esempio n. 6
0
def create_database(cfg, default_traits):
    dsn = cfg.get_dsn()
    dsn['dbname'] = 'mishmash'
    conn = QuickConn(dsn)
    cmd = StatementCursor(conn, 'create_database')
    for table in cmd.tables():
        cmd.execute('drop table %s' % table)
    start_schema(conn, default_traits)
    make_suites(conn)
    cmd.execute(grant_public(cmd.tables()))
    cmd.execute(grant_public(['current_environment'], 'ALL'))
Esempio n. 7
0
class Family(object):
    def __init__(self, conn):
        object.__init__(self)
        self.conn = conn
        #self.suites = Suites(conn).list()
        self.cursor = StatementCursor(self.conn)
        self.suites = [r.suite for r in self.cursor.select(table='suites')]
        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 _check_family(self, family):
        if family is not None:
            return family
        else:
            family = self.current
        if family is None:
            raise Error, 'either pass a family arguement or call set_family on this object'
        return 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):
        family = self._check_family(family)
        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):
        family = self._check_family(family)
        rows = self.parent_rows(family)
        return [x.parent for x in rows]
    
    def environment_rows(self, family=None):
        family = self._check_family(family)
        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, family=None):
        family = self._check_family(family)
        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_family_xml(self, xmlfile):
        xml = parse_file(xmlfile)
        elements = xml.getElementsByTagName('family')
        if len(elements) != 1:
            raise Error, 'bad number of family tags %s' % len(elements)
        element = elements[0]
        return element
    
            
    def import_family_xml(self, xmlfile):
        element = self._import_family_xml(xmlfile)
        self.import_family(element)
        
    def import_families(self, path):
        xmlfiles = [join(path, x) for x in os.listdir(path) if x[-4:] == '.xml']
        families = []
        for f in xmlfiles:
            element = self._import_family_xml(f)
            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'

    def getVariablesConfig(self, family=None):
        family = self._check_family(family)
        return FamilyVariablesConfig(self.conn, family)
    
    def deleteVariable(self, trait, name, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        self.env.cursor.delete(clause=clause)

    def insertVariable(self, trait, name, value, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        testrows = self.cursor.select(table='family_environment', clause=clause)
        if not len(testrows):
            data['value'] = value
            self.cursor.insert(table='family_environment', data=data)
Esempio n. 8
0
class BaseDatabase(QSqlDatabase):
    def __init__(self, dsn, name, parent=None, objname=None):
        deprecated('useless.kdedb.BaseDatabase is deprecated')
        QSqlDatabase.__init__(self, 'QPSQL7', name, parent, objname)
        if hasattr(dsn, 'items'): #if dsn is dictionary
            self.conn = BasicConnection(**dsn)
            self.setDatabaseName(dsn['dbname'])
            self.setHostName(dsn['host'])
            self.dbuser = dsn['user']
        else: #else a conn was passed as dsn
            self.conn = dsn
            self.setDatabaseName(self.conn.conn.db)
            self.setHostName(self.conn.conn.host)
            self.dbuser = self.conn.conn.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 RuntimeError, '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)
Esempio n. 9
0
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)
Esempio n. 10
0
class BaseDatabase(object):
    def __init__(self, conn=None):
        if conn is None:
            self.conn = KonsultantConnection()
        else:
            self.conn = conn
        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 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)
Esempio n. 11
0
class SuiteHandler(object):
    def __init__(self, conn, cfg):
        self.conn = conn
        self.cfg = cfg
        self.cursor = StatementCursor(self.conn)
        self.http_mirror = 'http://%s' % self.cfg.get('debrepos', 'repos_host')
        self.local_mirror = 'file:/tmp/paellamirror'
        self.suite = None
        self.sources_rows = []

    def set_suite(self, suite):
        self.suite = suite
        self.sources_rows = self.get_sources_rows(self.suite)

    def make_suite(self):
        if self.suite is None:
            raise Error, 'the suite needs to be set first'
        self._make_suite_tables()
        self.update_packagelists()
        for row in self.sources_rows:
            self._insert_packages(row)
            
    def _make_suite_tables(self):
        tables = suite_tables(self.suite)
        map(self.cursor.create_table, tables)
        self.cursor.execute(grant_public([x.name for x in tables]))
        
    def update_packagelists(self):
        self._update_suite_packages(self.suite)
        
    def get_sources_rows(self, suite):
        table = 'apt_sources natural join suite_apt_sources'
        rows = self.cursor.select(table=table, clause=Eq('suite', suite), order=['ord'])
        return rows

    def _insert_some_packages(self, table, packages):
        for package in packages:
            try:
                self.cursor.insert(table, data=package_to_row(package))
            except OperationalError:
                pass
            
    def _insert_packages(self, src_row):
        table = '%s_packages' % self.suite
        repos = self._row2repos(src_row)
        prow = package_to_row
        local = repos.local
        if local.source.sections:
            repos.update()
            local.parse_release()
            for section in local.source.sections:
                packages = local.full_parse(section).values()
                self._insert_some_packages(table, packages)
        else:
            packages = local.full_parse().values()
            self._insert_some_packages(table, packages)
            
            
        
    def _row2repsource(self, row, http_mirror):
        lp = row.local_path
        while lp[0] == '/':
            lp = lp[1:]
        uri = os.path.join(http_mirror, lp)
        suite = row.dist
        src = 'deb %s %s' % (uri, suite)
        if not suite.endswith('/'):
            src = '%s %s' % (src, row.sections)
        r = RepositorySource(src)
        return r

    def _row2repos(self, row):
        rsource = self._row2repsource(row, self.http_mirror)
        lsource = self._row2repsource(row, self.local_mirror)
        return RemoteRepos(rsource, lsource)
                
    def _get_packages_file(self, repos):
        if repos.source.has_release():
            repos.update()
        else:
            rpath = os.path.join(repos.source.uri, repos.source.suite, 'Packages.gz')
            # the [5:] slice is to remove file: from local uri
            lpath = os.path.join(repos.local.source.uri, repos.source.suite, 'Packages.gz')[5:]
            if not os.path.isfile(lpath):
                print 'lpath is --->', lpath
                makepaths(os.path.dirname(lpath))
                print rpath, lpath, 'getting now'
                wget(rpath, lpath)
            
    def _update_suite_packages(self, suite):
        rows = self.get_sources_rows(suite)
        for row in rows:
            repos = self._row2repos(row)
            self._get_packages_file(repos)
Esempio n. 12
0
def start_schema(conn):
    cursor = StatementCursor(conn, 'start_schema')
    map(cursor.create_sequence, primary_sequences())
    tables, mapping = primary_tables()
    map(cursor.create_table, tables)
    priorities_table = mapping['priorities']
    insert_list(cursor, priorities_table.name, 'priority', PRIORITIES)
    insert_list(cursor, 'scriptnames', 'script', SCRIPTS)
    newscripts = [s for s in MTSCRIPTS if s not in SCRIPTS]
    insert_list(cursor, 'scriptnames', 'script', newscripts)
    cursor.execute(grant_public([x.name for x in tables]))
    cursor.execute(grant_public(['current_environment'], 'ALL'))
    cursor.execute(grant_public(['partition_workspace'], 'ALL'))
    cursor.execute(plpgsql_delete_trait)
    cursor.execute(pgsql_delete_profile())
    cursor.execute(pgsql_delete_family())
    cursor.execute(pgsql_delete_disk())
    cursor.execute(pgsql_delete_mtype())
    cursor.execute(pgsql_delete_filesystem())
Esempio n. 13
0
class SuiteHandler(object):
    def __init__(self, conn, cfg):
        self.conn = conn
        self.cfg = cfg
        self.cursor = StatementCursor(self.conn)
        self.http_mirror = 'http://%s' % self.cfg.get('debrepos', 'repos_host')
        self.local_mirror = 'file:/tmp/paellamirror'
        self.suite = None
        self.sources_rows = []

    def set_suite(self, suite):
        self.suite = suite
        self.sources_rows = self.get_sources_rows(self.suite)

    def make_suite(self):
        if self.suite is None:
            raise Error, 'the suite needs to be set first'
        self._make_suite_tables()
        self.update_packagelists()
        for row in self.sources_rows:
            self._insert_packages(row)

    def _make_suite_tables(self):
        tables = suite_tables(self.suite)
        map(self.cursor.create_table, tables)
        self.cursor.execute(grant_public([x.name for x in tables]))

    def update_packagelists(self):
        self._update_suite_packages(self.suite)

    def get_sources_rows(self, suite):
        table = 'apt_sources natural join suite_apt_sources'
        rows = self.cursor.select(table=table,
                                  clause=Eq('suite', suite),
                                  order=['ord'])
        return rows

    def _insert_some_packages(self, table, packages):
        for package in packages:
            try:
                self.cursor.insert(table, data=package_to_row(package))
            except OperationalError:
                pass

    def _insert_packages(self, src_row):
        table = '%s_packages' % self.suite
        repos = self._row2repos(src_row)
        prow = package_to_row
        local = repos.local
        if local.source.sections:
            repos.update()
            local.parse_release()
            for section in local.source.sections:
                packages = local.full_parse(section).values()
                self._insert_some_packages(table, packages)
        else:
            packages = local.full_parse().values()
            self._insert_some_packages(table, packages)

    def _row2repsource(self, row, http_mirror):
        lp = row.local_path
        while lp[0] == '/':
            lp = lp[1:]
        uri = os.path.join(http_mirror, lp)
        suite = row.dist
        src = 'deb %s %s' % (uri, suite)
        if not suite.endswith('/'):
            src = '%s %s' % (src, row.sections)
        r = RepositorySource(src)
        return r

    def _row2repos(self, row):
        rsource = self._row2repsource(row, self.http_mirror)
        lsource = self._row2repsource(row, self.local_mirror)
        return RemoteRepos(rsource, lsource)

    def _get_packages_file(self, repos):
        if repos.source.has_release():
            repos.update()
        else:
            rpath = os.path.join(repos.source.uri, repos.source.suite,
                                 'Packages.gz')
            # the [5:] slice is to remove file: from local uri
            lpath = os.path.join(repos.local.source.uri, repos.source.suite,
                                 'Packages.gz')[5:]
            if not os.path.isfile(lpath):
                print 'lpath is --->', lpath
                makepaths(os.path.dirname(lpath))
                print rpath, lpath, 'getting now'
                wget(rpath, lpath)

    def _update_suite_packages(self, suite):
        rows = self.get_sources_rows(suite)
        for row in rows:
            repos = self._row2repos(row)
            self._get_packages_file(repos)
Esempio n. 14
0
class Family(object):
    def __init__(self, conn):
        object.__init__(self)
        self.conn = conn
        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 _check_family(self, family):
        if family is not None:
            return family
        else:
            family = self.current
        if family is None:
            raise Error, 'either pass a family arguement or call set_family on this object'
        return 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):
        family = self._check_family(family)
        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):
        family = self._check_family(family)
        rows = self.parent_rows(family)
        return [x.parent for x in rows]
    
    def environment_rows(self, family=None):
        family = self._check_family(family)
        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):
        suite_cursor = SuiteCursor(self.conn)
        suites = suite_cursor.get_suites()
        stmt = select_multisuite_union(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, family=None):
        family = self._check_family(family)
        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()
        self.report_total_families(len(families))
        for f in families:
            self.write_family(f, path)
            self.report_family_exported(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_family_xml(self, xmlfile):
        xml = parse_file(xmlfile)
        elements = xml.getElementsByTagName('family')
        if len(elements) != 1:
            raise Error, 'bad number of family tags %s' % len(elements)
        element = elements[0]
        return element
    
            
    def import_family_xml(self, xmlfile):
        element = self._import_family_xml(xmlfile)
        self.import_family(element)
        
    def import_families(self, path):
        xmlfiles = [join(path, x) for x in os.listdir(path) if x[-4:] == '.xml']
        families = []
        for f in xmlfiles:
            element = self._import_family_xml(f)
            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'

    def getVariablesConfig(self, family=None):
        family = self._check_family(family)
        return FamilyVariablesConfig(self.conn, family)
    
    def deleteVariable(self, trait, name, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        self.env.cursor.delete(clause=clause)

    def insertVariable(self, trait, name, value, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        testrows = self.cursor.select(table='family_environment', clause=clause)
        if not len(testrows):
            data['value'] = value
            self.cursor.insert(table='family_environment', data=data)
        
    def report_family_exported(self, family, path):
        print 'family %s exported to %s' % (family, path)

    def report_total_families(self, total):
        print 'exporting %d families' % total
Esempio n. 15
0
class Family(object):
    def __init__(self, conn):
        object.__init__(self)
        self.conn = conn
        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 _check_family(self, family):
        if family is not None:
            return family
        else:
            family = self.current
        if family is None:
            raise Error, 'either pass a family argument or call set_family on this object'
        return family

    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):
        family = self._check_family(family)
        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):
        family = self._check_family(family)
        rows = self.parent_rows(family)
        return [x.parent for x in rows]

    def environment_rows(self, family=None):
        family = self._check_family(family)
        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):
        suite_cursor = SuiteCursor(self.conn)
        suites = suite_cursor.get_suites()
        stmt = select_multisuite_union(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 delete_family(self, family=None):
        family = self._check_family(family)
        self.cursor.execute("select * from delete_family('%s')" % family)

    def insert_parents(self, parents, family=None):
        family = self._check_family(family)
        self.parent.insert('parent', parents)

    def delete_parents(self, parents=[], family=None):
        family = self._check_family(family)
        if not parents:
            parents = self.parents()
        if not parents:
            return
        table = 'family_parent'
        clause = Eq('family', family) & In('parent', parents)
        self.cursor.delete(table=table, clause=clause)

    def FamilyData(self, families=[]):
        # change sep here
        sep = PAELLA_TRAIT_NAME_SEP
        # we don't know what to do here
        # it is possible to have an empty
        # list of families, and if that's the
        # case we don't need to make the
        # list of [self.current] .  I need to look
        # for everywhere this method is called.
        # In the installer, the family is never set,
        # and we use the list of families provided
        # by the profile and the machine.  In this
        # case, the families argument will always
        # be a list, empty or not.
        # I can't think of any other circumstance
        # where this method would be called outside
        # of the installer, or a script, and the script
        # should be using either a profile or machine
        # to get the list of families anyway, make the
        # point moot.  I will probably remove this part
        # of the code in the near future, once I'm sure
        # that there's no reason to pass None to the
        # families argument.
        if families is None:
            families = [self.current]
        all = self.make_familylist(families)
        superdict = {}
        for f in all:
            superdict.update(self.env.make_tagdict(f, sep=sep))
        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
        # row isn't used, except to determine that the family exists
        row = self.cursor.select_row(table='families',
                                     clause=Eq('family', family))
        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()
        self.report_total_families(len(families))
        for f in families:
            self.write_family(f, path)
            self.report_family_exported(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_family_xml(self, xmlfile):
        xml = parse_file(xmlfile)
        elements = xml.getElementsByTagName('family')
        if len(elements) != 1:
            raise Error, 'bad number of family tags %s' % len(elements)
        element = elements[0]
        return element

    def import_family_xml(self, xmlfile):
        element = self._import_family_xml(xmlfile)
        self.import_family(element)

    def import_families(self, path):
        xmlfiles = [
            join(path, x) for x in os.listdir(path) if x[-4:] == '.xml'
        ]
        families = []
        for f in xmlfiles:
            element = self._import_family_xml(f)
            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'

    def getVariablesConfig(self, family=None):
        family = self._check_family(family)
        return FamilyVariablesConfig(self.conn, family)

    def deleteVariable(self, trait, name, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k, v in data.items()])
        self.env.cursor.delete(clause=clause)

    def insertVariable(self, trait, name, value, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k, v in data.items()])
        testrows = self.cursor.select(table='family_environment',
                                      clause=clause)
        if not len(testrows):
            data['value'] = value
            self.cursor.insert(table='family_environment', data=data)

    def report_family_exported(self, family, path):
        print 'family %s exported to %s' % (family, path)

    def report_total_families(self, total):
        print 'exporting %d families' % total
Esempio n. 16
0
class Family(object):
    def __init__(self, conn):
        object.__init__(self)
        self.conn = conn
        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 _check_family(self, family):
        if family is not None:
            return family
        else:
            family = self.current
        if family is None:
            raise Error, 'either pass a family argument or call set_family on this object'
        return family
    
    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):
        family = self._check_family(family)
        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):
        family = self._check_family(family)
        rows = self.parent_rows(family)
        return [x.parent for x in rows]
    
    def environment_rows(self, family=None):
        family = self._check_family(family)
        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):
        suite_cursor = SuiteCursor(self.conn)
        suites = suite_cursor.get_suites()
        stmt = select_multisuite_union(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 delete_family(self, family=None):
        family = self._check_family(family)
        self.cursor.execute("select * from delete_family('%s')" % family)
        
    def insert_parents(self, parents, family=None):
        family = self._check_family(family)
        self.parent.insert('parent', parents)

    def delete_parents(self, parents=[], family=None):
        family = self._check_family(family)
        if not parents:
            parents = self.parents()
        if not parents:
            return
        table= 'family_parent'
        clause = Eq('family', family) & In('parent', parents)
        self.cursor.delete(table=table, clause=clause)
        
    def FamilyData(self, families=[]):
        # change sep here
        sep = PAELLA_TRAIT_NAME_SEP
        # we don't know what to do here
        # it is possible to have an empty
        # list of families, and if that's the
        # case we don't need to make the
        # list of [self.current] .  I need to look
        # for everywhere this method is called.
        # In the installer, the family is never set,
        # and we use the list of families provided
        # by the profile and the machine.  In this
        # case, the families argument will always
        # be a list, empty or not.
        # I can't think of any other circumstance
        # where this method would be called outside
        # of the installer, or a script, and the script
        # should be using either a profile or machine
        # to get the list of families anyway, make the
        # point moot.  I will probably remove this part
        # of the code in the near future, once I'm sure
        # that there's no reason to pass None to the
        # families argument.
        if families is None:
            families = [self.current]
        all = self.make_familylist(families)
        superdict = {}
        for f in all:
            superdict.update(self.env.make_tagdict(f, sep=sep))
        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
        # row isn't used, except to determine that the family exists
        row = self.cursor.select_row(table='families', clause=Eq('family', family))
        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()
        self.report_total_families(len(families))
        for f in families:
            self.write_family(f, path)
            self.report_family_exported(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_family_xml(self, xmlfile):
        xml = parse_file(xmlfile)
        elements = xml.getElementsByTagName('family')
        if len(elements) != 1:
            raise Error, 'bad number of family tags %s' % len(elements)
        element = elements[0]
        return element
    
            
    def import_family_xml(self, xmlfile):
        element = self._import_family_xml(xmlfile)
        self.import_family(element)
        
    def import_families(self, path):
        xmlfiles = [join(path, x) for x in os.listdir(path) if x[-4:] == '.xml']
        families = []
        for f in xmlfiles:
            element = self._import_family_xml(f)
            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'

    def getVariablesConfig(self, family=None):
        family = self._check_family(family)
        return FamilyVariablesConfig(self.conn, family)
    
    def deleteVariable(self, trait, name, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        self.env.cursor.delete(clause=clause)

    def insertVariable(self, trait, name, value, family=None):
        family = self._check_family(family)
        data = dict(family=family, trait=trait, name=name)
        clause = reduce(and_, [Eq(k, v) for k,v in data.items()])
        testrows = self.cursor.select(table='family_environment', clause=clause)
        if not len(testrows):
            data['value'] = value
            self.cursor.insert(table='family_environment', data=data)
        
    def report_family_exported(self, family, path):
        print 'family %s exported to %s' % (family, path)

    def report_total_families(self, total):
        print 'exporting %d families' % total
Esempio n. 17
0
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)
Esempio n. 18
0
def start_schema(conn):
    cursor = StatementCursor(conn, 'start_schema')
    map(cursor.create_sequence, primary_sequences())
    tables, mapping = primary_tables()
    map(cursor.create_table, tables)
    priorities_table = mapping['priorities']
    insert_list(cursor, priorities_table.name, 'priority', PRIORITIES)
    insert_list(cursor, 'scriptnames', 'script', SCRIPTS)
    newscripts = [s for s in MTSCRIPTS if s not in SCRIPTS]
    insert_list(cursor, 'scriptnames', 'script', newscripts)
    cursor.execute(grant_public([x.name for x in tables]))
    cursor.execute(grant_public(['current_environment'], 'ALL'))
    cursor.execute(grant_public(['partition_workspace'], 'ALL'))
    cursor.execute(plpgsql_delete_trait)
    cursor.execute(pgsql_delete_profile())
    cursor.execute(pgsql_delete_family())
    cursor.execute(pgsql_delete_disk())
    cursor.execute(pgsql_delete_mtype())
    cursor.execute(pgsql_delete_filesystem())