# Buggy code
def createBatchSystem(config, jobStore=None, userScript=None):
    """
    Creates an instance of the batch system specified in the given config. If a job store and
    a user script are given then the user script can be hot deployed into the workflow.

    :param toil.common.Config config: the current configuration
    :param jobStores.abstractJobStore.AbstractJobStore jobStore: an instance of a jobStore
    :param ModuleDescriptor userScript: a user supplied script to use for hot development
    :return: an instance of a concrete subclass of AbstractBatchSystem
    :rtype: batchSystems.abstractBatchSystem.AbstractBatchSystem
    """
    kwargs = dict(
        config=config,
        maxCores=config.maxCores,
        maxMemory=config.maxMemory,
        maxDisk=config.maxDisk,
    )

    if config.batchSystem == "parasol":
        from toil.batchSystems.parasol import ParasolBatchSystem

        batchSystemClass = ParasolBatchSystem

    elif (
        config.batchSystem == "single_machine" or config.batchSystem == "singleMachine"
    ):
        from toil.batchSystems.singleMachine import SingleMachineBatchSystem

        batchSystemClass = SingleMachineBatchSystem

    elif config.batchSystem == "gridengine" or config.batchSystem == "gridEngine":
        from toil.batchSystems.gridengine import GridengineBatchSystem

        batchSystemClass = GridengineBatchSystem

    elif config.batchSystem == "lsf" or config.batchSystem == "LSF":
        from toil.batchSystems.lsf import LSFBatchSystem

        batchSystemClass = LSFBatchSystem

    elif config.batchSystem == "mesos" or config.batchSystem == "Mesos":
        from toil.batchSystems.mesos.batchSystem import MesosBatchSystem

        batchSystemClass = MesosBatchSystem

        kwargs["masterAddress"] = config.mesosMasterAddress

    elif config.batchSystem == "slurm" or config.batchSystem == "Slurm":
        from toil.batchSystems.slurm import SlurmBatchSystem

        batchSystemClass = SlurmBatchSystem

    else:
        raise RuntimeError("Unrecognised batch system: %s" % config.batchSystem)

    if not batchSystemClass.supportsWorkerCleanup():
        raise RuntimeError(
            "%s currently does not support shared caching.  Use Toil version "
            "3.1.6 along with the --disableSharedCache option if you want to "
            "use this batch system." % config.batchSystem
        )
    logger.info(
        "Using the %s"
        % re.sub("([a-z])([A-Z])", "\g<1> \g<2>", batchSystemClass.__name__).lower()
    )

    if jobStore is not None and userScript is not None:
        if not userScript.belongsToToil and batchSystemClass.supportsHotDeployment():
            kwargs["userScript"] = userScript.saveAsResourceTo(jobStore)

    return batchSystemClass(**kwargs)


# Traceback (ImportError)
WARNING:toil.leader:The jobWrapper seems to have left a log file, indicating failure: 52d0d400_4080_480d_9c53_e7a7624b20e9
WARNING:toil.leader:Reporting file: 52d0d400_4080_480d_9c53_e7a7624b20e9
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       ---TOIL WORKER OUTPUT LOG---
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       WARNING:toil.resource:Can't find resource for leader path '/home/anovak/hgvm-graph-bakeoff-evaluations/scripts'
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       WARNING:toil.resource:Can't localize module ModuleDescriptor(dirPath='/home/anovak/hgvm-graph-bakeoff-evaluations/scripts', name='parallelMappingEvaluation')
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       ERROR:toil.job:Failed to import user module ModuleDescriptor(dirPath='/home/anovak/hgvm-graph-bakeoff-evaluations/scripts', name='parallelMappingEvaluation') from sys.path=['/usr/local/bin', '/usr/local/lib/python2.7/dist-packages/pip-8.1.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/mesos.interface-0.22.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/mesos-0.22.0-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/home/anovak/hgvm-graph-bakeoff-evaluations/scripts']
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       Traceback (most recent call last):
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:         File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line 316, in main
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:           job = Job._loadJob(jobWrapper.command, jobStore)
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 666, in _loadJob
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:           userModule = cls._loadUserModule(userModule)
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 640, in _loadUserModule
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:           return importlib.import_module(userModule.name)
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:         File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:           __import__(name)
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       ImportError: No module named parallelMappingEvaluation
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       Exiting the worker because of a failed jobWrapper on host amn2agent3
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host amn2agent3
WARNING:toil.leader:52d0d400_4080_480d_9c53_e7a7624b20e9:       WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job 52d0d400_4080_480d_9c53_e7a7624b20e9 to 0
WARNING:toil.leader:Job: 52d0d400_4080_480d_9c53_e7a7624b20e9 is completely failed

