Created
October 11, 2016 15:48
-
-
Save PsyDebug/a3eea632582fca958e6d7de3da3ce6fb to your computer and use it in GitHub Desktop.
Remove all backups for the given project except the last 5
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
import sys | |
import os | |
import os.path | |
import argparse | |
def_folder=os.getcwd() | |
def createParser (): | |
parser = argparse.ArgumentParser(description='Delete your backups') | |
parser.add_argument ('-p', '--proj',default='all',help='Project name') | |
parser.add_argument ('-l','--list',action='store_true',help='Print all projects') | |
parser.add_argument ('-f', '--folder',default=def_folder,help='Folder projects') | |
return parser | |
parser = createParser() | |
if len(sys.argv) == 1: | |
print 'nERROR! You must specify at least one option.n' | |
parser.print_help() | |
exit(); | |
arg=parser.parse_args() | |
folder=arg.folder | |
dirs = [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))] | |
if arg.list: | |
print(dirs) | |
exit(); | |
os.chdir(folder) | |
def rm_from_proj(fold): | |
datehash={} | |
date=[] | |
os.chdir(fold) | |
files=os.listdir('.') | |
for f in files: | |
untime=(os.path.getctime(f)) | |
datehash[untime]=f | |
date.append(os.path.getctime(f)) | |
date.sort() | |
i=len(date) | |
i=i-6 | |
while i >= 0: | |
os.remove(datehash[date[i]]) | |
print(fold,datehash[date[i]],"is deleted") | |
i=i-1 | |
os.chdir(folder) | |
if arg.proj and arg.proj!='all': | |
fold=arg.proj | |
rm_from_proj(fold) | |
if arg.proj=='all': | |
for g in dirs: | |
rm_from_proj(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment