forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CleanFileCache.py
102 lines (86 loc) · 2.79 KB
/
CleanFileCache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=no-init,invalid-name,bare-except,too-many-arguments,multiple-statements
from mantid.api import *
from mantid.kernel import *
import os
# See ticket #14716
class CleanFileCache(PythonAlgorithm):
""" Remove cache files from the cache directory
"""
def category(self):
"""
"""
return "Workflow\\DataHandling"
def seeAlso(self):
return [ "ClearCache" ]
def name(self):
"""
"""
return "CleanFileCache"
def summary(self):
""" Return summary
"""
return """Remove cache files"""
def require(self):
return
def PyInit(self):
""" Declare properties
"""
# this is the requirement of using this plugin
# is there a place to register that?
self.require()
self.declareProperty(
"CacheDir", "",
"the directory in which the cache file will be created. If nothing is given, default location for cache files will be used",
Direction.Input)
self.declareProperty(
"AgeInDays", 14,
"If any file is more than this many days old, it will be deleted. 0 means remove everything",
Direction.Input)
return
def PyExec(self):
""" Main Execution Body
"""
# Inputs
cache_dir = self.getPropertyValue("CacheDir")
if not cache_dir:
cache_dir = os.path.join(
ConfigService.getUserPropertiesDir(),
"cache"
)
age = int(self.getPropertyValue("AgeInDays"))
#
_run(cache_dir, age)
return
def _run(cache_dir, days):
import glob
import re
import time
from datetime import timedelta, date
rm_date = date.today() - timedelta(days = days)
rm_date = time.mktime(rm_date.timetuple()) + 24*60*60
for f in glob.glob(os.path.join(cache_dir, "*.nxs")):
# skip over non-files
if not os.path.isfile(f):
continue
# skip over new files
# print os.stat(f).st_mtime, rm_date
if os.stat(f).st_mtime > rm_date:
continue
# check filename pattern
base = os.path.basename(f)
if re.match(".*_[0-9a-f]{40}.nxs", base):
os.remove(f)
continue
if re.match("[0-9a-f]{40}.nxs", base):
os.remove(f)
continue
continue
return
# Register algorithm with Mantid
AlgorithmFactory.subscribe(CleanFileCache)