import os, glob, re """Script that is run on the CIPRES web server weekly to remove old builds (and build logs and test-reports) It leaves the newest build for each platform and any archives listed in /misc/www/projects/TOL/dev_snapshot/save.txt """ import sys dryRun = '--dry-run' in sys.argv def lslt(filePattern = '*'): 'Returns a list of files that match the fileglob sorted by mod time (newest first)' d = filePattern l = glob.glob(d) withTimes = [(os.stat(i).st_mtime, i) for i in l] withTimes.sort() withTimes.reverse() return [i[1] for i in withTimes] _pat = re.compile(r'CIPRES-v([^-]+)-r([^-]+)-(.*)(.dmg|.tar.gz|.zip)') def archiveNameToTuple(filename): m = _pat.match(filename) if m: return m.groups() return (None, None, None, None) def getFilesToSave(): try: f = open(os.environ.get('WEB_PAGE_HOME', '/misc/www/projects/TOL') + '/dev_snapshot/save.txt', 'rU') return [archiveNameToTuple(filename) for filename in f] except: return [] def remove(p): if not os.path.exists(p): print p, 'does not exist!' return if os.path.isdir(p): s = 'rm -rf %s' % p print s if not dryRun: os.system(s) else: print 'rm ', p if not dryRun: os.remove(p) if __name__ == '__main__': webDir = os.environ.get('WEB_PAGE_HOME', '/misc/www/projects/TOL/') dev_snapshotPage = os.path.join(webDir, 'dev_snapshot') buildLogPage = os.path.join(webDir, 'software/SDK/build-logs') testLogPage = os.path.join(webDir, 'software/SDK/test-reports') toSave = getFilesToSave() snapshotsToSave = ['CIPRES-v%s-r%s-%s%s' % i for i in toSave] #toSave = getFilesToSave() #toSave = getFilesToSave() patterns = ['CIPRES-v*-r*-Mac*Universal.dmg', 'CIPRES-v*-r*-win*.zip', 'CIPRES-v*-r*-Linux*.tar.gz'] for p in patterns: snapshots = lslt(dev_snapshotPage + '/' + p) newest = True for path in snapshots: if newest: newest = False print 'Leaving the newest: ', path continue par, filename = os.path.split(path) if filename not in snapshotsToSave: remove(path) t = archiveNameToTuple(filename) remove(os.path.join(buildLogPage, '%s-cipres-%s-r%s-build-log.txt' % (t[2], t[0], t[1]))) remove(os.path.join(testLogPage, '%s-cipres-%s-r%s-test-report' % (t[2], t[0], t[1]))) else: print 'Leaving saved: ', filename