| Revision 7614 (by gradha, 2006/12/02 18:13:42) |
Whitespace cleanup. |
#!/usr/bin/env python
# -*- mode:Python; tab-width: 3 -*-
"""
This is a small script which emulates the GNU rm binary, which was
used to delete files. Since the whole build needs python anyway,
this little script avoids the need to have GNU's rm installed,
which is usually not the case under Windows platforms.
The behaviour of the program is similar to that of 'rm -fv':
the program will try to delete as much of the provided files
(which can be patterns using metacharacters like * or ?) while
displaying each deleted file on stdout. Beware: there's no help,
no switches, no anything. It will wip out your most precious files
without blinking if you say so. Well, if the first parameter is
"-R", you get to delete directories recursively.
This script is gift-ware, it's given to you freely as a gift.
You may use, modify, redistribute, and generally hack it about
in any way you like, and you do not have to give me anything in
return. However, if you like this script you are encouraged to thank
me emailing me your opinion about it, requests for more features,
or directly diffs which implement them. If you redistribute parts
of this script or use it somewhere successfully, it would be nice if
you mentioned me somewhere in the credits, but you are not required
to do this.
Of course, I do not accept responsibility for any effects, adverse
or otherwise, that this code may have on you, your computer, your
sanity, your dog and anything else that you can think of.
Use it at your own risk.
Written by Grzegorz Adam Hankiewicz <gradha@users.sourceforge.net>.
Download this and other things from http://gradha.sdf-eu.org/.
$Id: rm.py 7614 2006-12-02 18:13:42Z gradha $
"""
import glob
import os
import shutil
import sys
errors = []
def remove_file(filename, remove_dirs):
"""Attempts to remove the file. Errors are stored in the errors list."""
if not os.path.isfile(filename):
if remove_dirs:
shutil.rmtree(filename, ignore_errors = 1)
else:
try: os.unlink(filename)
except IOError, desc:
errors.append((filename, desc))
if __name__ == "__main__":
remove_dirs = 0
args = sys.argv[1:]
if len(args) and args[0] == "-R":
del args[0]
remove_dirs = 1
for filemask in args:
for file in glob.glob (filemask):
remove_file (file, remove_dirs)
if errors:
for f in errors: print "Error deleting '%s': %s" % f
sys.exit (errors)