# Path: sumatra/core.py
# def run(args, cwd=None, shell=False, kill_tree=True, timeout=-1, env=None):
#     """
#     Run a command with a timeout after which it will be forcibly
#     killed.
# 
#     Based on http://stackoverflow.com/a/3326559
#     """
#     class Alarm(Exception):
#         pass
# 
#     def alarm_handler(signum, frame):
#         raise Alarm
#     p = subprocess.Popen(args, shell=shell, cwd=cwd, stdout=subprocess.PIPE,
#                          stderr=subprocess.PIPE, env=env)
#     if timeout != -1:
#         signal.signal(signal.SIGALRM, alarm_handler)
#         signal.alarm(timeout)
#     try:
#         stdout, stderr = p.communicate()
#         stdout = stdout.decode(get_encoding())
#         stderr = stderr.decode(get_encoding())
#         if timeout != -1:
#             signal.alarm(0)
#     except Alarm:
#         pids = [p.pid]
#         if kill_tree:
#             pids.extend(_get_process_children(p.pid))
#         for pid in pids:
#             # process might have died before getting to this line
#             # so wrap to avoid OSError: no such process
#             try:
#                 os.kill(pid, signal.SIGKILL)
#             except OSError:
#                 pass
#         return -9, '', ''
#     return p.returncode, str(stdout), str(stderr)
# 
# def component(cls):
#     """Class decorator to define Sumatra components.
# 
#     Use this decorator to declare a class as a Sumatra component. It can then be used
#     by Sumatra.
#     """
#     _Registry().register(cls)
#     return cls
# 
# def component_type(cls):
#     """Class decorator to define base types for components.
# 
#     Use this decorator to register a base type of a Sumatra component. Concrete components
#     should subclass from this component type to then be registered as a Sumatra component.
# 
#     Not to be used when writing Sumatra plug-ins! For that, please use the `component`
#     decorator.
# 
#     Example:
#     @component_type
#     class MyComponentBase(object):
#         pass
# 
#     @component
#     class MyConcreteComponent(MyComponentBase):
#         pass
#     """
#     for base_type in _Registry().components:
#         if issubclass(cls, base_type):
#             msg = "{cls} is a subclass of already registered component type {base} and " \
#                   "hence could not be registered as component type." \
#                   .format(**{'cls': cls, 'base': base_type})
#             raise TypeError(msg)
#     _Registry().add_component_type(cls)
#     return cls
# 
# def get_registered_components(base_type):
#     """Returns all registered components for the given component base type."""
#     return _Registry().components[base_type]


from builtins import object
from .core import run, component, component_type, get_registered_components
import os.path
import re
import sys
import warnings
        with open("genesis_version.g", "w") as fd:
            fd.write("""openfile genesis_version.out w
                        writefile genesis_version.out {version}
                        closefile genesis_version.out
                        quit
                    """)
        returncode, output, err = run("%s genesis_version.g" % self.path, shell=True)
        with open("genesis_version.out") as fd:
            version = fd.read()
        os.remove("genesis_version.g")
        os.remove("genesis_version.out")
        return version.strip()


def get_executable(path=None, script_file=None):
    """
    Given the path to an executable, determine what program it is, if possible.
    Given the name of a script file, try to infer the program that runs that
    script.
    Return an appropriate subclass of Executable
    """
    if path:
        prog_name = os.path.basename(path)
        program = Executable(path)
        for executable_type in get_registered_components(Executable).values():
            if prog_name in executable_type.executable_names:
                program = executable_type(path)
    elif script_file:
        script_path, ext = os.path.splitext(script_file)
        program = None

# Path: sumatra/core.py
# def run(args, cwd=None, shell=False, kill_tree=True, timeout=-1, env=None):
#     """
#     Run a command with a timeout after which it will be forcibly
#     killed.
# 
#     Based on http://stackoverflow.com/a/3326559
#     """
#     class Alarm(Exception):
#         pass
# 
#     def alarm_handler(signum, frame):
#         raise Alarm
#     p = subprocess.Popen(args, shell=shell, cwd=cwd, stdout=subprocess.PIPE,
#                          stderr=subprocess.PIPE, env=env)
#     if timeout != -1:
#         signal.signal(signal.SIGALRM, alarm_handler)
#         signal.alarm(timeout)
#     try:
#         stdout, stderr = p.communicate()
#         stdout = stdout.decode(get_encoding())
#         stderr = stderr.decode(get_encoding())
#         if timeout != -1:
#             signal.alarm(0)
#     except Alarm:
#         pids = [p.pid]
#         if kill_tree:
#             pids.extend(_get_process_children(p.pid))
#         for pid in pids:
#             # process might have died before getting to this line
#             # so wrap to avoid OSError: no such process
#             try:
#                 os.kill(pid, signal.SIGKILL)
#             except OSError:
#                 pass
#         return -9, '', ''
#     return p.returncode, str(stdout), str(stderr)
# 
# def component(cls):
#     """Class decorator to define Sumatra components.
# 
#     Use this decorator to declare a class as a Sumatra component. It can then be used
#     by Sumatra.
#     """
#     _Registry().register(cls)
#     return cls
# 
# def component_type(cls):
#     """Class decorator to define base types for components.
# 
#     Use this decorator to register a base type of a Sumatra component. Concrete components
#     should subclass from this component type to then be registered as a Sumatra component.
# 
#     Not to be used when writing Sumatra plug-ins! For that, please use the `component`
#     decorator.
# 
#     Example:
#     @component_type
#     class MyComponentBase(object):
#         pass
# 
#     @component
#     class MyConcreteComponent(MyComponentBase):
#         pass
#     """
#     for base_type in _Registry().components:
#         if issubclass(cls, base_type):
#             msg = "{cls} is a subclass of already registered component type {base} and " \
#                   "hence could not be registered as component type." \
#                   .format(**{'cls': cls, 'base': base_type})
#             raise TypeError(msg)
#     _Registry().add_component_type(cls)
#     return cls
# 
# def get_registered_components(base_type):
#     """Returns all registered components for the given component base type."""
#     return _Registry().components[base_type]


# Path: sumatra/programs.py
from builtins import object
from .core import run, component, component_type, get_registered_components
import os.path
import re
import sys
import warnings

        with open("genesis_version.g", "w") as fd:
            fd.write("""openfile genesis_version.out w
                        writefile genesis_version.out {version}
                        closefile genesis_version.out
                        quit
                    """)
        returncode, output, err = run("%s genesis_version.g" % self.path, shell=True)
        with open("genesis_version.out") as fd:
            version = fd.read()
        os.remove("genesis_version.g")
        os.remove("genesis_version.out")
        return version.strip()


def get_executable(path=None, script_file=None):
    """
    Given the path to an executable, determine what program it is, if possible.
    Given the name of a script file, try to infer the program that runs that
    script.
    Return an appropriate subclass of Executable
    """
    if path:
        prog_name = os.path.basename(path)
        program = Executable(path)
        for executable_type in get_registered_components(Executable).values():
            if prog_name in executable_type.executable_names:
                program = executable_type(path)
    elif script_file:
        script_path, ext = os.path.splitext(script_file)
        program = None

        for executable_type in get_registered_components(Executable).values():