forked from mit-athena/delete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete
executable file
·193 lines (180 loc) · 7.41 KB
/
delete
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
import logging
import optparse
import os
import shutil
import stat
import sys
import afs.fs
logger = logging.getLogger('delete')
whoami = os.path.basename(sys.argv[0])
import libdelete
def debug_callback(option, opt_str, value, parser):
"""
An OptionParser callback that enables debugging.
"""
all_loggers = [logger.name, 'libdelete']
loggers = [x.strip() for x in value.split(',')]
if value.lower() == 'all':
loggers = all_loggers
else:
if not set(loggers) == set(all_loggers):
parser.error('Valid debug targets: {0}'.format(
", ".join(all_loggers)))
for l in loggers:
logging.getLogger(l).setLevel(logging.DEBUG)
def ask(question, *args, **kwargs):
"""
Ask a question, possibly prepended with the name of the program
and determine whether the user answered in the affirmative
"""
yes = ('y', 'yes')
prepend = '' if kwargs.get('nowhoami', False) else "{0}: ".format(whoami)
try:
return raw_input("%s%s " % (prepend,
question % args)).strip().lower() in yes
except KeyboardInterrupt:
sys.exit(0)
def perror(message, **kwargs):
"""
Format an error message, log it in the debug log
and maybe also print it to stderr.
"""
should_print = kwargs.pop('_maybe', False)
msg = "{0}: {1}".format(whoami, message.format(**kwargs))
logger.debug("Error: %s", msg)
if should_print:
print >>sys.stderr, msg
def actually_delete(filename, options):
"""
Actually delete the file.
"""
logger.debug("actually_delete(%s)", filename)
if options.interactive and not ask('remove %s?', filename):
return False
if not options.force and \
not os.path.islink(filename) and \
not os.access(filename, os.W_OK):
if not ask("File %s not writeable. Delete anyway?",
filename):
return False
if options.noop:
print >>sys.stderr, "{0}: {1} would be removed".format(whoami, filename)
return True
(dirname, basename) = os.path.split(filename)
newname = os.path.join(dirname, '.#' + basename)
if os.path.exists(newname):
# Yes, it just unconditionally scribbles over things, and always has.
if os.path.isdir(newname) and not os.path.islink(newname):
shutil.rmtree(newname)
else:
os.unlink(newname)
assert not os.path.exists(newname), "Fatal error: new path exists"
logger.debug("Move %s to %s", filename, newname)
# Maybe we can just use os.rename here
shutil.move(filename, newname)
# None means use the current time
os.utime(newname, None)
return True
def delete(filename, options):
logger.debug("delete(%s)", filename)
if not os.path.lexists(filename):
perror('{filename}: No such file or directory',
filename=filename, _maybe=options.report_errors)
return False
if os.path.isdir(filename) and not os.path.islink(filename):
if os.path.basename(filename) in ('.', '..'):
perror("Cannot delete '.' or '..'", _maybe=options.report_errors)
return False
if options.filesonly:
if not options.recursive:
perror("{filename}: can't delete (not file)",
filename=filename, _maybe=options.report_errors)
return False
for x in libdelete.dir_listing(filename):
logger.debug("Recursively deleting %s", x)
if not delete(x, options):
logger.debug("Recursive delete failed")
return False
return actually_delete(x, options)
else:
try:
is_empty = libdelete.empty_directory(filename)
except OSError as e:
# Do we want to only do this if emulating rm?
print >>sys.stderr, ": ".join((whoami, e.filename, e.strerror))
return False
if is_empty:
return actually_delete(filename, options)
if options.directoriesonly or not options.recursive:
perror("{filename}: can't delete (directory not empty)",
filename=filename, _maybe=options.report_errors)
elif options.recursive:
for x in libdelete.dir_listing(filename):
logger.debug("Recursively deleting %s", x)
if not delete(x, options):
logger.debug("Recursively delete failed")
return False
return actually_delete(filename, options)
# Not a directory
else:
if options.directoriesonly:
perror("{filename}: can't delete (not directory)",
filename=filename, _maybe=options.report_errors)
else:
return actually_delete(filename, options)
def main():
parser = optparse.OptionParser(usage="%prog [options] filename ...")
# This is probably a terrible idea, but the old code did it
linked_to_rm = whoami == "rm"
linked_to_rmdir = whoami == "rmdir"
parser.add_option(
"-r", dest="recursive", action="store_true", default=False,
help="Recursively delete non-empty directories")
parser.add_option(
"-f", dest="force", action="store_true", default=False,
help="Do not ask questions or report errors about nonexistent files")
parser.add_option(
"-i", dest="interactive", action="store_true", default=False,
help="Prompt for confirmation before deleting each file/directory")
parser.add_option(
"-n", dest="noop", action="store_true", default=False,
help="Don't actually delete, just print what would be deleted")
parser.add_option(
"-v", dest="verbose", action="store_true", default=False,
help="Print each filename as it is deleted")
parser.add_option(
"-e", dest="emulate_rm", action="store_true",
default=(linked_to_rm or linked_to_rmdir),
help="Emulate the pecularities of rm and rmdir")
parser.add_option(
"-F", dest="filesonly", action="store_true", default=linked_to_rm,
help="Remove files only (refuse to remove even non-empty directories)")
parser.add_option(
"-D", dest="directoriesonly", action="store_true",
default=linked_to_rmdir,
help="Only remove empty directories (refuse to remove files)")
parser.add_option(
"--debug", action="callback", type='string', help="Enable debugging (logger target or 'all')",
callback=debug_callback, metavar='target')
(options, args) = parser.parse_args()
if options.filesonly and options.directoriesonly:
parser.error("-F and -D are mutually exclusive")
if options.recursive and options.directoriesonly:
parser.error("-r and -D are mutually exclusive")
if len(args) < 1:
parser.error("No files or directories specified.")
options.report_errors = not (options.emulate_rm or options.force)
errors = 0
for filename in args:
# Because you know _someone_ will try it
if len(filename.rstrip('/')) < 1:
print >>sys.stderr, "That's not a good idea."
sys.exit(1)
# Trailing slashes make bad things happen
if not delete(filename.rstrip('/'), options):
errors = 1
return errors
if __name__ == "__main__":
logging.basicConfig(level=logging.WARNING)
sys.exit(main())