Skip to content

Commit

Permalink
Merge branch 'feature/fix-4' into dev
Browse files Browse the repository at this point in the history
* 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
richardbuckle committed Aug 27, 2017
2 parents 44889f1 + 20b72ea commit 436efaa
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions purgePrivateConfigs.py
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()

0 comments on commit 436efaa

Please sign in to comment.