Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/gsutil/gslib/commands/rm.py

Issue 2280023003: depot_tools: Remove third_party/gsutil (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/gsutil/gslib/commands/rb.py ('k') | third_party/gsutil/gslib/commands/setacl.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2011 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import boto
16
17 from boto.exception import GSResponseError
18 from gslib.command import Command
19 from gslib.command import COMMAND_NAME
20 from gslib.command import COMMAND_NAME_ALIASES
21 from gslib.command import CONFIG_REQUIRED
22 from gslib.command import FILE_URIS_OK
23 from gslib.command import MAX_ARGS
24 from gslib.command import MIN_ARGS
25 from gslib.command import PROVIDER_URIS_OK
26 from gslib.command import SUPPORTED_SUB_ARGS
27 from gslib.command import URIS_START_ARG
28 from gslib.exception import CommandException
29 from gslib.help_provider import HELP_NAME
30 from gslib.help_provider import HELP_NAME_ALIASES
31 from gslib.help_provider import HELP_ONE_LINE_SUMMARY
32 from gslib.help_provider import HELP_TEXT
33 from gslib.help_provider import HelpType
34 from gslib.help_provider import HELP_TYPE
35 from gslib.name_expansion import NameExpansionIterator
36 from gslib.util import NO_MAX
37
38 _detailed_help_text = ("""
39 <B>SYNOPSIS</B>
40 gsutil rm [-f] [-R] uri...
41
42
43 <B>DESCRIPTION</B>
44 The gsutil rm command removes objects.
45 For example, the command:
46
47 gsutil rm gs://bucket/subdir/*
48
49 will remove all objects in gs://bucket/subdir, but not in any of its
50 sub-directories. In contrast:
51
52 gsutil rm gs://bucket/subdir/**
53
54 will remove all objects under gs://bucket/subdir or any of its
55 subdirectories.
56
57 You can also use the -R option to specify recursive object deletion. Thus, for
58 example, the following two commands will both remove all objects in a bucket:
59
60 gsutil rm gs://bucket/**
61 gsutil rm -R gs://bucket
62
63 If you have a large number of objects to remove you might want to use the
64 gsutil -m option, to perform a parallel (multi-threaded/multi-processing)
65 removes:
66
67 gsutil -m rm -R gs://my_bucket/subdir
68
69 Note that gsutil rm will refuse to remove files from the local
70 file system. For example this will fail:
71
72 gsutil rm *.txt
73
74
75 <B>OPTIONS</B>
76 -f Continues silently (without printing error messages) despite
77 errors when removing multiple objects.
78
79 -R, -r Causes bucket contents to be removed recursively (i.e., including
80 all objects and subdirectories). Will not delete the bucket
81 itself; you need to run the gsutil rb command separately to do
82 that.
83
84 -a Delete all versions of an object.
85 """)
86
87
88 class RmCommand(Command):
89 """Implementation of gsutil rm command."""
90
91 # Command specification (processed by parent class).
92 command_spec = {
93 # Name of command.
94 COMMAND_NAME : 'rm',
95 # List of command name aliases.
96 COMMAND_NAME_ALIASES : ['del', 'delete', 'remove'],
97 # Min number of args required by this command.
98 MIN_ARGS : 1,
99 # Max number of args required by this command, or NO_MAX.
100 MAX_ARGS : NO_MAX,
101 # Getopt-style string specifying acceptable sub args.
102 SUPPORTED_SUB_ARGS : 'afrRv',
103 # True if file URIs acceptable for this command.
104 FILE_URIS_OK : False,
105 # True if provider-only URIs acceptable for this command.
106 PROVIDER_URIS_OK : False,
107 # Index in args of first URI arg.
108 URIS_START_ARG : 0,
109 # True if must configure gsutil before running command.
110 CONFIG_REQUIRED : True,
111 }
112 help_spec = {
113 # Name of command or auxiliary help info for which this help applies.
114 HELP_NAME : 'rm',
115 # List of help name aliases.
116 HELP_NAME_ALIASES : ['del', 'delete', 'remove'],
117 # Type of help:
118 HELP_TYPE : HelpType.COMMAND_HELP,
119 # One line summary of this help.
120 HELP_ONE_LINE_SUMMARY : 'Remove objects',
121 # The full help text.
122 HELP_TEXT : _detailed_help_text,
123 }
124
125 # Command entry point.
126 def RunCommand(self):
127 # self.recursion_requested initialized in command.py (so can be checked
128 # in parent class for all commands).
129 self.continue_on_error = False
130 self.all_versions = False
131 if self.sub_opts:
132 for o, unused_a in self.sub_opts:
133 if o == '-a':
134 self.all_versions = True
135 elif o == '-f':
136 self.continue_on_error = True
137 elif o == '-r' or o == '-R':
138 self.recursion_requested = True
139 elif o == '-v':
140 self.THREADED_LOGGER.info('WARNING: The %s -v option is no longer'
141 ' needed, and will eventually be removed.\n'
142 % self.command_name)
143
144 # Used to track if any files failed to be removed.
145 self.everything_removed_okay = True
146
147 # Tracks if any URIs matched the given args.
148
149 remove_func = self._MkRemoveFunc()
150 exception_handler = self._MkRemoveExceptionHandler()
151
152 try:
153 # Expand wildcards, dirs, buckets, and bucket subdirs in URIs.
154 name_expansion_iterator = NameExpansionIterator(
155 self.command_name, self.proj_id_handler, self.headers, self.debug,
156 self.bucket_storage_uri_class, self.args, self.recursion_requested,
157 flat=self.recursion_requested, all_versions=self.all_versions)
158
159 # Perform remove requests in parallel (-m) mode, if requested, using
160 # configured number of parallel processes and threads. Otherwise,
161 # perform requests with sequential function calls in current process.
162 self.Apply(remove_func, name_expansion_iterator, exception_handler)
163
164 # Assuming the bucket has versioning enabled, uri's that don't map to
165 # objects should throw an error even with all_versions, since the prior
166 # round of deletes only sends objects to a history table.
167 # This assumption that rm -a is only called for versioned buckets should be
168 # corrected, but the fix is non-trivial.
169 except CommandException as e:
170 if not self.continue_on_error:
171 raise
172 except GSResponseError, e:
173 if not self.continue_on_error:
174 raise
175
176 if not self.everything_removed_okay and not self.continue_on_error:
177 raise CommandException('Some files could not be removed.')
178
179 # If this was a gsutil rm -r command covering any bucket subdirs,
180 # remove any dir_$folder$ objects (which are created by various web UI
181 # tools to simulate folders).
182 if self.recursion_requested:
183 folder_object_wildcards = []
184 for uri_str in self.args:
185 uri = self.suri_builder.StorageUri(uri_str)
186 if uri.names_object:
187 folder_object_wildcards.append('%s**_$folder$' % uri)
188 if len(folder_object_wildcards):
189 self.continue_on_error = True
190 try:
191 name_expansion_iterator = NameExpansionIterator(
192 self.command_name, self.proj_id_handler, self.headers, self.debug,
193 self.bucket_storage_uri_class, folder_object_wildcards,
194 self.recursion_requested, flat=True,
195 all_versions=self.all_versions)
196 self.Apply(remove_func, name_expansion_iterator, exception_handler)
197 except CommandException as e:
198 # Ignore exception from name expansion due to an absent folder file.
199 if not e.reason.startswith('No URIs matched:'):
200 raise
201
202 return 0
203
204 def _MkRemoveExceptionHandler(self):
205 def RemoveExceptionHandler(e):
206 """Simple exception handler to allow post-completion status."""
207 self.THREADED_LOGGER.error(str(e))
208 self.everything_removed_okay = False
209 return RemoveExceptionHandler
210
211 def _MkRemoveFunc(self):
212 def RemoveFunc(name_expansion_result):
213 exp_src_uri = self.suri_builder.StorageUri(
214 name_expansion_result.GetExpandedUriStr(),
215 is_latest=name_expansion_result.is_latest)
216 if exp_src_uri.names_container():
217 if exp_src_uri.is_cloud_uri():
218 # Before offering advice about how to do rm + rb, ensure those
219 # commands won't fail because of bucket naming problems.
220 boto.s3.connection.check_lowercase_bucketname(exp_src_uri.bucket_name)
221 uri_str = exp_src_uri.object_name.rstrip('/')
222 raise CommandException('"rm" command will not remove buckets. To '
223 'delete this/these bucket(s) do:\n\tgsutil rm '
224 '%s/*\n\tgsutil rb %s' % (uri_str, uri_str))
225
226 # Perform delete.
227 self.THREADED_LOGGER.info('Removing %s...',
228 name_expansion_result.expanded_uri_str)
229 try:
230 exp_src_uri.delete_key(validate=False, headers=self.headers)
231
232 except:
233 if self.continue_on_error:
234 self.everything_removed_okay = False
235 else:
236 raise
237 return RemoveFunc
OLDNEW
« no previous file with comments | « third_party/gsutil/gslib/commands/rb.py ('k') | third_party/gsutil/gslib/commands/setacl.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698