Add is_exec_found to utils and use it in dagascii module

This commit is contained in:
Tymoteusz Jankowski
2019-12-01 19:04:04 +01:00
parent c17cf2b2f7
commit cade3b396c
3 changed files with 17 additions and 4 deletions
+2 -4
View File
@@ -7,7 +7,6 @@ import math
import os
import pydoc
import sys
from distutils.spawn import find_executable
from grandalf.graphs import Edge
from grandalf.graphs import Graph
@@ -16,7 +15,7 @@ from grandalf.layouts import SugiyamaLayout
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__)
@@ -39,8 +38,7 @@ Also, you can override default pager via {} env. var.
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):
+5
View File
@@ -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
View File
@@ -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