Issue #3142: Improve error-handling and logging in CleanupGfeHDF5Storage delta script.
Change-Id: I5c75cfb44654781a53f4588236f5d5d562432615 Former-commit-id:39266d563f
[formerly720993104c
] [formerly9ebbf783df
] [formerly39266d563f
[formerly720993104c
] [formerly9ebbf783df
] [formerly29072aa824
[formerly9ebbf783df
[formerly 920999bda9dda329e71359ab46872130f45d298c]]]] Former-commit-id:29072aa824
Former-commit-id:dfe3d2d467
[formerly800438edc0
] [formerly bbc219d6a0b4a04af16b25b53aa80a7784f5dba1 [formerly1769a52111
]] Former-commit-id: 67a88d860b32cecf8811ae2bd18b3868389a1782 [formerly36684955e5
] Former-commit-id:1a3d0a9413
This commit is contained in:
parent
0400944d82
commit
160aacc111
1 changed files with 33 additions and 13 deletions
|
@ -30,27 +30,47 @@
|
|||
# ------------ ---------- ----------- --------------------------
|
||||
# 11/18/10 njensen Initial Creation.
|
||||
# 06/13/13 #2044 randerso Fixed to use correct python
|
||||
# 05/08/14 #3142 dgilling Add better error-handling, logging.
|
||||
#
|
||||
#
|
||||
#
|
||||
import os
|
||||
import logging
|
||||
|
||||
hdf5loc = "/awips2/edex/data/hdf5/gfe"
|
||||
|
||||
def processDir(dir):
|
||||
logging.basicConfig(format="%(asctime)s %(name)s:%(lineno)d %(levelname)s: %(message)s",
|
||||
datefmt="%H:%M:%S",
|
||||
level=logging.INFO)
|
||||
log = logging.getLogger("CleanupGfeHDF5Storage")
|
||||
|
||||
|
||||
def processDir(hdf5Dir):
|
||||
# walk the directory tree removing *_GridParm.h5 files
|
||||
for file in os.listdir(dir):
|
||||
filePath = os.path.join(dir, file)
|
||||
if os.path.isfile(filePath) and str(filePath).endswith("_GridParm.h5"):
|
||||
print "Removing ", filePath
|
||||
os.remove(filePath)
|
||||
elif os.path.isdir(filePath):
|
||||
processDir(filePath)
|
||||
|
||||
# if directory is empty remove it
|
||||
if len(os.listdir(dir)) == 0:
|
||||
print "Removing ", dir
|
||||
os.removedirs(dir)
|
||||
# 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)
|
||||
|
||||
|
||||
def main():
|
||||
processDir(hdf5loc)
|
||||
|
|
Loading…
Add table
Reference in a new issue