# Fixed code
def createBatchSystem(config, jobStore=None, userScript=None):
    """
    Creates an instance of the batch system specified in the given config. If a job store and
    a user script are given then the user script can be hot deployed into the workflow.

    :param toil.common.Config config: the current configuration

    :param toil.jobStores.abstractJobStore.AbstractJobStore jobStore: an instance of a jobStore

    :param ModuleDescriptor userScript: a handle to the Python module defining the root job

    :return: an instance of a concrete subclass of AbstractBatchSystem

    :rtype: batchSystems.abstractBatchSystem.AbstractBatchSystem
    """
    kwargs = dict(
        config=config,
        maxCores=config.maxCores,
        maxMemory=config.maxMemory,
        maxDisk=config.maxDisk,
    )

    if config.batchSystem == "parasol":
        from toil.batchSystems.parasol import ParasolBatchSystem

        batchSystemClass = ParasolBatchSystem

    elif (
        config.batchSystem == "single_machine" or config.batchSystem == "singleMachine"
    ):
        from toil.batchSystems.singleMachine import SingleMachineBatchSystem

        batchSystemClass = SingleMachineBatchSystem

    elif config.batchSystem == "gridengine" or config.batchSystem == "gridEngine":
        from toil.batchSystems.gridengine import GridengineBatchSystem

        batchSystemClass = GridengineBatchSystem

    elif config.batchSystem == "lsf" or config.batchSystem == "LSF":
        from toil.batchSystems.lsf import LSFBatchSystem

        batchSystemClass = LSFBatchSystem

    elif config.batchSystem == "mesos" or config.batchSystem == "Mesos":
        from toil.batchSystems.mesos.batchSystem import MesosBatchSystem

        batchSystemClass = MesosBatchSystem

        kwargs["masterAddress"] = config.mesosMasterAddress

    elif config.batchSystem == "slurm" or config.batchSystem == "Slurm":
        from toil.batchSystems.slurm import SlurmBatchSystem

        batchSystemClass = SlurmBatchSystem

    else:
        raise RuntimeError("Unrecognised batch system: %s" % config.batchSystem)

    if not batchSystemClass.supportsWorkerCleanup():
        raise RuntimeError(
            "%s currently does not support shared caching.  Use Toil version "
            "3.1.6 along with the --disableSharedCache option if you want to "
            "use this batch system." % config.batchSystem
        )
    logger.info(
        "Using the %s"
        % re.sub("([a-z])([A-Z])", "\g<1> \g<2>", batchSystemClass.__name__).lower()
    )

    if jobStore is not None:
        if userScript is not None:
            if (
                not userScript.belongsToToil
                and batchSystemClass.supportsHotDeployment()
            ):
                userScriptResource = userScript.saveAsResourceTo(jobStore)
                with jobStore.writeSharedFileStream("userScript") as f:
                    f.write(userScriptResource.pickle())
                kwargs["userScript"] = userScriptResource
        else:
            from toil.jobStores.abstractJobStore import NoSuchFileException

            try:
                with jobStore.readSharedFileStream("userScript") as f:
                    userScriptResource = Resource.unpickle(f.read())
                kwargs["userScript"] = userScriptResource
            except NoSuchFileException:
                pass

    return batchSystemClass(**kwargs)