| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2011 Google Inc. All Rights Reserved. | 2 # Copyright 2011 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # 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 not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 6 # | 7 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 9 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 14 # limitations under the License. |
| 15 """Implementation of Unix-like mv command for cloud storage providers.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 14 | 18 |
| 15 from gslib.command import Command | 19 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 FILE_URIS_OK | |
| 19 from gslib.command import MAX_ARGS | |
| 20 from gslib.command import MIN_ARGS | |
| 21 from gslib.command import PROVIDER_URIS_OK | |
| 22 from gslib.command import SUPPORTED_SUB_ARGS | |
| 23 from gslib.command import URIS_START_ARG | |
| 24 from gslib.commands.cp import CP_SUB_ARGS | 20 from gslib.commands.cp import CP_SUB_ARGS |
| 21 from gslib.cs_api_map import ApiSelector |
| 25 from gslib.exception import CommandException | 22 from gslib.exception import CommandException |
| 26 from gslib.help_provider import HELP_NAME | 23 from gslib.storage_url import StorageUrlFromString |
| 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 | 24 from gslib.util import NO_MAX |
| 33 | 25 |
| 34 _detailed_help_text = (""" | 26 _DETAILED_HELP_TEXT = (""" |
| 35 <B>SYNOPSIS</B> | 27 <B>SYNOPSIS</B> |
| 36 gsutil mv [-p] src_uri dst_uri | 28 gsutil mv [-p] src_url dst_url |
| 37 gsutil mv [-p] uri... dst_uri | 29 gsutil mv [-p] url... dst_url |
| 38 | 30 |
| 39 | 31 |
| 40 <B>DESCRIPTION</B> | 32 <B>DESCRIPTION</B> |
| 41 The gsutil mv command allows you to move data between your local file | 33 The gsutil mv command allows you to move data between your local file |
| 42 system and the cloud, move data within the cloud, and move data between | 34 system and the cloud, move data within the cloud, and move data between |
| 43 cloud storage providers. For example, to move all objects from a | 35 cloud storage providers. For example, to move all objects from a |
| 44 bucket to a local directory you could use: | 36 bucket to a local directory you could use: |
| 45 | 37 |
| 46 gsutil mv gs://my_bucket dir | 38 gsutil mv gs://my_bucket dir |
| 47 | 39 |
| 48 Similarly, to move all objects from a local directory to a bucket you could | 40 Similarly, to move all objects from a local directory to a bucket you could |
| 49 use: | 41 use: |
| 50 | 42 |
| 51 gsutil mv ./dir gs://my_bucket | 43 gsutil mv ./dir gs://my_bucket |
| 52 | 44 |
| 53 | 45 |
| 54 <B>RENAMING BUCKET SUBDIRECTORIES</B> | 46 <B>RENAMING BUCKET SUBDIRECTORIES</B> |
| 55 You can use the gsutil mv command to rename subdirectories. For example, | 47 You can use the gsutil mv command to rename subdirectories. For example, |
| 56 the command: | 48 the command: |
| 57 | 49 |
| 58 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir | 50 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir |
| 59 | 51 |
| 60 would rename all objects and subdirectories under gs://my_bucket/olddir to be | 52 would rename all objects and subdirectories under gs://my_bucket/olddir to be |
| 61 under gs://my_bucket/newdir, otherwise preserving the subdirectory structure. | 53 under gs://my_bucket/newdir, otherwise preserving the subdirectory structure. |
| 62 | 54 |
| 63 If you do a rename as specified above and you want to preserve ACLs, you | 55 If you do a rename as specified above and you want to preserve ACLs, you |
| 64 should use the -p option (see OPTIONS). | 56 should use the -p option (see OPTIONS). |
| 65 | 57 |
| 66 Note that when using mv to rename bucket subdirectories you cannot specify | 58 Note that when using mv to rename bucket subdirectories you cannot specify |
| 67 the source URI using wildcards. You need to spell out the complete name: | 59 the source URL using wildcards. You need to spell out the complete name: |
| 68 | 60 |
| 69 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir | 61 gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir |
| 70 | 62 |
| 71 If you have a large number of files to move you might want to use the | 63 If you have a large number of files to move you might want to use the |
| 72 gsutil -m option, to perform a multi-threaded/multi-processing move: | 64 gsutil -m option, to perform a multi-threaded/multi-processing move: |
| 73 | 65 |
| 74 gsutil -m mv gs://my_bucket/olddir gs://my_bucket/newdir | 66 gsutil -m mv gs://my_bucket/olddir gs://my_bucket/newdir |
| 75 | 67 |
| 76 | 68 |
| 77 <B>NON-ATOMIC OPERATION</B> | 69 <B>NON-ATOMIC OPERATION</B> |
| 78 Unlike the case with many file systems, the gsutil mv command does not | 70 Unlike the case with many file systems, the gsutil mv command does not |
| 79 perform a single atomic operation. Rather, it performs a copy from source | 71 perform a single atomic operation. Rather, it performs a copy from source |
| 80 to destination followed by removing the source for each object. | 72 to destination followed by removing the source for each object. |
| 81 | 73 |
| 82 | 74 |
| 83 <B>OPTIONS</B> | 75 <B>OPTIONS</B> |
| 84 All options that are available for the gsutil cp command are also available | 76 All options that are available for the gsutil cp command are also available |
| 85 for the gsutil mv command (except for the -R flag, which is implied by the | 77 for the gsutil mv command (except for the -R flag, which is implied by the |
| 86 gsutil mv command). Please see the OPTIONS sections of "gsutil help cp" | 78 gsutil mv command). Please see the OPTIONS sections of "gsutil help cp" |
| 87 for more information. | 79 for more information. |
| 88 | 80 |
| 89 """) | 81 """) |
| 90 | 82 |
| 91 | 83 |
| 92 class MvCommand(Command): | 84 class MvCommand(Command): |
| 93 """Implementation of gsutil mv command. | 85 """Implementation of gsutil mv command. |
| 86 |
| 94 Note that there is no atomic rename operation - this command is simply | 87 Note that there is no atomic rename operation - this command is simply |
| 95 a shorthand for 'cp' followed by 'rm'. | 88 a shorthand for 'cp' followed by 'rm'. |
| 96 """ | 89 """ |
| 97 | 90 |
| 98 # Command specification (processed by parent class). | 91 # Command specification. See base class for documentation. |
| 99 command_spec = { | 92 command_spec = Command.CreateCommandSpec( |
| 100 # Name of command. | 93 'mv', |
| 101 COMMAND_NAME : 'mv', | 94 command_name_aliases=['move', 'ren', 'rename'], |
| 102 # List of command name aliases. | 95 min_args=2, |
| 103 COMMAND_NAME_ALIASES : ['move', 'ren', 'rename'], | 96 max_args=NO_MAX, |
| 104 # Min number of args required by this command. | 97 # Flags for mv are passed through to cp. |
| 105 MIN_ARGS : 2, | 98 supported_sub_args=CP_SUB_ARGS, |
| 106 # Max number of args required by this command, or NO_MAX. | 99 file_url_ok=True, |
| 107 MAX_ARGS : NO_MAX, | 100 provider_url_ok=False, |
| 108 # Getopt-style string specifying acceptable sub args. | 101 urls_start_arg=0, |
| 109 SUPPORTED_SUB_ARGS : CP_SUB_ARGS, # Flags for mv are passed through to cp. | 102 gs_api_support=[ApiSelector.XML, ApiSelector.JSON], |
| 110 # True if file URIs acceptable for this command. | 103 gs_default_api=ApiSelector.JSON, |
| 111 FILE_URIS_OK : True, | 104 ) |
| 112 # True if provider-only URIs acceptable for this command. | 105 # Help specification. See help_provider.py for documentation. |
| 113 PROVIDER_URIS_OK : False, | 106 help_spec = Command.HelpSpec( |
| 114 # Index in args of first URI arg. | 107 help_name='mv', |
| 115 URIS_START_ARG : 0, | 108 help_name_aliases=['move', 'rename'], |
| 116 } | 109 help_type='command_help', |
| 117 help_spec = { | 110 help_one_line_summary='Move/rename objects and/or subdirectories', |
| 118 # Name of command or auxiliary help info for which this help applies. | 111 help_text=_DETAILED_HELP_TEXT, |
| 119 HELP_NAME : 'mv', | 112 subcommand_help_text={}, |
| 120 # List of help name aliases. | 113 ) |
| 121 HELP_NAME_ALIASES : ['move', 'rename'], | |
| 122 # Type of help: | |
| 123 HELP_TYPE : HelpType.COMMAND_HELP, | |
| 124 # One line summary of this help. | |
| 125 HELP_ONE_LINE_SUMMARY : 'Move/rename objects and/or subdirectories', | |
| 126 # The full help text. | |
| 127 HELP_TEXT : _detailed_help_text, | |
| 128 } | |
| 129 | 114 |
| 130 # Command entry point. | |
| 131 def RunCommand(self): | 115 def RunCommand(self): |
| 132 # Check each source arg up, refusing to delete a bucket src URI (force users | 116 """Command entry point for the mv command.""" |
| 117 # Check each source arg up, refusing to delete a bucket src URL (force users |
| 133 # to explicitly do that as a separate operation). | 118 # to explicitly do that as a separate operation). |
| 134 for arg_to_check in self.args[0:-1]: | 119 for arg_to_check in self.args[0:-1]: |
| 135 if self.suri_builder.StorageUri(arg_to_check).names_bucket(): | 120 url = StorageUrlFromString(arg_to_check) |
| 121 if url.IsCloudUrl() and (url.IsBucket() or url.IsProvider()): |
| 136 raise CommandException('You cannot move a source bucket using the mv ' | 122 raise CommandException('You cannot move a source bucket using the mv ' |
| 137 'command. If you meant to move\nall objects in ' | 123 'command. If you meant to move\nall objects in ' |
| 138 'the bucket, you can use a command like:\n' | 124 'the bucket, you can use a command like:\n' |
| 139 '\tgsutil mv %s/* %s' % | 125 '\tgsutil mv %s/* %s' % |
| 140 (arg_to_check, self.args[-1])) | 126 (arg_to_check, self.args[-1])) |
| 141 | 127 |
| 142 # Insert command-line opts in front of args so they'll be picked up by cp | 128 # Insert command-line opts in front of args so they'll be picked up by cp |
| 143 # and rm commands (e.g., for -p option). Use undocumented (internal | 129 # and rm commands (e.g., for -p option). Use undocumented (internal |
| 144 # use-only) cp -M option, which causes each original object to be deleted | 130 # use-only) cp -M option, which causes each original object to be deleted |
| 145 # after successfully copying to its destination, and also causes naming | 131 # after successfully copying to its destination, and also causes naming |
| 146 # behavior consistent with Unix mv naming behavior (see comments in | 132 # behavior consistent with Unix mv naming behavior (see comments in |
| 147 # _ConstructDstUri in cp.py). | 133 # ConstructDstUrl). |
| 148 unparsed_args = ['-M'] | 134 unparsed_args = ['-M'] |
| 149 if self.recursion_requested: | 135 if self.recursion_requested: |
| 150 unparsed_args.append('-R') | 136 unparsed_args.append('-R') |
| 151 unparsed_args.extend(self.unparsed_args) | 137 unparsed_args.extend(self.unparsed_args) |
| 152 self.command_runner.RunNamedCommand('cp', unparsed_args, self.headers, | 138 self.command_runner.RunNamedCommand('cp', unparsed_args, self.headers, |
| 153 self.debug, self.parallel_operations) | 139 self.debug, self.parallel_operations) |
| 154 | 140 |
| 155 return 0 | 141 return 0 |
| OLD | NEW |