diff options
author | Robbie Harwood <rharwood@redhat.com> | 2016-05-08 02:31:10 -0400 |
---|---|---|
committer | Simo Sorce <simo@redhat.com> | 2016-05-18 17:48:27 -0400 |
commit | 0645b2feb3c3549b67c1d64663be91655f1cbecf (patch) | |
tree | 3e6e5a71f31eeed914d236ba7e9a1621eb1bdd17 | |
parent | 6a0bc4f5cd46b1ab85dba5bd2de28f568cc947b0 (diff) | |
download | mod_auth_gssapi-0645b2feb3c3549b67c1d64663be91655f1cbecf.tar.gz mod_auth_gssapi-0645b2feb3c3549b67c1d64663be91655f1cbecf.tar.xz mod_auth_gssapi-0645b2feb3c3549b67c1d64663be91655f1cbecf.zip |
Add example script for ccache cleaning to contrib
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Reviewed-by: Simo Sorce <simo@redhat.com>
Closes #80
-rw-r--r-- | contrib/sweeper.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/contrib/sweeper.py b/contrib/sweeper.py new file mode 100644 index 0000000..40ed8e2 --- /dev/null +++ b/contrib/sweeper.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# Works with both python2 and python3; please preserve this property + +# Copyright (C) 2016 mod_auth_gssapi contributors - See COPYING for (C) terms + +# If one uses both sessions and unique ccache names, then the filesystem will +# become littered with ccache files unless the accessed application cleans +# them up itself. This script will minimize ccache file proliferation by +# removing any ccaches that have expired from the filesystem, and serves as an +# example of how this cleaning can be performed. + +import gssapi +import os +import re +import stat +import sys +import time + +try: + from gssapi.raw import acquire_cred_from +except ImportError: + print("Your GSSAPI does not provide cred store extension; exiting!") + exit(1) + +# process file as a ccache and indicate whether it is expired +def should_delete(fname, t): + try: + # skip directories and other non-files + st = os.stat(fname) + if not stat.S_ISREG(st.st_mode): + return False + + # ignore files that are newer than 30 minutes + if t - st.st_mtime < 30 * 60: + return False + + creds = acquire_cred_from({b"ccache": fname.encode("UTF-8")}) + except FileNotFoundError: + # someone else did the work for us + return False + except Exception as e: + print("Not deleting %s due to error %s" % (fname, e)) + return False + + return creds.lifetime == 0 + +if __name__ == "__main__": + dirs = sys.argv[1:] + if len(dirs) < 1: + print("Usage: %s dir1 [dir2...]" % sys.argv[0]) + exit(1) + + print("System looks okay; running sweeper...") + + t = time.time() + + for basedir in dirs: + os.chdir(basedir) + print("Sweeping %s" % basedir) + + for fname in os.listdir(basedir): + if should_delete(fname, t): + os.unlink(fname) + + print("Sweeper finished successfully!") + exit(0) |