This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
purgePrivateConfigs.py
executable file
·64 lines (48 loc) · 1.9 KB
/
purgePrivateConfigs.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
#!/usr/bin/env python3
'''
Walk the configs dir and reap all unpublished configs.
These are configs that have no saved ".replay" pickle file.
'''
import sys
import time
from pathlib import Path
class Purger:
def __init__(self):
self.configsDir = Path('./www/configs')
def allBindings(self):
return list(self.configsDir.glob('**/*.binds'))
def replayPath(self, path):
return path.with_suffix('.replay')
def hasReplay(self, bindPath):
replayPath = self.replayPath(bindPath)
return replayPath.exists()
def isOverOneDayOld(self, bindPath):
stat = bindPath.stat()
fileTouchedTime = stat.st_ctime
now = time.time()
cutoff = now - 86400 # bad practice but good enough for this
return fileTouchedTime < cutoff
def thoseWithoutReplay(self, bindingsPaths):
return [path for path in bindingsPaths if not self.hasReplay(path)]
def allFilesStartingWithStem(self, path):
nameGlob = '%s*.*' % path.stem
parent = path.parent
return list(parent.glob(nameGlob))
def purgeFile(self, path):
path.unlink()
def purge(self):
if not self.configsDir.exists():
# script probably installed in the wrong place or called with the wrong working directory
sys.exit('%s not found' % self.configsDir)
allBindings = self.allBindings()
privateBindings = self.thoseWithoutReplay(allBindings)
oldPrivateBindings = [path for path in privateBindings if self.isOverOneDayOld(path)]
deepListToPurge = [self.allFilesStartingWithStem(path) for path in oldPrivateBindings]
filesToPurge = [x for sublist in deepListToPurge for x in sublist]
for path in filesToPurge:
self.purgeFile(path)
def main():
purger = Purger()
purger.purge()
if __name__ == '__main__':
main()