def __init__(self, conn, suite, trait, script): self.conn = conn self.suite = suite self.trait = trait self.script = TraitScript(self.conn, self.suite) self.script.set_trait(trait) self.current_script = script text = self.script.read_script(self.current_script) TextScroll.__init__(self, text)
class ScriptNotebook(MyNotebook): def __init__(self, conn, suite, trait): MyNotebook.__init__(self) self.conn = conn self.suite = suite self.trait = trait self.script = TraitScript(self.conn, self.suite) self.script.set_trait(trait) self.menu = SimpleMenu() for row in self.script.scripts(): name = row.script sfile = self.script.scriptfile(name) self.append_script(name, sfile.read()) sfile.close() def populate_menu(self, widget, mainmenu, tmenu): menuitem = widget.get_name() populate_menu(mainmenu, self.trait, [menuitem], self.edit_script) def edit_script(self, menuitem, name, parent): sfile = self.script.get(name) tmp, path = tempfile.mkstemp('paella', 'script') script = sfile.read() sfile.close() tmp = file(path, 'w') tmp.write(script) tmp.close() os.system('$EDITOR %s' %path) tmp = file(path, 'r') mod = tmp.read() tmp.seek(0) if mod != script: print 'script modified' self.script.save_script(name, tmp) self.set_text(name, mod) os.remove(path) def new_script(self, name): if self.script.get(name) is not None: dialogs.Message('script exists') raise Error, 'script exists' tmp = strfile() self.script.insert_script(name, tmp) self.append_script(name) tmp.close() self.edit_script(None, name, None) def append_script(self, name, text=''): page = TextScroll(text, name=name) page.tview.connect('populate-popup', self.populate_menu, self.menu) self.append_page(page, name) def set_text(self, name, text): self.pages[name].set_text(text)
def __init__(self, conn, suite, trait): MyNotebook.__init__(self) self.conn = conn self.suite = suite self.trait = trait self.script = TraitScript(self.conn, self.suite) self.script.set_trait(trait) self.menu = SimpleMenu() for row in self.script.scripts(): name = row.script sfile = self.script.scriptfile(name) self.append_script(name, sfile.read()) sfile.close()
class TraitList(KListView): def __init__(self, app, parent, ftype='template', name='TraitList'): KListView.__init__(self, parent, name) dbwidget(self, app) self.ftype = ftype self.scripts = None self.templates = None self.traits = None self.setRootIsDecorated(True) self.addColumn('trait/file') self.addColumn('name') self.addColumn('package') def set_suite(self, suite): self.scripts = TraitScript(self.conn, suite) self.templates = TraitTemplate(self.conn, suite) self.traits = Traits(self.conn, suite) def refreshlistView(self): self.clear() if self.ftype == 'template': self.setColumnText(1, 'template') elif self.ftype == 'script': self.setColumnText(1, 'script') for trait in self.traits.list(): item = KListViewItem(self, trait) item.trait = trait if self.ftype == 'template': for row in self.templates.templates(trait): titem = KListViewItem(item, str(row.templatefile), row.template, row.package) titem.trait = trait titem.row = row elif self.ftype == 'script': for row in self.scripts.cmd.select(clause=Eq('trait', trait), order='script'): sitem = KListViewItem(item, str(row.scriptfile), row.script) sitem.trait = trait sitem.row = row def getData(self): item = self.currentItem() if self.ftype == 'template': self.templates.set_trait(item.trait) return self.templates.templatedata(item.row.package, item.row.template) elif self.ftype == 'script': self.scripts.set_trait(item.trait) return self.scripts.scriptdata(item.row.script) def updateData(self, data): item = self.currentItem() row = item.row if self.ftype == 'template': self.templates.set_trait(item.trait) self.templates.update_templatedata(row.package, row.template, data) elif self.ftype == 'script': self.scripts.set_trait(item.trait) self.scripts.update_scriptdata(row.script, data)
def diff_selection(self, *args): lrow = self.view.lbox.get_selected_data()[0] rrow = self.view.rbox.get_selected_data()[0] lscript = TraitScript(self.conn, self.lsuite) rscript = TraitScript(self.conn, self.rsuite) lscript.set_trait(lrow.trait) rscript.set_trait(rrow.trait) ldata = lscript.scriptdata(lrow.script) rdata = rscript.scriptdata(rrow.script) differ = Differ(ldata, rdata) differ.diff() if differ.isdifferent('left', ldata): newdata = differ.get_data('left') lscript.update_scriptdata(lrow.script, newdata) if differ.isdifferent('right', rdata): newdata = differ.get_data('right') rscript.update_scriptdata(rrow.script, newdata)
def set_suite(self, suite): self.scripts = TraitScript(self.conn, suite) self.templates = TraitTemplate(self.conn, suite) self.traits = Traits(self.conn, suite)
def __init__(self, conn, suite, cfg): Installer.__init__(self, conn, cfg=cfg) self.traitpackage = TraitPackage(conn, suite) self.traittemplate = TraitTemplate(conn, suite) self.traitdebconf = TraitDebconf(conn, suite) self.traitscripts = TraitScript(conn, suite)
class TraitInstaller(Installer): def __init__(self, conn, suite, cfg): Installer.__init__(self, conn, cfg=cfg) self.traitpackage = TraitPackage(conn, suite) self.traittemplate = TraitTemplate(conn, suite) self.traitdebconf = TraitDebconf(conn, suite) self.traitscripts = TraitScript(conn, suite) def set_trait(self, trait): self.traitpackage.set_trait(trait) self.traittemplate.set_trait(trait) self.traitdebconf.set_trait(trait) self.traitscripts.set_trait(trait) self._current_trait_ = trait self.log.info('trait set to %s' % self._current_trait_) def run(self, name, command, args='', proc=False, chroot=True, keeprunning=False): tname = 'trait-%s-%s' % (self._current_trait_, name) Installer.run(self, tname, command, args=args, proc=proc, chroot=chroot, keeprunning=keeprunning) def runscript(self, script, name, info, chroot=False): self.log.info(info['start']) self.run(name, script, chroot=chroot) os.remove(script) self.log.info(info['done']) def process(self): self.log.info('processing %s' % self._current_trait_) os.environ['PAELLA_TARGET'] = self.target os.environ['PAELLA_TRAIT'] = self._current_trait_ packages = self.traitpackage.packages() templates = self.traittemplate.templates() #start pre script script = self._make_script('pre') if script is not None: info = dict(start='pre script started', done='pre script done') self.runscript(script, 'pre-script', info) #remove packages script = self._make_script('remove') if script is None: remove = [p for p in packages if p.action == 'remove'] if len(remove): self.remove(remove) else: info = dict(start='remove script started', done='remove script done') self.runscript(script, 'remove-script', info) #install packages script = self._make_script('install') if script is None: install = [p for p in packages if p.action == 'install'] if len(install): self.install(install, templates) else: info = dict(start='install script started', done='install script done') self.runscript(script, 'install-script', info) #configure packages script = self._make_script('config') if script is None: config = [p for p in packages if p.action in ['install', 'config']] if len(config): self.configure(config, templates) else: info = dict(start='config script started', done='config script done') self.runscript(script, 'config-script', info) #reconfigure debconf script = self._make_script('reconfig') if script is None: self.reconfigure_debconf() else: info = dict(start='reconfig script started', done='reconfig script done') self.runscript(script, 'reconfig-script', info) #start post script script = self._make_script('post') if script is not None: info = dict(start='post script started', done='post script done') self.runscript(script, 'post-script', info) def remove(self, packages): packages = ' '.join([p.package for p in packages]) command, args = 'apt-get -y remove', packages self.run('remove', command, args=args, proc=True) def install(self, packages, templates): package_args = ' '.join([p.package for p in packages]) cmd = 'apt-get -y --force-yes install %s\n' % package_args cmd += 'rm /var/cache/apt/archives/*.deb -f' #os.system(self.with_proc(cmd)) run = self.run('install', cmd, proc=True, keeprunning=True) if run: self.log.warn('PROBLEM installing %s' % self._current_trait_) self.log.warn('packages --> %s' % package_args) def configure(self, packages, templates): dpkg_rec = False for p in packages: for t in [t for t in templates if t.package == p.package]: if t.template == 'var/cache/debconf/config.dat': dpkg_rec = True self.log.info('Installing Debconf template ...') self.install_debconf_template(t) else: self.make_template(t) if dpkg_rec: self.log.info('Reconfiguring packages') for p in packages: cmd = 'dpkg-reconfigure -plow %s' % p.package run = self.run('dpkg-recfg', cmd, proc=True, keeprunning=True) if run: self.log.info('reconfigure failed on %s' % p.package) else: self.log.info('%s reconfigured' % p.package) script = self._make_script('chroot', execpath=True) if script is not None: #os.system(self.command(script)) self.run('chroot-script', script) if script[0] == '/': script = script[1:] os.remove(join(self.target, script)) def _make_script(self, name, execpath=False): script = self.traitscripts.get(name) if script is not None: exec_path = join('/tmp', name + '-script') target_path = join(self.target, 'tmp', name + '-script') sfile = file(target_path, 'w') sfile.write(script.read()) sfile.close() os.system('chmod 755 %s' % target_path) if not execpath: return target_path else: return exec_path else: return None def make_template(self, template): self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(self.familydata) self.traittemplate.template.update(self.profiledata) self._make_template_common(template, tmpl) def make_template_with_data(self, template, data): self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(data) self._make_template_common(template, tmpl) def _make_template_common(self, template, tmpl): sub = self.traittemplate.template.sub() newpath = join(self.target, template.template) self.log.info('target template %s' % newpath) dir = dirname(newpath) if not isdir(dir): makepaths(dir) if tmpl != sub: self.log.info('%s %s subbed' % (template.package, template.template)) newfile = file(newpath, 'w') newfile.write(sub) newfile.close() mode = template.mode if mode[0] == '0' and len(mode) <= 7 and mode.isdigit(): mode = eval(mode) os.chmod(newpath, mode) own = ':'.join([template.owner, template.grp_owner]) os.system(self.command('chown', '%s %s' %(own, join('/', template.template)))) def install_debconf_template(self, template): self.log.info('Installing debconf for %s' % self._current_trait_) self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(self.profiledata) sub = self.traittemplate.template.sub() if tmpl == sub: self.log.info('static debconf, no substitutions') self.log.info('for trait %s ' % self._current_trait_) config_path = join(self.target, 'tmp/paella_debconf') if isfile(config_path): raise Error, '%s is not supposed to be there' % config_path debconf = file(config_path, 'w') debconf.write(sub + '\n') debconf.close() target_path = join(self.target, 'var/cache/debconf/config.dat') self.log.info('debconf config is %s %s' % (config_path, target_path)) copy_configdb(config_path, target_path) os.remove(config_path) def set_template_path(self, path): self.traittemplate.template.set_path(path) def install_debconf(self): config = self.traitdebconf.get_config() config_path = join(self.target, 'tmp/paella_debconf') if isfile(config_path): raise Error, '%s is not supposed to be there' % config_path debconf = file(config_path, 'w') debconf.write(config + '\n') debconf.close() target_path = join(self.target, 'var/cache/debconf/config.dat') self.log.info('debconf config is %s %s' % (config_path, target_path)) cmd = install_debconf(config_path, target_path) command = 'sh -c "%s"' % cmd self.log.info(cmd) os.system(cmd) os.remove(config_path) def reconfigure_debconf(self): owners = self.traitdebconf.all_owners() self.log.info('ALL OWNERS %s' % owners) os.environ['DEBIAN_FRONTEND'] = 'noninteractive' for owner in owners: self.log.info('RECONFIGURING %s' % owner) os.system(self.command('dpkg-reconfigure -plow %s' % owner))
def __init__(self, message, conn, suite, name='AllScriptsDialog'): dialogs.CList.__init__(self, message, name=name) self.script = TraitScript(conn, suite) rows = self.script.cmd.select() self.set_rows(rows)
class TraitList(KListView): def __init__(self, app, parent, ftype='template', name='TraitList'): KListView.__init__(self, parent, name) dbwidget(self, app) self.ftype = ftype self.scripts = None self.templates = None self.traits = None self.trait = None self.setRootIsDecorated(True) self.addColumn('trait/file') self.addColumn('name') self.addColumn('package') def set_suite(self, suite): self.scripts = TraitScript(self.conn, suite) self.templates = TraitTemplate(self.conn, suite) self.traits = Traits(self.conn, suite) def set_trait(self, trait): self.trait = trait def refreshlistView(self): self.clear() if self.ftype == 'template': self.setColumnText(1, 'template') elif self.ftype == 'script': self.setColumnText(1, 'script') if self.trait is None: traits = self.traits.list() else: traits = [self.trait] for trait in traits: item = KListViewItem(self, trait) item.trait = trait if self.ftype == 'template': for row in self.templates.templates(trait): titem = KListViewItem(item, str(row.templatefile), row.template, row.package) titem.trait = trait titem.row = row elif self.ftype == 'script': for row in self.scripts.cmd.select(clause=Eq('trait', trait), order='script'): sitem = KListViewItem(item, str(row.scriptfile), row.script) sitem.trait = trait sitem.row = row def getData(self): item = self.currentItem() if self.ftype == 'template': self.templates.set_trait(item.trait) return self.templates.templatedata(item.row.package, item.row.template) elif self.ftype == 'script': self.scripts.set_trait(item.trait) return self.scripts.scriptdata(item.row.script) def updateData(self, data): item = self.currentItem() row = item.row if self.ftype == 'template': self.templates.set_trait(item.trait) self.templates.update_templatedata(row.package, row.template, data) elif self.ftype == 'script': self.scripts.set_trait(item.trait) self.scripts.update_scriptdata(row.script, data)
class TraitInstaller(Installer): def __init__(self, conn, suite, cfg): Installer.__init__(self, conn, cfg=cfg) self.traitpackage = TraitPackage(conn, suite) self.traittemplate = TraitTemplate(conn, suite) self.traitdebconf = TraitDebconf(conn, suite) self.traitscripts = TraitScript(conn, suite) def set_trait(self, trait): self.traitpackage.set_trait(trait) self.traittemplate.set_trait(trait) self.traitdebconf.set_trait(trait) self.traitscripts.set_trait(trait) self._current_trait_ = trait self.log.info("trait set to %s" % self._current_trait_) def run(self, name, command, args="", proc=False, chroot=True, keeprunning=False): tname = "trait-%s-%s" % (self._current_trait_, name) Installer.run(self, tname, command, args=args, proc=proc, chroot=chroot, keeprunning=keeprunning) def runscript(self, script, name, info, chroot=False): self.log.info(info["start"]) self.run(name, script, chroot=chroot) os.remove(script) self.log.info(info["done"]) def process(self): self.log.info("processing %s" % self._current_trait_) os.environ["PAELLA_TARGET"] = self.target os.environ["PAELLA_TRAIT"] = self._current_trait_ packages = self.traitpackage.packages() templates = self.traittemplate.templates() # start pre script script = self._make_script("pre") if script is not None: info = dict(start="pre script started", done="pre script done") self.runscript(script, "pre-script", info) # remove packages script = self._make_script("remove") if script is None: remove = [p for p in packages if p.action == "remove"] if len(remove): self.remove(remove) else: info = dict(start="remove script started", done="remove script done") self.runscript(script, "remove-script", info) # install packages script = self._make_script("install") if script is None: install = [p for p in packages if p.action == "install"] if len(install): self.install(install, templates) else: info = dict(start="install script started", done="install script done") self.runscript(script, "install-script", info) # configure packages script = self._make_script("config") if script is None: config = [p for p in packages if p.action in ["install", "config"]] if len(config): self.configure(config, templates) else: info = dict(start="config script started", done="config script done") self.runscript(script, "config-script", info) # reconfigure debconf script = self._make_script("reconfig") if script is None: self.reconfigure_debconf() else: info = dict(start="reconfig script started", done="reconfig script done") self.runscript(script, "reconfig-script", info) # start post script script = self._make_script("post") if script is not None: info = dict(start="post script started", done="post script done") self.runscript(script, "post-script", info) def remove(self, packages): packages = " ".join([p.package for p in packages]) command, args = "apt-get -y remove", packages self.run("remove", command, args=args, proc=True) def install(self, packages, templates): package_args = " ".join([p.package for p in packages]) cmd = "apt-get -y --force-yes install %s\n" % package_args cmd += "rm /var/cache/apt/archives/*.deb -f" # os.system(self.with_proc(cmd)) run = self.run("install", cmd, proc=True, keeprunning=True) if run: self.log.warn("PROBLEM installing %s" % self._current_trait_) self.log.warn("packages --> %s" % package_args) def configure(self, packages, templates): dpkg_rec = False for p in packages: for t in [t for t in templates if t.package == p.package]: if t.template == "var/cache/debconf/config.dat": dpkg_rec = True self.log.info("Installing Debconf template ...") self.install_debconf_template(t) else: self.make_template(t) if dpkg_rec: self.log.info("Reconfiguring packages") for p in packages: cmd = "dpkg-reconfigure -plow %s" % p.package run = self.run("dpkg-recfg", cmd, proc=True, keeprunning=True) if run: self.log.info("reconfigure failed on %s" % p.package) else: self.log.info("%s reconfigured" % p.package) script = self._make_script("chroot", execpath=True) if script is not None: # os.system(self.command(script)) self.run("chroot-script", script) if script[0] == "/": script = script[1:] os.remove(join(self.target, script)) def _make_script(self, name, execpath=False): script = self.traitscripts.get(name) if script is not None: exec_path = join("/tmp", name + "-script") target_path = join(self.target, "tmp", name + "-script") sfile = file(target_path, "w") sfile.write(script.read()) sfile.close() os.system("chmod 755 %s" % target_path) if not execpath: return target_path else: return exec_path else: return None def make_template(self, template): self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(self.familydata) self.traittemplate.template.update(self.profiledata) self._make_template_common(template, tmpl) def make_template_with_data(self, template, data): self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(data) self._make_template_common(template, tmpl) def _make_template_common(self, template, tmpl): sub = self.traittemplate.template.sub() newpath = join(self.target, template.template) self.log.info("target template %s" % newpath) dir = dirname(newpath) if not isdir(dir): makepaths(dir) if tmpl != sub: self.log.info("%s %s subbed" % (template.package, template.template)) newfile = file(newpath, "w") newfile.write(sub) newfile.close() mode = template.mode if mode[0] == "0" and len(mode) <= 7 and mode.isdigit(): mode = eval(mode) os.chmod(newpath, mode) own = ":".join([template.owner, template.grp_owner]) os.system(self.command("chown", "%s %s" % (own, join("/", template.template)))) def install_debconf_template(self, template): self.log.info("Installing debconf for %s" % self._current_trait_) self.traittemplate.set_template(template.package, template.template) tmpl = self.traittemplate.template.template self.traittemplate.template.update(self.profiledata) sub = self.traittemplate.template.sub() if tmpl == sub: self.log.info("static debconf, no substitutions") self.log.info("for trait %s " % self._current_trait_) config_path = join(self.target, "tmp/paella_debconf") if isfile(config_path): raise Error, "%s is not supposed to be there" % config_path debconf = file(config_path, "w") debconf.write(sub + "\n") debconf.close() target_path = join(self.target, "var/cache/debconf/config.dat") self.log.info("debconf config is %s %s" % (config_path, target_path)) copy_configdb(config_path, target_path) os.remove(config_path) def set_template_path(self, path): self.traittemplate.template.set_path(path) def install_debconf(self): config = self.traitdebconf.get_config() config_path = join(self.target, "tmp/paella_debconf") if isfile(config_path): raise Error, "%s is not supposed to be there" % config_path debconf = file(config_path, "w") debconf.write(config + "\n") debconf.close() target_path = join(self.target, "var/cache/debconf/config.dat") self.log.info("debconf config is %s %s" % (config_path, target_path)) cmd = install_debconf(config_path, target_path) command = 'sh -c "%s"' % cmd self.log.info(cmd) os.system(cmd) os.remove(config_path) def reconfigure_debconf(self): owners = self.traitdebconf.all_owners() self.log.info("ALL OWNERS %s" % owners) os.environ["DEBIAN_FRONTEND"] = "noninteractive" for owner in owners: self.log.info("RECONFIGURING %s" % owner) os.system(self.command("dpkg-reconfigure -plow %s" % owner))