Files
q-tools/anduril/anduril-remove-files
2014-02-15 10:45:11 +02:00

106 lines
3.5 KiB
Python
Executable File

#!/usr/bin/python
import sys
import os,shutil
import re
from argparse import ArgumentParser
LOGFILE="_global"
EXECFILE="_state"
def setup_options():
parser=ArgumentParser(description="Remove execution folders recusively")
parser.add_argument("-l",action="store_true",dest="logs",default=False,
help="Search for log folders [%(default)s]")
parser.add_argument("-e",action="store_true",dest="execs",default=False,
help="Search for execution folders [%(default)s]")
parser.add_argument("-f",action="store_true",dest="force",default=False,
help="Force deletion, otherwise just lists the entries. WARNING: THIS OPTION DELETES FILES RECURSIVELY WITHOUT ASKING. [%(default)s]")
parser.add_argument("-L",action="store_true",dest="symlinks",default=False,
help="Follow symbolic links [%(default)s]")
parser.add_argument("-m",type=str,action='store',dest="move",default="",
help="Move folders to a given subfolder, instead of deleting. WARNING: MOVES FILES WITHOUT ASKING. Moving is only sensible within the same filesystem.")
parser.add_argument('startpath', action="store",default='.', nargs='?')
options=parser.parse_args()
if options.move!="":
options.force=False
if not options.move.endswith("/"):
options.move+="/"
if (not options.logs) and (not options.execs):
print("Nothing to do, not printing logs, nor execution folders")
sys.exit(1)
return options
def delete_folder(folder):
# Delete folder if it exists
if os.path.exists(folder):
shutil.rmtree(folder)
def move_folder(folder,options):
# Move a folder to a location, with folder name flattened /->_
if not os.path.exists(options.move):
os.makedirs(options.move)
new_name=folder
while new_name.startswith(".") or new_name.startswith("/"):
new_name=new_name[1:]
new_name=new_name.replace("/","_")
sys.stdout.write(new_name)
new_name=os.path.join(options.move,new_name)
if os.path.exists(new_name):
sys.stdout.write(" Folder already exists! Not moving.")
return
shutil.move(folder, new_name)
def recurse(options):
for path,dirs,files in os.walk(options.startpath,followlinks=options.symlinks):
# do not move files in move folder a second time..
if options.move!="":
if os.path.samefile(options.move, path):
del dirs[:]
continue
take_action=False
if options.logs:
if LOGFILE in files:
take_action=True
if EXECFILE in files:
del dirs[:]
# never descend in to execution directories
if options.execs:
take_action=True
if take_action:
del dirs[:]
sys.stdout.write(path)
# only one of the following will be true:
if options.force:
sys.stdout.write(" Deleting")
delete_folder(path)
if options.move!="":
sys.stdout.write(" -> "+options.move)
move_folder(path,options)
sys.stdout.write("\n")
else:
#Recurse with cleaned out dirs
dirs.sort()
dirs=clean_dirs(dirs)
return
def clean_dirs(dirs):
# removes hidden folders
for s in dirs[:]:
if (s.startswith(".")):
dirs.remove(s)
return dirs
def main():
options=setup_options();
recurse(options)
sys.exit(0)
main()