This patch allows you to set the current working directory (in the
chroot) before running a command with --chroot. This avoids the need to
pass shell snippets ('cd /some/path && /run/cmd') to mock when running a
command that expects to executed from a certain directory. It's useful
when using --copyin to setup the environment before running a command.
>From e4071d1d41a62ccf4461dfab958f9325edf30c97 Mon Sep 17 00:00:00 2001
From: Mike Bonnet <mikeb(a)redhat.com>
Date: Thu, 24 Jan 2008 17:09:06 -0500
Subject: [PATCH] optionally set the current working directory (in the chroot) before running command with --chroot
---
docs/mock.1 | 3 +++
py/mock.py | 8 ++++++--
py/mock/util.py | 13 +++++++++----
3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/docs/mock.1 b/docs/mock.1
index 38c3233..531d117 100644
--- a/docs/mock.1
+++ b/docs/mock.1
@@ -140,6 +140,9 @@ Fail build if rpmbuild takes longer than 'timeout' seconds
\fB\-\-unpriv\fR
Drop privileges before running command when using --chroot
.TP
+\fB\-\-cwd=\fR\fIDIR\fP
+Change to the specified directory (relative to the chroot) before running command when using --chroot
+.TP
\fB\-q\fR, \fB\-\-quiet\fR
Be quiet.
.TP
diff --git a/py/mock.py b/py/mock.py
index f422a33..d5afbbe 100755
--- a/py/mock.py
+++ b/py/mock.py
@@ -152,6 +152,10 @@ def command_parse(config_opts):
" seconds ")
parser.add_option("--unpriv", action="store_true", default=False,
help="Drop privileges before running command when using --chroot")
+ parser.add_option("--cwd", action="store", default=None,
+ metavar="DIR",
+ help="Change to the specified directory (relative to the chroot)"
+ " before running command when using --chroot")
# verbosity
parser.add_option("-v", "--verbose", action="store_const", const=2,
@@ -536,9 +540,9 @@ def main(ret):
chroot._mountall()
if options.unpriv:
chroot.doChroot(args, shell=shell,
- uid=chroot.chrootuid, gid=chroot.chrootgid)
+ uid=chroot.chrootuid, gid=chroot.chrootgid, cwd=options.cwd)
else:
- chroot.doChroot(args, shell=shell)
+ chroot.doChroot(args, shell=shell, cwd=options.cwd)
finally:
chroot._umountall()
diff --git a/py/mock/util.py b/py/mock/util.py
index f93f98b..65cc995 100644
--- a/py/mock/util.py
+++ b/py/mock/util.py
@@ -201,6 +201,10 @@ def condChroot(chrootPath):
os.chroot(chrootPath)
uid.setresuid(saved['ruid'], saved['euid'])
+def condChdir(cwd):
+ if cwd is not None:
+ os.chdir(cwd)
+
def condDropPrivs(uid, gid):
if gid is not None:
os.setregid(gid, gid)
@@ -245,12 +249,12 @@ def logOutput(fds, logger, returnOutput=1, start=0, timeout=0):
# The "Not-as-complicated" version
#
decorate(traceLog())
-def do(command, shell=False, chrootPath=None, timeout=0, raiseExc=True, returnOutput=0, uid=None, gid=None, personality=None, *args, **kargs):
+def do(command, shell=False, chrootPath=None, cwd=None, timeout=0, raiseExc=True, returnOutput=0, uid=None, gid=None, personality=None, *args, **kargs):
logger = kargs.get("logger", getLog())
output = ""
start = time.time()
- preexec = ChildPreExec(personality, chrootPath, uid, gid)
+ preexec = ChildPreExec(personality, chrootPath, cwd, uid, gid)
try:
child = None
logger.debug("Executing command: %s" % command)
@@ -292,9 +296,10 @@ def do(command, shell=False, chrootPath=None, timeout=0, raiseExc=True, returnOu
return output
class ChildPreExec(object):
- def __init__(self, personality, chrootPath, uid, gid):
+ def __init__(self, personality, chrootPath, cwd, uid, gid):
self.personality = personality
self.chrootPath = chrootPath
+ self.cwd = cwd
self.uid = uid
self.gid = gid
@@ -303,4 +308,4 @@ class ChildPreExec(object):
condPersonality(self.personality)
condChroot(self.chrootPath)
condDropPrivs(self.uid, self.gid)
-
+ condChdir(self.cwd)
--
1.5.3.3