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

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

Issue 12042069: Scripts to download files from google storage based on sha1 sums (Closed) Base URL: https://chromium.googlesource.com/chromium/tools/depot_tools.git@master
Patch Set: Removed gsutil/tests and gsutil/docs Created 7 years, 10 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
OLDNEW
(Empty)
1 # Copyright 2011 Google Inc.
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 from gslib.command import Command
16 from gslib.command import COMMAND_NAME
17 from gslib.command import COMMAND_NAME_ALIASES
18 from gslib.command import CONFIG_REQUIRED
19 from gslib.command import FILE_URIS_OK
20 from gslib.command import MAX_ARGS
21 from gslib.command import MIN_ARGS
22 from gslib.command import PROVIDER_URIS_OK
23 from gslib.command import SUPPORTED_SUB_ARGS
24 from gslib.command import URIS_START_ARG
25 from gslib.exception import CommandException
26 from gslib.help_provider import HELP_NAME
27 from gslib.help_provider import HELP_NAME_ALIASES
28 from gslib.help_provider import HELP_ONE_LINE_SUMMARY
29 from gslib.help_provider import HELP_TEXT
30 from gslib.help_provider import HelpType
31 from gslib.help_provider import HELP_TYPE
32 from gslib.util import NO_MAX
33
34 _detailed_help_text = ("""
35 <B>SYNOPSIS</B>
36 gsutil mv [-p] src_uri dst_uri
37 - or -
38 gsutil mv [-p] uri... dst_uri
39
40
41 <B>DESCRIPTION</B>
42 The gsutil mv command allows you to move data between your local file
43 system and the cloud, move data within the cloud, and move data between
44 cloud storage providers. For example, to move all objects from a
45 bucket to a local directory you could use:
46
47 gsutil mv gs://my_bucket dir
48
49 The mv command, like the rm command, will refuse to remove data from
50 the local disk. Thus, for example, this command will not be allowed:
51
52 gsutil mv *.txt gs://my_bucket
53
54
55 <B>RENAMING BUCKET SUBDIRECTORIES</B>
56 You can use the gsutil mv command to rename subdirectories. For example,
57 the command:
58
59 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir
60
61 would rename all objects and subdirectories under gs://my_bucket/olddir to be
62 under gs://my_bucket/newdir, otherwise preserving the subdirectory structure.
63
64 If you do a rename as specified above and you want to preserve ACLs, you
65 should use the -p option (see OPTIONS).
66
67 Note that when using mv to rename bucket subdirectories you cannot specify
68 the source URI using wildcards. You need to spell out the complete name:
69
70 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir
71
72 If you have a large number of files to move you might want to use the
73 gsutil -m option, to perform a multi-threaded/multi-processing move:
74
75 gsutil -m mv gs://my_bucket/olddir gs://my_bucket/newdir
76
77
78 <B>NON-ATOMIC OPERATION</B>
79 Unlike the case with many file systems, the gsutil mv command does not
80 perform a single atomic operation. Rather, it performs a copy from source
81 to destination followed by removing the source for each object.
82
83
84 <B>OPTIONS</B>
85 -p Causes ACL to be preserved when moving in the cloud. Note that
86 this option has performance and cost implications, because it
87 is essentially performing three requests (getacl, cp, setacl).
88 (The performance issue can be mitigated to some degree by
89 using gsutil -m cp to cause multi-threaded/multi-processing
90 copying.)
91 """)
92
93
94 class MvCommand(Command):
95 """Implementation of gsutil mv command.
96 Note that there is no atomic rename operation - this command is simply
97 a shorthand for 'cp' followed by 'rm'.
98 """
99
100 # Command specification (processed by parent class).
101 command_spec = {
102 # Name of command.
103 COMMAND_NAME : 'mv',
104 # List of command name aliases.
105 COMMAND_NAME_ALIASES : ['move', 'ren', 'rename'],
106 # Min number of args required by this command.
107 MIN_ARGS : 2,
108 # Max number of args required by this command, or NO_MAX.
109 MAX_ARGS : NO_MAX,
110 # Getopt-style string specifying acceptable sub args.
111 SUPPORTED_SUB_ARGS : 'pv',
112 # True if file URIs acceptable for this command.
113 FILE_URIS_OK : True,
114 # True if provider-only URIs acceptable for this command.
115 PROVIDER_URIS_OK : False,
116 # Index in args of first URI arg.
117 URIS_START_ARG : 0,
118 # True if must configure gsutil before running command.
119 CONFIG_REQUIRED : True,
120 }
121 help_spec = {
122 # Name of command or auxiliary help info for which this help applies.
123 HELP_NAME : 'mv',
124 # List of help name aliases.
125 HELP_NAME_ALIASES : ['move', 'rename'],
126 # Type of help:
127 HELP_TYPE : HelpType.COMMAND_HELP,
128 # One line summary of this help.
129 HELP_ONE_LINE_SUMMARY : 'Move/rename objects and/or subdirectories',
130 # The full help text.
131 HELP_TEXT : _detailed_help_text,
132 }
133
134 # Command entry point.
135 def RunCommand(self):
136 # Check each source arg up, refusing to delete a bucket or directory src
137 # URI (force users to explicitly do that as a separate operation).
138 for arg_to_check in self.args[0:-1]:
139 if self.suri_builder.StorageUri(arg_to_check).names_container():
140 raise CommandException('The mv command disallows removing source '
141 'buckets or directories (%s).\nYou must '
142 'separately copy and remove if you wish to do '
143 'that.' % arg_to_check)
144
145 # Insert command-line opts in front of args so they'll be picked up by cp
146 # and rm commands (e.g., for -p option). Use undocumented (internal
147 # use-only) cp -M option, which causes each original object to be deleted
148 # after sucessfully copying to its destination, and also causes naming
149 # behavior consistent with Unix mv naming behavior (see comments in
150 # _ConstructDstUri in cp.py).
151 unparsed_args = ['-M']
152 if self.recursion_requested:
153 unparsed_args.append('-R')
154 unparsed_args.extend(self.unparsed_args)
155 self.command_runner.RunNamedCommand('cp', unparsed_args, self.headers,
156 self.debug, self.parallel_operations)
157
158 return 0
159
160 # Test specification. See definition of test_steps in base class for
161 # details on how to populate these fields.
162 num_test_buckets = 3
163 test_steps = [
164 # (test name, cmd line, ret code, (result_file, expect_file))
165 ('gen expect files', 'echo 0 >$F0; echo 1 >$F1; echo 2 >$F2', 0, None),
166 ('verify 2 src objs', 'gsutil ls gs://$B2 | wc -l >$F9', 0, ('$F9', '$F2')),
167 ('verify 0 dst objs', 'gsutil ls gs://$B0 | wc -l >$F9', 0, ('$F9', '$F0')),
168 ('mv 2 objects',
169 'gsutil -m mv gs://$B2/$O0 gs://$B2/$O1 gs://$B0 2>&1 | grep Removing',
170 0, None),
171 ('verify 0 src objs', 'gsutil ls gs://$B2 | wc -l >$F9', 0, ('$F9', '$F0')),
172 ('verify 2 dst objs', 'gsutil ls gs://$B0 | wc -l >$F9', 0, ('$F9', '$F2')),
173 ('rm 1 src object', 'gsutil rm gs://$B0/$O0', 0, None),
174 ('verify 1 src obj', 'gsutil ls gs://$B0 | wc -l >$F9', 0, ('$F9', '$F1')),
175 ('verify 0 dst objs', 'gsutil ls gs://$B2 | wc -l >$F9', 0, ('$F9', '$F0')),
176 ('mv 2 objects', 'gsutil -m mv gs://$B0/$O0 gs://$B0/$O1 gs://$B2', 1, None) ,
177 ]
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698