# Path: src/sii/lib/lib/shell.py
# def which(program, fail=True):
#     """ Sort of replicates the `which` utility.
#     """
#     is_exe    = lambda fp: os.path.isfile(fp) and os.access(fp, os.X_OK)
#     locations = [os.path.join(path, program) for path in os.environ["PATH"].split(os.pathsep)]
#     found     = [loc for loc in locations if is_exe(loc)]
# 
#     if not found:
#         if not fail:
#             return False
#         else:
#             raise RuntimeError("Did not find program: <{0}>".format(program))
#     elif len(found) > 1:
#         if not fail:
#             return False
#         else:
#             raise RuntimeError("Found more than one instance of the program:\n"
#                                "{0}".format('\n'.join(found)))
#     else:
#         return found[0]
# 
# class cd(object):
#     """ Directory Context Switcher.
# 
#     Switches directory within the guards, switching back when leaving them.
# 
#     [NOT THREAD SAFE]
#     If multiple threads switch around it looses its way back to the original
#     working directory. A possible way would be to take into consideration in
#     which thread it is currently being called.
#     """
#     def __init__(self, dir):
#         self.dir    = dir
#         self.olddir = None
# 
#     def __enter__(self):
#         self._olddir = os.getcwd()
#         os.chdir(self.dir)
# 
#     def __exit__(self, etype, evalue, etraceback):
#         if any([etype, evalue, etraceback]):
#             traceback.print_exception(etype, evalue, etraceback)
#         os.chdir(self._olddir)


import os
import re
import tempfile
import subprocess
from .shell import which, cd
    def executable(self):
        """ Path to the Executable. """
        raise NotImplementedError

    def check(self):
        """ Check if the Executable can be found and run. This is the point we have to
        fail if the program can not be found and/or is presumably not installed on the system.
        """
        raise NotImplementedError

    def call(self):
        """ Call the executable to do something. Here the parameters and behaviours are
        implementation specific.

        NOTE: This function should default to hiding stdout and stderr from the callable.
        """
        raise NotImplementedError


class Ghostscript(SystemCall):

    @property
    def name(self):
        return 'ghostscript'

    @property
    def executable(self):
        return which(self.name)

    def check(self):

# Path: src/sii/lib/lib/shell.py
# def which(program, fail=True):
#     """ Sort of replicates the `which` utility.
#     """
#     is_exe    = lambda fp: os.path.isfile(fp) and os.access(fp, os.X_OK)
#     locations = [os.path.join(path, program) for path in os.environ["PATH"].split(os.pathsep)]
#     found     = [loc for loc in locations if is_exe(loc)]
# 
#     if not found:
#         if not fail:
#             return False
#         else:
#             raise RuntimeError("Did not find program: <{0}>".format(program))
#     elif len(found) > 1:
#         if not fail:
#             return False
#         else:
#             raise RuntimeError("Found more than one instance of the program:\n"
#                                "{0}".format('\n'.join(found)))
#     else:
#         return found[0]
# 
# class cd(object):
#     """ Directory Context Switcher.
# 
#     Switches directory within the guards, switching back when leaving them.
# 
#     [NOT THREAD SAFE]
#     If multiple threads switch around it looses its way back to the original
#     working directory. A possible way would be to take into consideration in
#     which thread it is currently being called.
#     """
#     def __init__(self, dir):
#         self.dir    = dir
#         self.olddir = None
# 
#     def __enter__(self):
#         self._olddir = os.getcwd()
#         os.chdir(self.dir)
# 
#     def __exit__(self, etype, evalue, etraceback):
#         if any([etype, evalue, etraceback]):
#             traceback.print_exception(etype, evalue, etraceback)
#         os.chdir(self._olddir)


# Path: src/sii/lib/lib/syscall.py
import os
import re
import tempfile
import subprocess
from .shell import which, cd

    def executable(self):
        """ Path to the Executable. """
        raise NotImplementedError

    def check(self):
        """ Check if the Executable can be found and run. This is the point we have to
        fail if the program can not be found and/or is presumably not installed on the system.
        """
        raise NotImplementedError

    def call(self):
        """ Call the executable to do something. Here the parameters and behaviours are
        implementation specific.

        NOTE: This function should default to hiding stdout and stderr from the callable.
        """
        raise NotImplementedError


class Ghostscript(SystemCall):

    @property
    def name(self):
        return 'ghostscript'

    @property
    def executable(self):
        return which(self.name)

    def check(self):

        exe = which(self.name, fail=False)