Example #1
0
 def __init__(self, conn):
     self.conn = conn
     self.suitecursor = SuiteCursor(self.conn)
     self.aptsrc = AptSourceHandler(self.conn)
     self.main_path = None
     self.profile = Profile(self.conn)
     self.family = Family(self.conn)
     self.diskconfig = DiskConfigHandler(self.conn)
     self.aptkeys = AptKeyHandler(self.conn)
Example #2
0
 def setup_cursors(self):
     self.profile = Profile(self.conn)
     self.family = Family(self.conn)
     self.machines = MachineHandler(self.conn)
     self.diskconfig = DiskConfigHandler(self.conn)
     self.aptkeys = AptKeyHandler(self.conn)
Example #3
0
 def setup_cursors(self):
     self.profile = Profile(self.conn)
     self.family = Family(self.conn)
     self.machines = MachineHandler(self.conn)
     self.diskconfig = DiskConfigHandler(self.conn)
     self.aptkeys = AptKeyHandler(self.conn)
Example #4
0
class PaellaExporter(object):
    def __init__(self, conn):
        self.conn = conn
        self.cursor = self.conn.cursor(statement=True)
        self.suitecursor = SuiteCursor(self.conn)
        self.init_db_element()
        if len(self.cursor.tables()):
            self.setup_cursors()

    def setup_cursors(self):
        self.profile = Profile(self.conn)
        self.family = Family(self.conn)
        self.machines = MachineHandler(self.conn)
        self.diskconfig = DiskConfigHandler(self.conn)
        self.aptkeys = AptKeyHandler(self.conn)

    def init_db_element(self):
        self.dbelement = PaellaDatabaseElement()

    def make_complete_db_element(self):
        self.init_db_element()
        self._append_apt_sources_to_db_element()
        fields = ['suite']
        for suite in self.suitecursor.get_suites():
            self._append_suite_to_db_element(suite)

    def _append_apt_sources_to_db_element(self):
        rows = self.cursor.select(table='apt_sources', order=['apt_id'])
        for row in rows:
            self.dbelement.append_apt_source(row.apt_id, row.uri, row.dist,
                                             row.sections, row.local_path)

    def _append_suite_to_db_element(self, suite):
        self.dbelement.append_suite(suite)
        element = self.dbelement.suites[suite]
        os = self.suitecursor.get_os(suite=suite)
        if os is None:
            os = ''
        element.set_os(os)
        rows = self.cursor.select(table='suite_apt_sources',
                                  order=['ord'],
                                  clause=Eq('suite', suite))
        for row in rows:
            self.dbelement.append_suite_apt_source(row.suite, row.apt_id,
                                                   str(row.ord))
        rows = self.cursor.select(fields=['trait'],
                                  table='%s_traits' % suite,
                                  order=['trait'])
        traits = [row.trait for row in rows]
        self.dbelement.append_suite_traits(suite, traits=traits)

    def set_db_export_path(self, dirname):
        self.db_export_path = path(dirname)

    def export_db_element(self, dirname=None, filename='database.xml'):
        if dirname is None:
            dirname = self.db_export_path
        filename = path(dirname) / filename
        dbfile = filename.open('w')
        self.dbelement.writexml(dbfile, indent='\t', newl='\n', addindent='\t')
        dbfile.close()

    def _make_suite_export_path(self, suite):
        suitedir = self.db_export_path / suite
        makepaths(suitedir)
        return suitedir

    def export_aptkey(self, name=None, dirname=None, row=None):
        dirname = path(dirname)
        makepaths(dirname)
        if name is None and row is None:
            raise RuntimeError, "need to set either row or name"
        if row is None:
            row = self.aptkeys.get_row(name)
        basename = '%s.gpg' % row.name
        filename = dirname / basename
        filename.write_text(row.data)

    def export_all_aptkeys(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'aptkeys'
        rows = self.aptkeys.get_rows()
        for row in rows:
            self.export_aptkey(dirname=dirname, row=row)

    def export_profile(self, profile, dirname=None):
        if dirname is None:
            dirname = path('.')
        dirname = path(dirname)
        makepaths(dirname)
        # this env object should be handled
        # inside the profile object instead
        # of being handled here.
        env = self.profile.make_environment_object()
        env.set_profile(profile)
        self.profile.export_profile(dirname, profile=profile, env=env)

    def export_all_profiles(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'profiles'
        makepaths(dirname)
        profiles = self.profile.get_profile_list()
        self.report_total_profiles(len(profiles))
        env = self.profile.make_environment_object()
        for profile in profiles:
            env.set_profile(profile)
            self.profile.export_profile(dirname, profile=profile, env=env)
            self.report_profile_exported(profile)

    def export_diskconfig(self, name=None, dirname=None, row=None):
        if dirname is None:
            dirname = self.db_export_path / 'diskconfig'
        dirname = path(dirname)
        makepaths(dirname)
        if name is None and row is None:
            raise RuntimeError, 'either name or row must be passed to export_diskconfig'
        if name is not None:
            row = self.diskconfig.get(name)
        filename = row.name
        content = row.content
        diskconfig = dirname / filename
        diskconfig.write_text(content)

    def export_all_diskconfigs(self, dirname=None):
        for row in self.diskconfig.cursor.select():
            self.export_diskconfig(dirname=dirname, row=row)

    def export_all_families(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'families'
        dirname = path(dirname)
        makepaths(dirname)
        self.family.export_families(dirname)

    def export_family(self, family, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'families'
        dirname = path(dirname)
        makepaths(dirname)
        self.family.write_family(family, dirname)

    def export_trait(self, trait, suite=None, dirname=None, traitdb=None):
        if traitdb is None:
            if suite is None:
                RuntimeError, "you must pass a suite if you don't pass a Trait object"
            print "make new traitdb"
            traitdb = Trait(self.conn, suite)
        traitdb.set_trait(trait)
        if suite is None:
            suite = traitdb.suite
        if dirname is None:
            dirname = self._make_suite_export_path(suite)
        traitdb.export_trait(dirname)
        self.report_trait_exported(trait, dirname)

    def export_all_traits(self, suite, dirname=None):
        self.report_start_exporting_traits()
        traitdb = Trait(self.conn, suite)
        traits = traitdb.get_trait_list()
        self.report_total_traits(len(traits))
        for trait in traits:
            self.export_trait(trait, dirname=dirname, traitdb=traitdb)
        #self.report_all_traits_exported()

    def export_suite(self, suite, dirname=None):
        self.export_all_traits(suite, dirname=dirname)

    def export_all_suites(self, dirname=None):
        suites = self.suitecursor.get_suites()
        self.report_total_suites(len(suites))
        for suite in suites:
            self.report_exporting_suite(suite)
            self.export_suite(suite, dirname=dirname)
            self.report_suite_exported(suite)

    def export_machine_database(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path
        else:
            dirname = path(dirname)
        self.machines.export_machine_database(dirname)

    def export_machine(self, machine, dirname=None):
        if dirname is None:
            dirname = self.db_export_path
        dirname = path(dirname)
        makepaths(dirname)
        current_machine = self.machines.current_machine
        self.machines.set_machine(machine)
        self.machines.export_machine(dirname)
        if current_machine is not None:
            self.machines.set_machine(current_machine)

    def _export_environment_common(self, dirname, envtype):
        if dirname is None:
            dirname = self.db_export_path
        else:
            dirname = path(dirname)
        if envtype == 'default':
            envclass = DefaultEnvironment
        elif envtype == 'current':
            envclass = CurrentEnvironment
        else:
            raise RuntimeError, 'bad envtype %s' % envtype
        env = envclass(self.conn)
        #filename = os.path.join(path, '%s-environment' % envtype)
        #efile = file(filename, 'w')
        #env.write(efile)
        #efile.close()
        filename = '%s-environment' % envtype
        fullname = dirname / filename
        envfile = fullname.open('w')
        env.write(envfile)
        envfile.close()

    def export_default_environment(self, path=None):
        self._export_environment_common(path, 'default')

    def export_current_environment(self, path=None):
        self._export_environment_common(path, 'common')

    def perform_full_export(self, path):
        self.make_complete_db_element()
        self.set_db_export_path(path)
        self.export_db_element()
        self.export_all_suites()
        self.export_all_profiles()
        self.export_all_families()
        self.export_all_diskconfigs()
        self.export_machine_database()
        self.export_default_environment()
        self.export_all_aptkeys()

    ###################
    # reporting methods
    ####################
    def report_total_suites(self, total):
        print 'exporting %d suites' % total

    def report_exporting_suite(self, suite):
        print 'exporting suite %s' % suite

    def report_suite_exported(self, suite):
        print 'suite %s exported' % suite

    def report_total_traits(self, total):
        print 'exporting %d traits' % total

    def report_trait_exported(self, trait, path):
        print 'trait %s exported to %s' % (trait, path)

    def report_all_traits_exported(self, *args):
        print 'all traits exported'

    def report_start_exporting_traits(self):
        print 'starting to export traits'

    def report_total_profiles(self, total):
        print 'exporting %d profiles' % total

    def report_profile_exported(self, profile):
        print 'profile %s exported' % profile
Example #5
0
class PaellaExporter(object):
    def __init__(self, conn):
        self.conn = conn
        self.cursor = self.conn.cursor(statement=True)
        self.suitecursor = SuiteCursor(self.conn)
        self.init_db_element()
        if len(self.cursor.tables()):
            self.setup_cursors()
            
    def setup_cursors(self):
        self.profile = Profile(self.conn)
        self.family = Family(self.conn)
        self.machines = MachineHandler(self.conn)
        self.diskconfig = DiskConfigHandler(self.conn)
        self.aptkeys = AptKeyHandler(self.conn)
        
    def init_db_element(self):
        self.dbelement = PaellaDatabaseElement()
        

    def make_complete_db_element(self):
        self.init_db_element()
        self._append_apt_sources_to_db_element()
        fields = ['suite']
        for suite in self.suitecursor.get_suites():
            self._append_suite_to_db_element(suite)
            
    def _append_apt_sources_to_db_element(self):
        rows = self.cursor.select(table='apt_sources', order=['apt_id'])
        for row in rows:
            self.dbelement.append_apt_source(row.apt_id, row.uri, row.dist,
                                             row.sections, row.local_path)
            
    def _append_suite_to_db_element(self, suite):
        self.dbelement.append_suite(suite)
        element = self.dbelement.suites[suite]
        os = self.suitecursor.get_os(suite=suite)
        if os is None:
            os = ''
        element.set_os(os)
        rows = self.cursor.select(table='suite_apt_sources', order=['ord'],
                                  clause=Eq('suite', suite))
        for row in rows:
            self.dbelement.append_suite_apt_source(row.suite, row.apt_id,
                                                   str(row.ord))
        rows = self.cursor.select(fields=['trait'], table='%s_traits' % suite,
                                  order=['trait'])
        traits = [row.trait for row in rows]
        self.dbelement.append_suite_traits(suite, traits=traits)
        
    
    def set_db_export_path(self, dirname):
        self.db_export_path = path(dirname)

    def export_db_element(self, dirname=None, filename='database.xml'):
        if dirname is None:
            dirname = self.db_export_path
        filename = path(dirname) / filename
        dbfile = filename.open('w')
        self.dbelement.writexml(dbfile, indent='\t', newl='\n', addindent='\t')                                
        dbfile.close()
        
        
    def _make_suite_export_path(self, suite):
        suitedir = self.db_export_path / suite
        makepaths(suitedir)
        return suitedir

    def export_aptkey(self, name=None, dirname=None, row=None):
        dirname = path(dirname)
        makepaths(dirname)
        if name is None and row is None:
            raise RuntimeError, "need to set either row or name"
        if row is None:
            row = self.aptkeys.get_row(name)
        basename = '%s.gpg' % row.name
        filename = dirname / basename
        filename.write_text(row.data)
        
    def export_all_aptkeys(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'aptkeys'
        rows = self.aptkeys.get_rows()
        for row in rows:
            self.export_aptkey(dirname=dirname, row=row)
            
    def export_profile(self, profile, dirname=None):
        if dirname is None:
            dirname = path('.')
        dirname = path(dirname)
        makepaths(dirname)
        # this env object should be handled
        # inside the profile object instead
        # of being handled here.
        env = self.profile.make_environment_object()
        env.set_profile(profile)
        self.profile.export_profile(dirname, profile=profile, env=env)
        

    def export_all_profiles(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'profiles'
        makepaths(dirname)
        profiles = self.profile.get_profile_list()
        self.report_total_profiles(len(profiles))
        env = self.profile.make_environment_object()
        for profile in profiles:
            env.set_profile(profile)
            self.profile.export_profile(dirname, profile=profile, env=env)
            self.report_profile_exported(profile)

    def export_diskconfig(self, name=None, dirname=None, row=None):
        if dirname is None:
            dirname = self.db_export_path / 'diskconfig'
        dirname = path(dirname)
        makepaths(dirname)
        if name is None and row is None:
            raise RuntimeError , 'either name or row must be passed to export_diskconfig'
        if name is not None:
            row = self.diskconfig.get(name)
        filename = row.name
        content = row.content
        diskconfig = dirname / filename
        diskconfig.write_text(content)
        
    def export_all_diskconfigs(self, dirname=None):
        for row in self.diskconfig.cursor.select():
            self.export_diskconfig(dirname=dirname, row=row)
            
    def export_all_families(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'families'
        dirname = path(dirname)
        makepaths(dirname)
        self.family.export_families(dirname)

    def export_family(self, family, dirname=None):
        if dirname is None:
            dirname = self.db_export_path / 'families'
        dirname = path(dirname)
        makepaths(dirname)
        self.family.write_family(family, dirname)
        
        
    def export_trait(self, trait, suite=None, dirname=None, traitdb=None):
        if traitdb is None:
            if suite is None:
                RuntimeError , "you must pass a suite if you don't pass a Trait object"
            print "make new traitdb"
            traitdb = Trait(self.conn, suite)
        traitdb.set_trait(trait)
        if suite is None:
            suite = traitdb.suite
        if dirname is None:
            dirname = self._make_suite_export_path(suite)
        traitdb.export_trait(dirname)
        self.report_trait_exported(trait, dirname)
        

    def export_all_traits(self, suite, dirname=None):
        self.report_start_exporting_traits()
        traitdb = Trait(self.conn, suite)
        traits = traitdb.get_trait_list()
        self.report_total_traits(len(traits))
        for trait in traits:
            self.export_trait(trait, dirname=dirname, traitdb=traitdb)
        #self.report_all_traits_exported()
            
    def export_suite(self, suite, dirname=None):
        self.export_all_traits(suite, dirname=dirname)

    def export_all_suites(self, dirname=None):
        suites = self.suitecursor.get_suites()
        self.report_total_suites(len(suites))
        for suite in suites:
            self.report_exporting_suite(suite)
            self.export_suite(suite, dirname=dirname)
            self.report_suite_exported(suite)

    def export_machine_database(self, dirname=None):
        if dirname is None:
            dirname = self.db_export_path
        else:
            dirname = path(dirname)
        self.machines.export_machine_database(dirname)

    def export_machine(self, machine, dirname=None):
        if dirname is None:
            dirname = self.db_export_path
        dirname = path(dirname)
        makepaths(dirname)
        current_machine = self.machines.current_machine
        self.machines.set_machine(machine)
        self.machines.export_machine(dirname)
        if current_machine is not None:
            self.machines.set_machine(current_machine)
        
    def _export_environment_common(self, dirname, envtype):
        if dirname is None:
            dirname = self.db_export_path
        else:
            dirname = path(dirname)
        if envtype == 'default':
            envclass = DefaultEnvironment
        elif envtype == 'current':
            envclass = CurrentEnvironment
        else:
            raise RuntimeError , 'bad envtype %s' % envtype
        env = envclass(self.conn)
        #filename = os.path.join(path, '%s-environment' % envtype)
        #efile = file(filename, 'w')
        #env.write(efile)
        #efile.close()
        filename = '%s-environment' % envtype
        fullname = dirname / filename
        envfile = fullname.open('w')
        env.write(envfile)
        envfile.close()
        
    def export_default_environment(self, path=None):
        self._export_environment_common(path, 'default')
        
    def export_current_environment(self, path=None):
        self._export_environment_common(path, 'common')

    def perform_full_export(self, path):
        self.make_complete_db_element()
        self.set_db_export_path(path)
        self.export_db_element()
        self.export_all_suites()
        self.export_all_profiles()
        self.export_all_families()
        self.export_all_diskconfigs()
        self.export_machine_database()
        self.export_default_environment()
        self.export_all_aptkeys()
        
        

    ###################
    # reporting methods
    ####################
    def report_total_suites(self, total):
        print 'exporting %d suites' % total

    def report_exporting_suite(self, suite):
        print 'exporting suite %s' % suite
        
    def report_suite_exported(self, suite):
        print 'suite %s exported' % suite
        
    def report_total_traits(self, total):
        print 'exporting %d traits' % total

    def report_trait_exported(self, trait, path):
        print 'trait %s exported to %s' % (trait, path)

    def report_all_traits_exported(self, *args):
        print 'all traits exported'

    def report_start_exporting_traits(self):
        print 'starting to export traits'

    def report_total_profiles(self, total):
        print 'exporting %d profiles' % total

    def report_profile_exported(self, profile):
        print 'profile %s exported' % profile