| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import argparse | 5 import argparse |
| 6 import errno |
| 6 import os | 7 import os |
| 7 import shutil | 8 import shutil |
| 9 import subprocess |
| 8 import sys | 10 import sys |
| 9 | 11 |
| 12 # Copies a macOS or iOS resource into a bundle. Special handling is given to |
| 13 # .strings files. |
| 14 # |
| 15 # Usage: python copy_bundle_data.py path/to/source path/of/destination |
| 16 # |
| 17 # Note that if |source| is a directory, its contents will be copied to |dest|, |
| 18 # rather than copying the |source| directory itself. Example: |
| 19 # copy_bundle_data.py out/Release/Foo.framework out/Release/App/Foo.framework |
| 20 # The contents of Foo.framwork will be copied into the destination path, which |
| 21 # includes the name of the destination framework, which is also Foo.framework. |
| 10 | 22 |
| 11 def DetectEncoding(data, default_encoding='UTF-8'): | 23 def DetectEncoding(data, default_encoding='UTF-8'): |
| 12 """Detects the encoding used by |data| from the Byte-Order-Mark if present. | 24 """Detects the encoding used by |data| from the Byte-Order-Mark if present. |
| 13 | 25 |
| 14 Args: | 26 Args: |
| 15 data: string whose encoding needs to be detected | 27 data: string whose encoding needs to be detected |
| 16 default_encoding: encoding returned if no BOM is found. | 28 default_encoding: encoding returned if no BOM is found. |
| 17 | 29 |
| 18 Returns: | 30 Returns: |
| 19 The encoding determined from the BOM if present or |default_encoding| if | 31 The encoding determined from the BOM if present or |default_encoding| if |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 dest_file.write(data) | 85 dest_file.write(data) |
| 74 | 86 |
| 75 | 87 |
| 76 def CopyFile(source, dest, strings_format): | 88 def CopyFile(source, dest, strings_format): |
| 77 """Copies a file or directory from |source| to |dest|. | 89 """Copies a file or directory from |source| to |dest|. |
| 78 | 90 |
| 79 Args: | 91 Args: |
| 80 source: string, path to the source file | 92 source: string, path to the source file |
| 81 dest: string, path to the destination file | 93 dest: string, path to the destination file |
| 82 """ | 94 """ |
| 83 if os.path.isdir(source): | 95 try: |
| 84 if os.path.exists(dest): | 96 shutil.rmtree(dest) |
| 85 shutil.rmtree(dest) | 97 except OSError as e: |
| 86 # Copy tree. | 98 if e.errno == errno.ENOENT: |
| 87 # TODO(thakis): This copies file attributes like mtime, while the | 99 pass |
| 88 # single-file branch below doesn't. This should probably be changed to | 100 elif e.errno == errno.ENOTDIR: |
| 89 # be consistent with the single-file branch. | 101 os.unlink(dest) |
| 90 shutil.copytree(source, dest, symlinks=True) | 102 else: |
| 91 return | 103 raise |
| 92 | |
| 93 if os.path.exists(dest): | |
| 94 os.unlink(dest) | |
| 95 | 104 |
| 96 _, extension = os.path.splitext(source) | 105 _, extension = os.path.splitext(source) |
| 97 if extension == '.strings': | 106 if extension == '.strings': |
| 98 CopyStringsFile(source, dest, strings_format) | 107 CopyStringsFile(source, dest, strings_format) |
| 99 return | 108 return |
| 100 | 109 |
| 101 shutil.copy(source, dest) | 110 # If the source is a directory, add a trailing slash so its contents get |
| 111 # copied, rather than copying the directory itself. |
| 112 if os.path.isdir(source) and not source.endswith('/'): |
| 113 source += '/' |
| 114 |
| 115 subprocess.check_call( |
| 116 ['rsync', '--recursive', '--perms', '--links', source, dest]) |
| 102 | 117 |
| 103 | 118 |
| 104 def Main(): | 119 def Main(): |
| 105 parser = argparse.ArgumentParser( | 120 parser = argparse.ArgumentParser( |
| 106 description='copy source to destination for the creation of a bundle') | 121 description='copy source to destination for the creation of a bundle') |
| 107 parser.add_argument('--strings-format', | 122 parser.add_argument('--strings-format', |
| 108 choices=('xml1', 'binary1', 'legacy'), default='legacy', | 123 choices=('xml1', 'binary1', 'legacy'), default='legacy', |
| 109 help='convert .strings file to format (default: %(default)s)') | 124 help='convert .strings file to format (default: %(default)s)') |
| 110 parser.add_argument('source', help='path to source file or directory') | 125 parser.add_argument('source', help='path to source file or directory') |
| 111 parser.add_argument('dest', help='path to destination') | 126 parser.add_argument('dest', help='path to destination') |
| 112 args = parser.parse_args() | 127 args = parser.parse_args() |
| 113 | 128 |
| 114 CopyFile(args.source, args.dest, args.strings_format) | 129 CopyFile(args.source, args.dest, args.strings_format) |
| 115 | 130 |
| 116 if __name__ == '__main__': | 131 if __name__ == '__main__': |
| 117 sys.exit(Main()) | 132 sys.exit(Main()) |
| OLD | NEW |