From c7425d50162106b2de997d71ec3b8bba65c03a15 Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Mon, 01 Oct 2012 21:31:06 +0000 Subject: Make environment available to kspost nochroot scripts Generate the environment just once during the build process, and additionally dump it in shell format to a file. Load this file when executing nochroot scripts. Such scripts now have access to config settings, etc. The default interpreter was changed from sh to bash because shlib uses bashisms. --- diff --git a/osbuilder.py b/osbuilder.py index 2550665..de4a15d 100755 --- a/osbuilder.py +++ b/osbuilder.py @@ -33,6 +33,7 @@ import subprocess import shutil import re import time +import pipes from optparse import OptionParser class StageException(Exception): @@ -49,43 +50,6 @@ class Stage(object): self.ignore_failures = ignore_failures pass - def _make_environment(self): - env = {} - - # copy some bits from parent environment - for key in ('HOSTNAME', 'USER', 'USERNAME', 'LANG', 'HOME', 'LOGNAME', - 'SHELL', 'PATH'): - if key in os.environ: - env[key] = os.environ[key] - - env['PYTHONPATH'] = self.osb.libdir - env['OOB__shlib'] = os.path.join(self.osb.libdir, 'shlib.sh') - env['OOB__libdir'] = self.osb.libdir - env['OOB__bindir'] = self.osb.bindir - env['OOB__builddir'] = self.osb.builddir - env['OOB__cachedir'] = self.osb.cachedir - env['OOB__intermediatesdir'] = self.osb.intermediatesdir - env['OOB__shareddir'] = self.osb.shareddir - env['OOB__outputdir'] = self.osb.outputdir - env['OOB__statedir'] = self.osb.statedir - env['OOB__fsmount'] = self.osb.fsmount - - env['oob_config_dir'] = os.path.dirname(self.osb.build_configs[0]) - - if self.osb.cacheonly: - env['OOB__cacheonly'] = 'True' - - envpath = env['PATH'].split(':') - for dir in ('/sbin', '/usr/sbin'): - if envpath.count(dir) == 0: - env['PATH'] = env['PATH'] + ':' + dir - - for section in self.osb.cfg.sections(): - for option in self.osb.cfg.options(section): - val = self.osb.cfg.get(section, option) - env["CFG_%s__%s" % (section, option)] = val - return env - def _run_part(self, mod, part, output): print " * Running part %s %s %s..." % (self.name, mod, part) self.on_run_part(mod, part, output) @@ -101,9 +65,8 @@ class Stage(object): for line in fd: output.write(line) elif path.endswith(".sh"): - shenv = self._make_environment() proc = subprocess.Popen(["/bin/bash", path], shell=False, - stdout=outtype, env=shenv) + stdout=outtype, env=self.osb.env) try: (out, err) = proc.communicate() except (Exception, KeyboardInterrupt), e: @@ -115,9 +78,8 @@ class Stage(object): if not self.console_output: output.write(out) elif path.endswith(".py"): - shenv = self._make_environment() proc = subprocess.Popen(["/usr/bin/python", path], shell=False, - stdout=outtype, env=shenv) + stdout=outtype, env=self.osb.env) try: (out, err) = proc.communicate() except (Exception, KeyboardInterrupt), e: @@ -212,13 +174,18 @@ class KspostStage(KsStage): super(KspostStage, self).__init__(osb, "kspost") def on_run_part(self, mod, part, output): - output.write("\n%post --erroronfail") - if ".nochroot." in part: + output.write("\n%post --erroronfail --interpreter=/bin/bash") + nochroot = ".nochroot." in part + if nochroot: output.write(" --nochroot") print >>output, "\necho 'Executing code generated by %s:%s/%s...'" \ % (self.name, mod, part) print >>output, "set -e" + # Make the normal environment available for non-chroot scripts + if nochroot: + print >>output, ". " + os.path.join(self.osb.intermediatesdir, 'env') + def on_run_part_done(self, mod, part, output): print >>output, "%end" @@ -313,6 +280,51 @@ class OsBuilder(object): self.modules.remove('global') self.read_config() + self.env = self.make_environment() + + def make_environment(self): + env = {} + + # copy some bits from parent environment + for key in ('HOSTNAME', 'USER', 'USERNAME', 'LANG', 'HOME', 'LOGNAME', + 'SHELL', 'PATH'): + if key in os.environ: + env[key] = os.environ[key] + + env['PYTHONPATH'] = self.libdir + env['OOB__shlib'] = os.path.join(self.libdir, 'shlib.sh') + env['OOB__libdir'] = self.libdir + env['OOB__bindir'] = self.bindir + env['OOB__builddir'] = self.builddir + env['OOB__cachedir'] = self.cachedir + env['OOB__intermediatesdir'] = self.intermediatesdir + env['OOB__shareddir'] = self.shareddir + env['OOB__outputdir'] = self.outputdir + env['OOB__statedir'] = self.statedir + env['OOB__fsmount'] = self.fsmount + + env['oob_config_dir'] = os.path.dirname(self.build_configs[0]) + + if self.cacheonly: + env['OOB__cacheonly'] = 'True' + + envpath = env['PATH'].split(':') + for dir in ('/sbin', '/usr/sbin'): + if envpath.count(dir) == 0: + env['PATH'] = env['PATH'] + ':' + dir + + for section in self.cfg.sections(): + for option in self.cfg.options(section): + val = self.cfg.get(section, option) + env["CFG_%s__%s" % (section, option)] = val + return env + + def dump_environment(self): + # Dump the build environment into a bash-compatible script + fd = open(os.path.join(self.intermediatesdir, "env"), "w") + for var, value in self.env.iteritems(): + fd.write("export %s=%s\n" % (var, pipes.quote(value))) + fd.close() def suggested_mismatch(self, suggested_version): # we only compare the first two version components @@ -388,6 +400,8 @@ class OsBuilder(object): if not os.path.exists(dir): os.makedirs(dir) + self.dump_environment() + # truncate file and write header ksfd = open(self.get_ks_file_path(), 'w') ksfd.write("# Generated with OLPC OS builder\n") -- cgit v0.9.1