# Path: problemtools/run/executable.py
# class Executable(Program):
#     """Class for executable files.
#     """
#     def __init__(self, path, args=None):
#         """Instantiate executable object.
# 
#         Args:
#             path (str): path to the executable file.  Must be a file,
#                 and must be executable.
#             args: list of additional command line arguments that
#                 should be passed to the program every time it is executed.
#         """
#         if not os.path.isfile(path) or not os.access(path, os.X_OK):
#             raise ProgramError('%s is not an executable program' % path)
#         self.path = path
#         self.args = args if args is not None else []
# 
#     def __str__(self):
#         """String representation"""
#         return '%s' % (self.path)
# 
#     def compile(self):
#         """Dummy implementation of the compile method -- nothing to check!
#         """
#         return (True, None)
# 
#     def get_runcmd(self, cwd=None, memlim=None):
#         """Command to run the program.
#         """
#         return [self.path] + self.args
# 
#     def should_skip_memory_rlimit(self):
#         """Ugly hack (see program.py for details)."""
#         return True


import os
from .executable import Executable

def get_tool_path(name):
    """Find the path to one of problemtools' external tools.

    Args:
        name (str): which tool is wanted (one of [default_grader,
            default_validator, interactive, checktestdata, viva.sh])

    Returns:
        str, path to the tool, or None if the tool was not found.
    """
    return __locate_executable([os.path.join(os.path.dirname(__file__),
                                             '..', 'support', name),
                                os.path.join(os.path.dirname(__file__),
                                             '..', '..', 'support',
                                             os.path.splitext(name)[0], name)])


def get_tool(name):
    """Get an Executable instance for one of problemtools' external tools.

    Args:
        name(str): same as for get_tool_path

    Returns:
        problemtools.run.Executable object for the tool, or None if
        the tool was not found.
    """
    path = get_tool_path(name)

# Path: problemtools/run/executable.py
# class Executable(Program):
#     """Class for executable files.
#     """
#     def __init__(self, path, args=None):
#         """Instantiate executable object.
# 
#         Args:
#             path (str): path to the executable file.  Must be a file,
#                 and must be executable.
#             args: list of additional command line arguments that
#                 should be passed to the program every time it is executed.
#         """
#         if not os.path.isfile(path) or not os.access(path, os.X_OK):
#             raise ProgramError('%s is not an executable program' % path)
#         self.path = path
#         self.args = args if args is not None else []
# 
#     def __str__(self):
#         """String representation"""
#         return '%s' % (self.path)
# 
#     def compile(self):
#         """Dummy implementation of the compile method -- nothing to check!
#         """
#         return (True, None)
# 
#     def get_runcmd(self, cwd=None, memlim=None):
#         """Command to run the program.
#         """
#         return [self.path] + self.args
# 
#     def should_skip_memory_rlimit(self):
#         """Ugly hack (see program.py for details)."""
#         return True


# Path: problemtools/run/tools.py
import os
from .executable import Executable


def get_tool_path(name):
    """Find the path to one of problemtools' external tools.

    Args:
        name (str): which tool is wanted (one of [default_grader,
            default_validator, interactive, checktestdata, viva.sh])

    Returns:
        str, path to the tool, or None if the tool was not found.
    """
    return __locate_executable([os.path.join(os.path.dirname(__file__),
                                             '..', 'support', name),
                                os.path.join(os.path.dirname(__file__),
                                             '..', '..', 'support',
                                             os.path.splitext(name)[0], name)])


def get_tool(name):
    """Get an Executable instance for one of problemtools' external tools.

    Args:
        name(str): same as for get_tool_path

    Returns:
        problemtools.run.Executable object for the tool, or None if
        the tool was not found.
    """
    path = get_tool_path(name)

    return Executable(path) if path is not None else None