]> git.ipfire.org Git - ipfire-3.x.git/commitdiff
naoki: Add shell command.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2010 14:06:37 +0000 (15:06 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2010 14:06:37 +0000 (15:06 +0100)
naoki/__init__.py
naoki/chroot.py
naoki/terminal.py
tools/chroot-shell [new file with mode: 0755]

index 23e54854dc373e7f81487ebcdcfeacfabb63b31a..04e9d88e28de825eb2049cf31f6a24fe9b08ba9c 100644 (file)
@@ -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)
index efcaf074640e8f1f9f93aa3a7621d5ea6f889ec7..eec4746804ffeedd441ae912cc262a141036d95d 100644 (file)
@@ -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):
index c979cbd4e1d8ec38a3589250bc3dc9edf8b360b6..bb1cd60a100306f8787b190dcc809e151a331e3f 100644 (file)
@@ -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 (executable)
index 0000000..cc8238f
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+export PS1="naoki-chroot> "
+
+exec /bin/bash --login