Replace is_on_path by shutil.which.

parent ce8fdcbc
......@@ -17,12 +17,12 @@ https://gitlab.gnome.org/World/OpenPaperwork/pyocr#readme
import codecs
from io import BytesIO
import re
import shutil
import subprocess
import tempfile
from . import builders
from .error import CuneiformError
from . import util
# CHANGE THIS IF CUNEIFORM IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
......@@ -108,7 +108,7 @@ def image_to_string(image, lang=None, builder=None):
def is_available():
return util.is_on_path(CUNEIFORM_CMD)
return shutil.which(CUNEIFORM_CMD) is not None
def get_available_languages():
......
......@@ -18,14 +18,13 @@ https://gitlab.gnome.org/World/OpenPaperwork/pyocr#readme
import codecs
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import contextlib
import shutil
from . import builders
from . import util
from .builders import DigitBuilder # backward compatibility
from .error import TesseractError # backward compatibility
from .util import digits_only
......@@ -393,7 +392,7 @@ def image_to_string(image, lang=None, builder=None):
def is_available():
_set_environment()
return util.is_on_path(TESSERACT_CMD)
return shutil.which(TESSERACT_CMD) is not None
def get_available_languages():
......
import os
import re
......@@ -9,18 +7,3 @@ def digits_only(string):
if match:
return int(match.group('digits'))
return 0
def is_on_path(exec_name):
"""
Indicates if the command 'exec_name' appears to be installed.
Returns:
True --- if it is installed
False --- if it isn't
"""
for dirpath in os.environ["PATH"].split(os.pathsep):
path = os.path.join(dirpath, exec_name)
if os.path.exists(path) and os.access(path, os.X_OK):
return True
return False
......@@ -16,12 +16,12 @@ class TestCuneiform(BaseTest):
These tests make sure the requirements for the tests are met.
"""
@patch("pyocr.util.is_on_path")
def test_available(self, is_on_path):
@patch("shutil.which")
def test_available(self, which):
# XXX is it useful?
is_on_path.return_value = True
which.return_value = True
self.assertTrue(cuneiform.is_available())
is_on_path.assert_called_once_with("cuneiform")
which.assert_called_once_with("cuneiform")
@patch("subprocess.Popen")
def test_version(self, popen):
......
......@@ -32,11 +32,11 @@ class TestTesseract(BaseTest):
self.stdout.stdout.read.return_value = self.message.encode()
self.stdout.wait.return_value = 0
@patch("pyocr.util.is_on_path")
def test_available(self, is_on_path):
is_on_path.return_value = True
@patch("shutil.which")
def test_available(self, which):
which.return_value = True
self.assertTrue(tesseract.is_available())
is_on_path.assert_called_once_with("tesseract")
which.assert_called_once_with("tesseract")
@patch("subprocess.Popen")
def test_version_error(self, popen):
......
......@@ -6,7 +6,6 @@ import pyocr
from pyocr.util import (
digits_only,
is_on_path,
)
......@@ -14,10 +13,9 @@ class TestPyOCR(unittest.TestCase):
@patch("pyocr.libtesseract.tesseract_raw.g_libtesseract")
@patch("pyocr.libtesseract.tesseract_raw.is_available")
@patch("pyocr.util.is_on_path")
def test_available_tools_tesseract4(self, is_on_path,
is_available, libtess):
is_on_path.return_value = True
@patch("shutil.which")
def test_available_tools_tesseract4(self, which, is_available, libtess):
which.return_value = True
is_available.return_value = True
libtess.TessVersion.return_value = b"4.0.0"
self.assertListEqual(
......@@ -31,10 +29,9 @@ class TestPyOCR(unittest.TestCase):
@patch("pyocr.libtesseract.tesseract_raw.g_libtesseract")
@patch("pyocr.libtesseract.tesseract_raw.is_available")
@patch("pyocr.util.is_on_path")
def test_available_tools_tesseract3(self, is_on_path,
is_available, libtess):
is_on_path.return_value = True
@patch("shutil.which")
def test_available_tools_tesseract3(self, which, is_available, libtess):
which.return_value = True
is_available.return_value = True
libtess.TessVersion.return_value = b"3.5.0"
self.assertListEqual(
......@@ -48,10 +45,9 @@ class TestPyOCR(unittest.TestCase):
@patch("pyocr.libtesseract.tesseract_raw.g_libtesseract")
@patch("pyocr.libtesseract.tesseract_raw.is_available")
@patch("pyocr.util.is_on_path")
def test_available_tools_tesseract3_0(self, is_on_path,
is_available, libtess):
is_on_path.return_value = True
@patch("shutil.which")
def test_available_tools_tesseract3_0(self, which, is_available, libtess):
which.return_value = True
is_available.return_value = True
libtess.TessVersion.return_value = b"3.0.0"
self.assertListEqual(
......@@ -70,9 +66,3 @@ class TestPyOCR(unittest.TestCase):
self.assertEqual(digits_only("v42"), 42)
self.assertEqual(digits_only("v42x35"), 42)
self.assertEqual(digits_only("v42x35qsdf"), 42)
def test_is_on_path(self):
self.assertTrue(any((is_on_path("python"), is_on_path("python2"),
is_on_path("python3"))))
# let's hope nobody is crazy enough to name an executable like this
self.assertFalse(is_on_path("windows95"))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment