# Buggy code
def localize(self):
    """
    Check if this module was saved as a resource. If it was, return a new module descriptor
    that points to a local copy of that resource. Should only be called on a worker node.

    :rtype: toil.resource.Resource
    """
    resource = Resource.lookup(self._resourcePath)
    if resource is None:
        log.warn("Can't localize module %r", self)
        return self
    else:

        def stash(tmpDirPath):
            # Save the original dirPath such that we can restore it in globalize()
            with open(os.path.join(tmpDirPath, ".original"), "w") as f:
                f.write(json.dumps(self))

        resource.download(callback=stash)
        return self.__class__(
            dirPath=resource.localDirPath, name=self.name, extension=self.extension
        )


# Traceback (ImportError)
INFO:toil.batchSystems.mesos.batchSystem:Preparing to launch Mesos task 0 using offer 20151120-222317-83886090-5050-35148-O5...
INFO:toil.batchSystems.mesos.batchSystem:...launching Mesos task 0
WARNING:toil.leader:The jobWrapper seems to have left a log file, indicating failure: 46c1efec_0849_4ce3_b74d_c74f1f0602a4
WARNING:toil.leader:Reporting file: 46c1efec_0849_4ce3_b74d_c74f1f0602a4
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ---TOIL WORKER OUTPUT LOG---
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       Traceback (most recent call last):
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/worker.py", line
266, in main
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           job = Job._loadJob(jobWrapper.command, jobStore)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 936, in _loadJob
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           userModule = cls._loadUserModule(userModule)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/local/lib/python2.7/dist-packages/toil/job.py", line 914, in _loadUserModule
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           return importlib.import_module(userModule.name)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           __import__(name)
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:         File "/tmp/tmpflu3Jl/5b3f3d5ba2a95124e308998df2ab7bb2/parallelMappingEvaluation.py", line 16, in <module>
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:           from toillib import *
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ImportError: No module named toillib
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       Exiting the worker because of a failed jobWrapper on host c4agent1
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       ERROR:toil.worker:Exiting the worker because of a failed jobWrapper on host c4agent1
WARNING:toil.leader:46c1efec_0849_4ce3_b74d_c74f1f0602a4:       WARNING:toil.jobWrapper:Due to failure we are reducing the remaining retry count of job 46c1efec_0849_4ce3_b74d_c74f1f0602a4 to 0
WARNING:toil.leader:Job: 46c1efec_0849_4ce3_b74d_c74f1f0602a4 is completely failed

# Fixed code
def localize(self):
    """
    Check if this module was saved as a resource. If it was, return a new module descriptor
    that points to a local copy of that resource. Should only be called on a worker node. On
    the leader, this method returns this resource, i.e. self.

    :rtype: toil.resource.Resource
    """
    if self._runningOnWorker():
        log.warn("The localize() method should only be invoked on a worker.")
    resource = Resource.lookup(self._resourcePath)
    if resource is None:
        log.warn("Can't localize module %r", self)
        return self
    else:

        def stash(tmpDirPath):
            # Save the original dirPath such that we can restore it in globalize()
            with open(os.path.join(tmpDirPath, ".original"), "w") as f:
                f.write(json.dumps(self))

        resource.download(callback=stash)
        return self.__class__(dirPath=resource.localDirPath, name=self.name)