From: Michael Tremer Date: Sat, 27 Mar 2010 14:06:37 +0000 (+0100) Subject: naoki: Add shell command. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1967b9ef77292e97757fddc9fc09c3e6519bce08;p=ipfire-3.x.git naoki: Add shell command. --- diff --git a/naoki/__init__.py b/naoki/__init__.py index 23e54854d..04e9d88e2 100644 --- a/naoki/__init__.py +++ b/naoki/__init__.py @@ -37,6 +37,7 @@ class Naoki(object): "toolchain" : self.call_toolchain, "package" : self.call_package, "source" : self.call_source, + "shell" : self.call_shell, } return actionmap[args.action.name](args.action) @@ -253,6 +254,14 @@ Release : %(release)s self.log.info("Removing %s..." % file) os.remove(os.path.join(TARBALLDIR, file)) + def call_shell(self, args): + package = backend.parse_package([args.package], naoki=self)[0] + + environ = chroot.Environment(package) + + return environ.shell(args.args, + cleanbefore=args.cleanbefore, cleanafter=not args.nocleanafter) + def _build(self, packages, force=False): requeue = [] packages = package.depsort(packages) diff --git a/naoki/chroot.py b/naoki/chroot.py index efcaf0746..eec474680 100644 --- a/naoki/chroot.py +++ b/naoki/chroot.py @@ -103,34 +103,39 @@ class Environment(object): return self.doChroot("make -C %s -f %s %s" % \ (os.path.dirname(file), file, target), shell=True) - def doChroot(self, command, shell=True, *args, **kwargs): - ret = None - try: - # XXX Should be globally defined - env = config.environment.copy() + @property + def environ(self): + env = config.environment.copy() + env.update({ + "HOME" : "/root", + "BASEDIR" : "/usr/src", + "PKGROOT" : "/usr/src/pkgs", + "TOOLS_DIR" : "/tools_%s" % self.arch["name"], + "TARGET" : "%s-ipfire-linux-gnu" % self.arch["machine"], + "TARGET_MACHINE" : self.arch["machine"], + "PATH" : "/sbin:/bin:/usr/sbin:/usr/bin:/tools_%(arch)s/sbin:/tools_%(arch)s/bin" \ + % { "arch" : self.arch["name"], }, + "BUILDROOT" : "/%s" % self.buildroot, + "CHROOT" : "1", + "CFLAGS" : self.arch["cflags"], + "CXXFLAGS" : self.arch["cxxflags"], + "PKG_ARCH" : self.arch["name"], + }) + + ccache_path = os.path.join("tools_%s" % self.arch["name"], + "usr", "ccache", "bin") + if os.path.exists(self.chrootPath(ccache_path)): env.update({ - "HOME" : "/root", - "BASEDIR" : "/usr/src", - "PKGROOT" : "/usr/src/pkgs", - "TOOLS_DIR" : "/tools_%s" % self.arch["name"], - "TARGET" : "%s-ipfire-linux-gnu" % self.arch["machine"], - "TARGET_MACHINE" : self.arch["machine"], - "PATH" : "/sbin:/bin:/usr/sbin:/usr/bin:/tools_%(arch)s/sbin:/tools_%(arch)s/bin" \ - % { "arch" : self.arch["name"], }, - "BUILDROOT" : "/%s" % self.buildroot, - "CHROOT" : "1", - "CFLAGS" : self.arch["cflags"], - "CXXFLAGS" : self.arch["cxxflags"], - "PKG_ARCH" : self.arch["name"], + "PATH" : "/%s:%s" % (ccache_path, env["PATH"]), + "CCACHE_DIR" : "/usr/src/ccache", }) - ccache_path = os.path.join("tools_%s" % self.arch["name"], - "usr", "ccache", "bin") - if os.path.exists(self.chrootPath(ccache_path)): - env.update({ - "PATH" : "/%s:%s" % (ccache_path, env["PATH"]), - "CCACHE_DIR" : "/usr/src/ccache", - }) + return env + + def doChroot(self, command, shell=True, *args, **kwargs): + ret = None + try: + env = self.environ if kwargs.has_key("env"): env.update(kwargs.pop("env")) @@ -284,6 +289,25 @@ class Environment(object): def log(self): return self.package.log + def shell(self, args, cleanbefore=False, cleanafter=True): + if cleanbefore: + self.clean() + + command = "chroot %s /usr/src/tools/chroot-shell %s" % \ + (self.chrootPath(), " ".join(args)) + + for key, val in self.environ.items(): + command = "%s=\"%s\" " % (key, val) + command + + try: + self._mountall() + + shell = os.system(command) + return os.WEXITSTATUS(shell) + + finally: + self._umountall() + class Toolchain(object): def __init__(self, arch): diff --git a/naoki/terminal.py b/naoki/terminal.py index c979cbd4e..bb1cd60a1 100644 --- a/naoki/terminal.py +++ b/naoki/terminal.py @@ -134,6 +134,29 @@ class _Argument(object): raise NotImplementedError +class Argument(_Argument): + def __init__(self, name, **kwargs): + _Argument.__init__(self, name, [], **kwargs) + + def parse(self, args): + self._parsed = True + + if len(args) >= 1: + self._parsed_args = args[:1] + + return args[1:] + + def value(self): + if self._parsed_args: + return self._parsed_args[0] + + return [] + + @property + def help_line(self): + return self.name + + class Option(_Argument): def parse(self, args): self._parsed = True @@ -324,6 +347,16 @@ class Commandline(object): parsers=[ Parser("cron", help="Command that gets called by cron"), ]), + + # Shell + Parser("shell", + help="Go into a chroot shell", + arguments=[ + Option("nocleanafter", ["--no-clean-after"], help="Don't clean up the environment afterwards"), + Option("cleanbefore", ["--clean-before"], help="Clean up the environment before"), + Argument("package", help="Give the package name"), + List("args", help="Give some additional arguments to be executed"), + ]), ]) self.parser = parser diff --git a/tools/chroot-shell b/tools/chroot-shell new file mode 100755 index 000000000..cc8238f51 --- /dev/null +++ b/tools/chroot-shell @@ -0,0 +1,5 @@ +#!/bin/bash + +export PS1="naoki-chroot> " + +exec /bin/bash --login