Skip to content

Commit 666c004

Browse files
dcermakphilpep
authored andcommitted
Fallback to which when command -v fails
Older versions of busybox do not support command -v, but they have which => fallback to using which if command does not exist.
1 parent 36ee72c commit 666c004

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

testinfra/host.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ def __repr__(self):
2929

3030
def exists(self, command):
3131
"""Return True if given command exist in $PATH"""
32-
return self.run_expect([0, 1, 127], "command -v %s", command).rc == 0
32+
rc = self.run_expect([0, 1, 127], "command -v %s", command).rc
33+
if rc == 127:
34+
return self.run_expect([0, 1], "which %s", command).rc == 0
35+
else:
36+
return rc == 0
3337

3438
def find_command(self, command, extrapaths=("/sbin", "/usr/sbin")):
3539
"""Return path of given command
@@ -39,6 +43,10 @@ def find_command(self, command, extrapaths=("/sbin", "/usr/sbin")):
3943
out = self.run_expect([0, 1, 127], "command -v %s", command)
4044
if out.rc == 0:
4145
return out.stdout.rstrip("\r\n")
46+
if out.rc == 127:
47+
out = self.run_expect([0, 1], "which %s", self.path)
48+
if out.rc == 0:
49+
return out.stdout.rstrip("\r\n")
4250
for basedir in extrapaths:
4351
path = os.path.join(basedir, command)
4452
if self.exists(path):

0 commit comments

Comments
 (0)