示例#1
0
def cleanTempDirs(olderThanDays=None):
    """
    Clean up temporary files after a run.

    The Windows HPC system sends a SIGBREAK signal when the user cancels a job, which
    is NOT handled by ``atexit``. Notably SIGBREAK doesn't exist off Windows.
    For the SIGBREAK signal to work with a Microsoft HPC, the ``TaskCancelGracePeriod``
    option must be configured to be non-zero. This sets the period between SIGBREAK
    and SIGTERM/SIGINT. To do cleanups in this case, we must use the ``signal`` module.
    Actually, even then it does not work because MS ``mpiexec`` does not pass signals
    through.

    Parameters
    ----------
    olderThanDays: int, optional
        If provided, deletes other ARMI directories if they are older than the requested
        time.
    """
    disconnectAllHdfDBs()

    if os.path.exists(FAST_PATH):
        if runLog.getVerbosity() <= runLog.getLogVerbosityRank("extra"):
            print(
                "Cleaning up temporary files in: {}".format(FAST_PATH), file=sys.stdout
            )
        try:
            shutil.rmtree(FAST_PATH)
        except Exception as error:  # pylint: disable=broad-except
            for outputStream in (sys.stderr, sys.stdout):
                print(
                    "Failed to delete temporary files in: {}\n"
                    "    error: {}".format(FAST_PATH, error),
                    file=outputStream,
                )

    # Also delete anything still here after `olderThanDays` days (in case this failed on
    # earlier runs)
    if olderThanDays is not None:
        gracePeriod = datetime.timedelta(days=olderThanDays)
        now = datetime.datetime.now()
        thisRunFolder = os.path.basename(FAST_PATH)

        for dirname in os.listdir(APP_DATA):
            dirPath = os.path.join(APP_DATA, dirname)
            if not os.path.isdir(dirPath):
                continue
            try:
                fromThisRun = dirname == thisRunFolder  # second chance to delete
                _rank, dateString = dirname.split("-")
                dateOfFolder = datetime.datetime.strptime(dateString, "%Y%m%d%H%M%S%f")
                runIsOldAndLikleyComplete = (now - dateOfFolder) > gracePeriod
                if runIsOldAndLikleyComplete or fromThisRun:
                    # Delete old files
                    shutil.rmtree(dirPath)
            except:  # pylint: disable=bare-except
                pass
示例#2
0
def cleanTempDirs(olderThanDays=None):
    """
    Clean up temporary files after a run.

    The Windows HPC system sends a SIGBREAK signal when the user cancels a job, which
    is NOT handled by ``atexit``. Notably SIGBREAK doesn't exist off Windows.
    For the SIGBREAK signal to work with a Microsoft HPC, the ``TaskCancelGracePeriod``
    option must be configured to be non-zero. This sets the period between SIGBREAK
    and SIGTERM/SIGINT. To do cleanups in this case, we must use the ``signal`` module.
    Actually, even then it does not work because MS ``mpiexec`` does not pass signals
    through.

    Parameters
    ----------
    olderThanDays: int, optional
        If provided, deletes other ARMI directories if they are older than the requested
        time.
    """
    from armi import (
        runLog, )  # pylint: disable=import-outside-toplevel # avoid cyclic import

    disconnectAllHdfDBs()
    printMsg = runLog.getVerbosity() <= runLog.getLogVerbosityRank("debug")
    if _FAST_PATH_IS_TEMPORARY and os.path.exists(_FAST_PATH):
        if printMsg:
            print(
                "Cleaning up temporary files in: {}".format(_FAST_PATH),
                file=sys.stdout,
            )
        try:
            shutil.rmtree(_FAST_PATH)
        except Exception as error:  # pylint: disable=broad-except
            for outputStream in (sys.stderr, sys.stdout):
                if printMsg:
                    print(
                        "Failed to delete temporary files in: {}\n"
                        "    error: {}".format(_FAST_PATH, error),
                        file=outputStream,
                    )

    if olderThanDays is not None:
        cleanAllArmiTempDirs(olderThanDays)
示例#3
0
 def test_setVerbosityFromString(self):
     """Test that the log verbosity can be set with a string."""
     expectedStrVerbosity = runLog.getLogVerbosityLevels()[0]
     verbosityRank = runLog.getLogVerbosityRank(expectedStrVerbosity)
     runLog.setVerbosity(expectedStrVerbosity)
     self.assertEqual(verbosityRank, runLog.getVerbosity())