Issue #3142: Improve error-handling and logging in CleanupGfeHDF5Storage delta script.

Change-Id: I5c75cfb44654781a53f4588236f5d5d562432615

Former-commit-id: 9ebbf783df [formerly 9ebbf783df [formerly 920999bda9dda329e71359ab46872130f45d298c]]
Former-commit-id: 29072aa824
Former-commit-id: 1769a52111
This commit is contained in:
David Gillingham 2014-05-08 12:18:23 -05:00
parent 9274027a7a
commit 36684955e5

View file

@ -30,27 +30,47 @@
# ------------ ---------- ----------- -------------------------- # ------------ ---------- ----------- --------------------------
# 11/18/10 njensen Initial Creation. # 11/18/10 njensen Initial Creation.
# 06/13/13 #2044 randerso Fixed to use correct python # 06/13/13 #2044 randerso Fixed to use correct python
# 05/08/14 #3142 dgilling Add better error-handling, logging.
# #
# #
# #
import os import os
import logging
hdf5loc = "/awips2/edex/data/hdf5/gfe" hdf5loc = "/awips2/edex/data/hdf5/gfe"
def processDir(dir): logging.basicConfig(format="%(asctime)s %(name)s:%(lineno)d %(levelname)s: %(message)s",
# walk the directory tree removing *_GridParm.h5 files datefmt="%H:%M:%S",
for file in os.listdir(dir): level=logging.INFO)
filePath = os.path.join(dir, file) log = logging.getLogger("CleanupGfeHDF5Storage")
if os.path.isfile(filePath) and str(filePath).endswith("_GridParm.h5"):
print "Removing ", filePath
os.remove(filePath) def processDir(hdf5Dir):
elif os.path.isdir(filePath): # walk the directory tree removing *_GridParm.h5 files
processDir(filePath) # any directories that become empty after removing those files will also
# be deleted.
for root, dirs, files in os.walk(hdf5Dir, topdown=False):
for file in files:
if str(file).endswith("_GridParm.h5"):
fullPath = os.path.join(root, file)
log.info("Removing " + str(fullPath))
try:
os.remove(fullPath)
except OSError:
log.exception("Could not delete file " + str(fullPath))
for dir in dirs:
fullPath = os.path.join(root, dir)
try:
if not os.listdir(fullPath):
log.info("Removing " + str(fullPath))
try:
os.rmdir(fullPath)
except OSError:
log.exception("Could not delete path " + str(fullPath))
except OSError:
log.warning("Skipping directory " + str(fullPath), exc_info=True)
# if directory is empty remove it
if len(os.listdir(dir)) == 0:
print "Removing ", dir
os.removedirs(dir)
def main(): def main():
processDir(hdf5loc) processDir(hdf5loc)