Skip to content

Commit

Permalink
Add is_exec_found to utils and use it in dagascii module
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymoteusz Jankowski committed Dec 1, 2019
1 parent c17cf2b commit cade3b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 2 additions & 4 deletions dvc/dagascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import pydoc
import sys
from distutils.spawn import find_executable

from grandalf.graphs import Edge
from grandalf.graphs import Graph
Expand All @@ -16,7 +15,7 @@
from grandalf.routing import EdgeViewer
from grandalf.routing import route_with_lines

from dvc.utils import boxify
from dvc.utils import boxify, is_exec_found


logger = logging.getLogger(__name__)
Expand All @@ -39,8 +38,7 @@

def find_pager(pager_cmd, pager_cmd_with_format, pager_env_name):
if sys.stdout.isatty():
less_found = find_executable(pager_cmd) is not None
if less_found:
if is_exec_found(pager_cmd):
pager_cmd = os.getenv(pager_env_name, pager_cmd_with_format)

def less_pager(text):
Expand Down
5 changes: 5 additions & 0 deletions dvc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,8 @@ def resolve_output(inp, out):
if os.path.isdir(out):
return os.path.join(out, name)
return out


def is_exec_found(exec_name):
cmd = '({}) 2>/dev/null'.format(exec_name)
return hasattr(os, 'system') and os.system(cmd) == 0
10 changes: 10 additions & 0 deletions tests/func/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,13 @@ def test_makedirs_permissions(tmpdir):

assert stat.S_IMODE(os.stat(test_dir).st_mode) == dir_mode
assert stat.S_IMODE(os.stat(intermediate_dir).st_mode) == dir_mode


def test_is_exec_found_returns_true_when_program_exists():
result = utils.is_exec_found("python")
assert result == True


def test_is_exec_found_returns_false_when_program_is_missing():
result = utils.is_exec_found("some-missing-program")
assert result == False

0 comments on commit cade3b3

Please sign in to comment.