Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:39:01

0001 import datetime
0002 import glob
0003 import optparse
0004 import os
0005 import sys
0006 
0007 from pandacommon.pandautils.PandaUtils import naive_utcnow
0008 
0009 
0010 # main
0011 def main(argv=tuple(), **kwargs):
0012     # options
0013     optP = optparse.OptionParser(conflict_handler="resolve")
0014     optP.add_option(
0015         "-t",
0016         action="store_const",
0017         const=True,
0018         dest="test",
0019         default=False,
0020         help="test mode",
0021     )
0022     optP.add_option(
0023         "-h",
0024         action="store",
0025         type="int",
0026         dest="limit",
0027         default=12,
0028         help="time limit in hour",
0029     )
0030     options, args = optP.parse_args(args=argv[1:])
0031 
0032     # patterns of tmp files
0033     tmpPatts = ["/tmp/tmp*", "/tmp/atlpan/tmp*", "/tmp/pansrv/tmp*"]
0034 
0035     # limit
0036     timeLimit = naive_utcnow() - datetime.timedelta(hours=options.limit)
0037 
0038     # loop over all pattern
0039     for tmpPatt in tmpPatts:
0040         tmpFiles = glob.glob(tmpPatt)
0041         # loop over all files
0042         for tmpFile in tmpFiles:
0043             try:
0044                 print(f"INFO: tmpfile -> {tmpFile}")
0045                 # only file
0046                 if not os.path.isfile(tmpFile):
0047                     continue
0048                 # not symlink
0049                 if os.path.islink(tmpFile):
0050                     continue
0051                 # writable
0052                 if not os.access(tmpFile, os.W_OK):
0053                     continue
0054                 # check time stamp
0055                 timeStamp = os.path.getmtime(tmpFile)
0056                 timeStamp = datetime.datetime.fromtimestamp(timeStamp)
0057                 if timeStamp > timeLimit:
0058                     continue
0059                 # remove
0060                 print(f"INFO:    remove {tmpFile}")
0061                 if not options.test:
0062                     os.remove(tmpFile)
0063             except Exception:
0064                 errType, errValue = sys.exc_info()[:2]
0065                 print(f"ERROR:   failed with {errType}:{errValue}")
0066 
0067 
0068 # run
0069 if __name__ == "__main__":
0070     main(argv=sys.argv)