# 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 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 config.disableCaching and not batchSystemClass.supportsWorkerCleanup():
        raise RuntimeError(
            "%s currently does not support shared caching.  Set the "
            "--disableCaching flag 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)


# Traceback (ImportError)
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   ---TOIL WORKER OUTPUT LOG---
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   Next available file descriptor: 10
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:toil.worker:Next available file descriptor: 10
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:urllib3.connectionpool:Setting read timeout to 65
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:urllib3.connectionpool:"GET /acmarkellotoilvgjobstore11xxjobitems(PartitionKey='default',RowKey='fc48619f_e141_4211_8ab9_3004f95b16df')?$select= HTTP/1.1" 200 None
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   Parsed jobWrapper
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:toil.worker:Parsed jobWrapper
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   Got a command to run: _toil b5feca2d_3cbf_46a6_81cc_43d5f3a43cb6 /home/cmarkello/toilvenv/local/lib/python2.7/site-packages toil_vg.vg_evaluation_pipeline
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:toil.worker:Got a command to run: _toil b5feca2d_3cbf_46a6_81cc_43d5f3a43cb6 /home/cmarkello/toilvenv/local/lib/python2.7/site-packages toil_vg.vg_evaluation_pipeline
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:toil.job:Loading user module ModuleDescriptor(dirPath='/home/cmarkello/toilvenv/local/lib/python2.7/site-packages', name='toil_vg.vg_evaluation_pipeline').
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   WARNING:toil.resource:Can't find resource for leader path '/home/cmarkello/toilvenv/local/lib/python2.7/site-packages/toil_vg'
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   WARNING:toil.resource:Can't localize module ModuleDescriptor(dirPath='/home/cmarkello/toilvenv/local/lib/python2.7/site-packages', name='toil_vg.vg_evaluation_pipeline')
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   ERROR:toil.job:Failed to import user module ModuleDescriptor(dirPath='/home/cmarkello/toilvenv/local/lib/python2.7/site-packages', name='toil_vg.vg_evaluation_pipeline') from sys.path=['/home/cmarkello/toilvenv/bin', '/home/cmarkello/toilvenv/lib/python2.7', '/home/cmarkello/toilvenv/lib/python2.7/plat-x86_64-linux-gnu', '/home/cmarkello/toilvenv/lib/python2.7/lib-tk', '/home/cmarkello/toilvenv/lib/python2.7/lib-old', '/home/cmarkello/toilvenv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/cmarkello/toilvenv/local/lib/python2.7/site-packages', '/home/cmarkello/toil/src', '/home/cmarkello/toilvenv/lib/python2.7/site-packages', '/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', '/home/cmarkello/toil-scripts/src', '/usr/local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/dist-packages']
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   Traceback (most recent call last):
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:     File "/home/cmarkello/toil/src/toil/worker.py", line 316, in main
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:       job = Job._loadJob(jobWrapper.command, jobStore)
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:     File "/home/cmarkello/toil/src/toil/job.py", line 666, in _loadJob
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:       userModule = cls._loadUserModule(userModule)
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:     File "/home/cmarkello/toil/src/toil/job.py", line 640, in _loadUserModuleWARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:       return importlib.import_module(userModule.name)
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:     File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:       __import__(name)
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   ImportError: No module named toil_vg.vg_evaluation_pipeline
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   Exiting the worker because of a failed jobWrapper on host c1agent3
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host c1agent3
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:urllib3.connectionpool:Setting read timeout to 65
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   DEBUG:urllib3.connectionpool:"GET /acmarkellotoilvgjobstore11xxjobitems(PartitionKey='default',RowKey='fc48619f_e141_4211_8ab9_3004f95b16df')?$select= HTTP/1.1" 200 None
WARNING:toil.leader:fc48619f_e141_4211_8ab9_3004f95b16df:   WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job fc48619f_e141_4211_8ab9_3004f95b16df to 0

# Fixed code
def createBatchSystem(config):
    """
    Creates an instance of the batch system specified in the given config.

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

    :rtype: batchSystems.abstractBatchSystem.AbstractBatchSystem

    :return: an instance of a concrete subclass of 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 config.disableCaching and not batchSystemClass.supportsWorkerCleanup():
        raise RuntimeError(
            "%s currently does not support shared caching.  Set the "
            "--disableCaching flag 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()
    )

    return batchSystemClass(**kwargs)