diff --git a/scripts/Tools/case.build b/scripts/Tools/case.build index 394e8d3c656..3e94c4db0e6 100755 --- a/scripts/Tools/case.build +++ b/scripts/Tools/case.build @@ -60,7 +60,9 @@ OR CIME.utils.handle_standard_logging_options(args) - return args.caseroot, args.sharedlib_only, args.model_only, args.clean, args.clean_all + cleanlist = args.clean if args.clean is None or len(args.clean) else comps + + return args.caseroot, args.sharedlib_only, args.model_only, cleanlist, args.clean_all ############################################################################### def _main_func(description): diff --git a/utils/python/CIME/SystemTests/system_tests_common.py b/utils/python/CIME/SystemTests/system_tests_common.py index bdcec2261c9..c8a09c1c683 100644 --- a/utils/python/CIME/SystemTests/system_tests_common.py +++ b/utils/python/CIME/SystemTests/system_tests_common.py @@ -118,7 +118,7 @@ def build_indv(self, sharedlib_only=False, model_only=False): sharedlib_only=sharedlib_only, model_only=model_only) def clean_build(self, comps=None): - build.clean(self._case, cleanlist=comps) + build.clean(self._case, cleanlist=comps if comps is not None else []) def run(self): """ diff --git a/utils/python/CIME/build.py b/utils/python/CIME/build.py index e58e0e7bf43..6e5c9d0463a 100644 --- a/utils/python/CIME/build.py +++ b/utils/python/CIME/build.py @@ -508,21 +508,32 @@ def _build_model_thread(config_dir, compclass, caseroot, libroot, bldroot, incro for mod_file in glob.glob(os.path.join(bldroot, "*_[Cc][Oo][Mm][Pp]_*.mod")): shutil.copy(mod_file, incroot) +def cleantree(base, ignoredirs=None): + """ + clean all files below base level, leave directories and top level files + """ + ignoredirs.append(base) + for root, _, files in os.walk(base, topdown=False): + if root not in ignoredirs: + for name in files: + os.remove(os.path.join(root, name)) + + ############################################################################### def clean(case, cleanlist=None, clean_all=False): ############################################################################### caseroot = case.get_value("CASEROOT") - if clean_all or cleanlist is not None and len(cleanlist) == 0: + if clean_all: # If cleanlist is empty just remove the bld directory exeroot = case.get_value("EXEROOT") if os.path.isdir(exeroot): - logging.info("removing directory %s" %exeroot) - shutil.rmtree(case.get_value("EXEROOT")) + logging.info("cleaning directory %s" %exeroot) + cleantree(exeroot, ignoredirs=[os.path.join(exeroot,"lib")]) # if clean_all is True also remove the sharedlibpath sharedlibroot = case.get_value("SHAREDLIBROOT") - if clean_all and os.path.isdir(sharedlibroot): - logging.info("removing directory %s" %sharedlibroot) - shutil.rmtree(sharedlibroot) + if sharedlibroot != exeroot and os.path.isdir(sharedlibroot): + logging.warn("cleaning directory %s" %sharedlibroot) + cleantree(sharedlibroot) else: debug = case.get_value("DEBUG") use_esmf_lib = case.get_value("USE_ESMF_LIB")