def bootstrapArmiTestEnv(): """ Perform ARMI config appropriate for running unit tests .. tip:: This can be imported and run from other ARMI applications for test support. """ from armi.nucDirectory import nuclideBases cs = caseSettings.Settings() context.Mode.setMode(context.Mode.BATCH) settings.setMasterCs(cs) # Need to init burnChain. # see armi.cases.case.Case._initBurnChain with open(cs["burnChainFileName"]) as burnChainStream: nuclideBases.imposeBurnChain(burnChainStream) # turn on a non-interactive mpl backend to minimize errors related to # initializing Tcl in parallel tests matplotlib.use("agg") # set and create a test-specific FAST_PATH for parallel unit testing # Not all unit tests have operators, and operators are usually # responsible for making FAST_PATH, so we make it here. # It will be deleted by the atexit hook. context.activateLocalFastPath() if not os.path.exists(context.getFastPath()): os.makedirs(context.getFastPath()) # some tests need to find the TEST_ROOT via an env variable when they're # filling in templates with ``$ARMITESTBASE`` in them or opening # input files use the variable in an `!include` tag. Thus # we provide it here. os.environ["ARMITESTBASE"] = TEST_ROOT
def _initFastPath(): """ Create the FAST_PATH directory for fast local operations Notes ----- The FAST_PATH was once created at import-time in order to support modules that use FAST_PATH without operators (e.g. Database). However, we decided to leave FAST_PATH as the CWD in INTERACTIVE mode, so this should not be a problem anymore, and we can safely move FAST_PATH creation back into the Operator. If the operator is being used interactively (e.g. at a prompt) we will still use a temporary local fast path (in case the user is working on a slow network path). """ context.activateLocalFastPath() try: os.makedirs(context.getFastPath()) except OSError: # If FAST_PATH exists already that generally should be an error because # different processes will be stepping on each other. # The exception to this rule is in cases that instantiate multiple operators in one # process (e.g. unit tests that loadTestReactor). Since the FAST_PATH is set at # import, these will use the same path multiple times. We pass here for that reason. if not os.path.exists(context.getFastPath()): # if it actually doesn't exist, that's an actual error. Raise raise
def setRunDirFromCaseTitle(self, caseTitle): """ Set run directory derived from case title and label. This is optional (you can set runDir to whatever you want). If you use this, you will get a relatively consistent naming convention for your fast-past folders. """ self.runDir = os.path.join(getFastPath(), f"{caseTitle}-{self.label}-{MPI_RANK}")
def setRunDirFromCaseTitle(self, caseTitle): """ Set run directory derived from case title and label. This is optional (you can set runDir to whatever you want). If you use this, you will get a relatively consistent naming convention for your fast-past folders. """ # This creates a hash of the case title plus the label # to shorten the running directory and to avoid path length # limitations on the OS. caseString = f"{caseTitle}-{str(self.label)}".encode("utf-8") caseTitleHash = str(hashlib.sha1(caseString).hexdigest())[:8] self.runDir = os.path.join(getFastPath(), f"{caseTitleHash}-{MPI_RANK}")
def __init__(self, root=None, filesToMove=None, filesToRetrieve=None, dumpOnException=True): DirectoryChanger.__init__(self, root, filesToMove, filesToRetrieve, dumpOnException) # If no root dir is given, the default path comes from context.getFastPath, which # *might* be relative to the cwd, making it possible to delete unintended files. # So this check is here to ensure that if we grab a path from context, it is a # proper temp dir. # That said, since the TemporaryDirectoryChanger *always* responsible for # creating its destination directory, it may always be safe to delete it # regardless of location. if root is None: root = context.getFastPath() # ARMIs temp dirs are in an context.APP_DATA directory: validate this is a temp dir. if pathlib.Path( context.APP_DATA) not in pathlib.Path(root).parents: raise ValueError( "Temporary directory not in a safe location for deletion.") # make the tmp dir, if necessary if not os.path.exists(root): try: os.makedirs(root) except FileExistsError: # ignore the obvious race condition pass # init the important path attributes self.initial = os.path.abspath(os.getcwd()) self.destination = TemporaryDirectoryChanger.GetRandomDirectory(root) while os.path.exists(self.destination): self.destination = TemporaryDirectoryChanger.GetRandomDirectory( root)