# Buggy code
def main():
    """Reports the state of the toil."""

    ##########################################
    # Construct the arguments.
    ##########################################

    parser = getBasicOptionParser()

    parser.add_argument(
        "jobStore",
        type=str,
        help=(
            "Store in which to place job management files \
              and the global accessed temporary files"
            "(If this is a file path this needs to be globally accessible "
            "by all machines running jobs).\n"
            "If the store already exists and restart is false an"
            " JobStoreCreationException exception will be thrown."
        ),
    )

    parser.add_argument(
        "--verbose",
        dest="verbose",
        action="store_true",
        help="Print loads of information, particularly all the log files of \
                      jobs that failed. default=%(default)s",
        default=False,
    )

    parser.add_argument(
        "--failIfNotComplete",
        dest="failIfNotComplete",
        action="store_true",
        help="Return exit value of 1 if toil jobs not all completed. default=%(default)s",
        default=False,
    )
    parser.add_argument("--version", action="version", version=version)
    options = parseBasicOptions(parser)
    logger.info("Parsed arguments")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(0)

    ##########################################
    # Do some checks.
    ##########################################

    logger.info("Checking if we have files for Toil")
    assert options.jobStore is not None

    ##########################################
    # Survey the status of the job and report.
    ##########################################

    jobStore = Toil.loadOrCreateJobStore(options.jobStore)
    try:
        rootJob = jobStore.loadRootJob()
    except JobException:
        print(
            "The root job of the job store is absent, the workflow completed successfully.",
            file=sys.stderr,
        )
        sys.exit(0)

    toilState = ToilState(jobStore, rootJob)

    # The first element of the toilState.updatedJobs tuple is the jobWrapper we want to inspect
    totalJobs = set(toilState.successorCounts.keys()) | {
        jobTuple[0] for jobTuple in toilState.updatedJobs
    }

    failedJobs = [job for job in totalJobs if job.remainingRetryCount == 0]

    print(
        "There are %i active jobs, %i parent jobs with children, and %i totally failed jobs "
        "currently in %s."
        % (
            len(toilState.updatedJobs),
            len(toilState.successorCounts),
            len(failedJobs),
            options.jobStore,
        ),
        file=sys.stderr,
    )

    if (
        options.verbose
    ):  # Verbose currently means outputting the files that have failed.
        for job in failedJobs:
            if job.logJobStoreFileID is not None:
                with job.getLogFileHandle(jobStore) as logFileHandle:
                    logStream(logFileHandle, job.jobStoreID, logger.warn)
            else:
                print(
                    "Log file for job %s is absent." % job.jobStoreID, file=sys.stderr
                )
        if len(failedJobs) == 0:
            print("There are no failed jobs to report.", file=sys.stderr)

    if (
        len(toilState.updatedJobs) + len(toilState.successorCounts)
    ) != 0 and options.failIfNotComplete:
        sys.exit(1)


# Traceback (toil.jobStores.abstractJobStore.NoSuchFileException)
cmarkello@c1master1:~$ toil clean azure:hgvm:hgvmevaljobstore
2016-05-13 23:08:36,438 INFO:toil.lib.bioio: Logging set at level: INFO
2016-05-13 23:08:36,445 INFO:toil.utils.toilClean: Parsed arguments
2016-05-13 23:08:36,445 INFO:toil.utils.toilClean: Checking if we have files for toil
2016-05-13 23:08:36,525 INFO:requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): hgvm.table.core.windows.net
2016-05-13 23:08:36,724 INFO:requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): hgvm.blob.core.windows.net
Traceback (most recent call last):
File "/usr/local/bin/toil", line 9, in <module>
load_entry_point('toil==3.2.0a2', 'console_scripts', 'toil')()
File "/usr/local/lib/python2.7/dist-packages/toil/utils/toilMain.py", line 30, in main
module.main()
File "/usr/local/lib/python2.7/dist-packages/toil/utils/toilClean.py", line 53, in main
jobStore = Toil.loadOrCreateJobStore(options.jobStore)
File "/usr/local/lib/python2.7/dist-packages/toil/common.py", line 589, in loadOrCreateJobStore
return AzureJobStore(account, namePrefix, config=config)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 120, in __init__
super(AzureJobStore, self).__init__(config=config)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/abstractJobStore.py", line 141, in __init__
with self.readSharedFileStream("config.pickle") as fileHandle:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 318, in readSharedFileStream
raise NoSuchFileException(sharedFileID)
toil.jobStores.abstractJobStore.NoSuchFileException: File 'a77581f6_ad82_5142_acb7_5a6b01eab3fb' does not exist

# Fixed code
def main():
    """Reports the state of the toil."""

    ##########################################
    # Construct the arguments.
    ##########################################

    parser = getBasicOptionParser()

    parser.add_argument(
        "jobStore",
        type=str,
        help="The location of a job store that holds the information about the "
        "workflow whose status is to be reported on." + jobStoreLocatorHelp,
    )

    parser.add_argument(
        "--verbose",
        dest="verbose",
        action="store_true",
        help="Print loads of information, particularly all the log files of \
                      jobs that failed. default=%(default)s",
        default=False,
    )

    parser.add_argument(
        "--failIfNotComplete",
        dest="failIfNotComplete",
        action="store_true",
        help="Return exit value of 1 if toil jobs not all completed. default=%(default)s",
        default=False,
    )
    parser.add_argument("--version", action="version", version=version)
    options = parseBasicOptions(parser)
    logger.info("Parsed arguments")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(0)

    ##########################################
    # Do some checks.
    ##########################################

    logger.info("Checking if we have files for Toil")
    assert options.jobStore is not None

    ##########################################
    # Survey the status of the job and report.
    ##########################################

    jobStore = Toil.resumeJobStore(options.jobStore)
    try:
        rootJob = jobStore.loadRootJob()
    except JobException:
        print(
            "The root job of the job store is absent, the workflow completed successfully.",
            file=sys.stderr,
        )
        sys.exit(0)

    toilState = ToilState(jobStore, rootJob)

    # The first element of the toilState.updatedJobs tuple is the jobWrapper we want to inspect
    totalJobs = set(toilState.successorCounts.keys()) | {
        jobTuple[0] for jobTuple in toilState.updatedJobs
    }

    failedJobs = [job for job in totalJobs if job.remainingRetryCount == 0]

    print(
        "There are %i active jobs, %i parent jobs with children, and %i totally failed jobs "
        "currently in %s."
        % (
            len(toilState.updatedJobs),
            len(toilState.successorCounts),
            len(failedJobs),
            options.jobStore,
        ),
        file=sys.stderr,
    )

    if (
        options.verbose
    ):  # Verbose currently means outputting the files that have failed.
        for job in failedJobs:
            if job.logJobStoreFileID is not None:
                with job.getLogFileHandle(jobStore) as logFileHandle:
                    logStream(logFileHandle, job.jobStoreID, logger.warn)
            else:
                print(
                    "Log file for job %s is absent." % job.jobStoreID, file=sys.stderr
                )
        if len(failedJobs) == 0:
            print("There are no failed jobs to report.", file=sys.stderr)

    if (
        len(toilState.updatedJobs) + len(toilState.successorCounts)
    ) != 0 and options.failIfNotComplete:
        sys.exit(1)