-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/fix-4' into dev
* feature/fix-4: #4 Arm purgePrivateConfigs.py Add one day cutoff #4 paths correctly identified, ready to purge #4 Now correctly identifying files to purge Purger will exit if it cannot find configsDir #4 Add purgePrivateConfigs.py
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 allFilesWithStartingWithStem(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.allFilesWithStartingWithStem(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() |