Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Use find_executable for gs and raise error if not found #166

Merged
merged 5 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions camelot/parsers/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,39 @@ def _copy_spanning_text(t, copy_text=None):
return t

def _generate_image(self):
# TODO: hacky, get rid of ghostscript #96
def get_platform():
# TODO: get rid of ghostscript #96
def get_executable():
import platform

info = {
'system': platform.system().lower(),
'machine': platform.machine().lower()
}
return info
from distutils.spawn import find_executable

class GhostscriptNotFound(Exception): pass

gs = None
system = platform.system().lower()
try:
if system == 'windows':
if find_executable('gswin32c.exe'):
gs = 'gswin32c.exe'
elif find_executable('gswin64c.exe'):
gs = 'gswin64c.exe'
else:
raise ValueError
else:
if find_executable('gs'):
gs = 'gs'
elif find_executable('gsc'):
gs = 'gsc'
else:
raise ValueError
if 'ghostscript' not in subprocess.check_output(
[gs, '-version']).decode('utf-8').lower():
raise ValueError
except ValueError:
raise GhostscriptNotFound(
'Please make sure that Ghostscript is installed'
' and available on the PATH environment variable')

return gs

self.imagename = ''.join([self.rootname, '.png'])
gs_call = [
Expand All @@ -193,15 +217,9 @@ def get_platform():
'-r600',
self.filename
]
info = get_platform()
if info['system'] == 'windows':
bit = info['machine'][-2:]
gs_call.insert(0, 'gswin{}c.exe'.format(bit))
else:
if 'ghostscript' in subprocess.check_output(['gs', '-version']).decode('utf-8').lower():
gs_call.insert(0, 'gs')
else:
gs_call.insert(0, "gsc")
gs = get_executable()
gs_call.insert(0, gs)

subprocess.call(
gs_call, stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ def test_no_tables_found_warnings_suppressed():
except Warning as e:
warning_text = str(e)
pytest.fail('Unexpected warning: {}'.format(warning_text))


def test_ghostscript_not_found(monkeypatch):
import distutils

def _find_executable_patch(arg):
return ''

monkeypatch.setattr(distutils.spawn, 'find_executable', _find_executable_patch)

message = ('Please make sure that Ghostscript is installed and available'
' on the PATH environment variable')
filename = os.path.join(testdir, 'foo.pdf')
with pytest.raises(Exception, message=message):
tables = camelot.read_pdf(filename)