summaryrefslogtreecommitdiffstats
path: root/pyfirstaidkit/utils.py
blob: 589398521381aeff0584e87d969130988ca35e00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# First Aid Kit - diagnostic and repair tool for Linux
# Copyright (C) 2007 Martin Sivak <msivak@redhat.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import os
import sys
import subprocess

def chroot_func(dir):
    def do_chroot():
        return os.chroot(dir)

    if os.path.abspath(dir)=="/":
        return lambda: True
    else:
        return do_chroot

def spawnvch(executable, args, chroot, env = None):
    """Use Popen to launch program in chroot
 executable - path to binary to execute (in chroot!)
 args - it's parameters
 chroot - directory to chroot to

Returns the subprocess.Popen object"""

    return subprocess.Popen(executable = executable, args = args, preexec_fn = chroot_func(chroot), env = env,
            stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)