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 |
8 import sys | 9 import sys |
| 10 import subprocess |
9 | 11 |
10 | 12 |
11 def detect_encoding(data, default_encoding='UTF-8'): | 13 def detect_encoding(data, default_encoding='UTF-8'): |
12 """Detects the encoding used by |data| from the Byte-Order-Mark if present. | 14 """Detects the encoding used by |data| from the Byte-Order-Mark if present. |
13 | 15 |
14 Args: | 16 Args: |
15 data: string whose encoding needs to be detected | 17 data: string whose encoding needs to be detected |
16 default_encoding: encoding returned if no BOM is found. | 18 default_encoding: encoding returned if no BOM is found. |
17 | 19 |
18 Returns: | 20 Returns: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 dest_file.write(data.decode(encoding).encode('UTF-16')) | 59 dest_file.write(data.decode(encoding).encode('UTF-16')) |
58 | 60 |
59 | 61 |
60 def copy_file(source, dest): | 62 def copy_file(source, dest): |
61 """Copies a file or directory from |source| to |dest|. | 63 """Copies a file or directory from |source| to |dest|. |
62 | 64 |
63 Args: | 65 Args: |
64 source: string, path to the source file | 66 source: string, path to the source file |
65 dest: string, path to the destination file | 67 dest: string, path to the destination file |
66 """ | 68 """ |
67 if os.path.isdir(source): | 69 try: |
68 if os.path.exists(dest): | 70 shutil.rmtree(dest) |
69 shutil.rmtree(dest) | 71 except OSError as e: |
70 # Copy tree. | 72 if e.errno == errno.ENOENT: |
71 # TODO(thakis): This copies file attributes like mtime, while the | 73 pass |
72 # single-file branch below doesn't. This should probably be changed to | 74 elif e.errno == errno.ENOTDIR: |
73 # be consistent with the single-file branch. | 75 os.unlink(dest) |
74 shutil.copytree(source, dest, symlinks=True) | 76 else: |
75 return | 77 raise |
76 | |
77 if os.path.exists(dest): | |
78 os.unlink(dest) | |
79 | 78 |
80 _, extension = os.path.splitext(source) | 79 _, extension = os.path.splitext(source) |
81 if extension == '.strings': | 80 if extension == '.strings': |
82 copy_strings_file(source, dest) | 81 copy_strings_file(source, dest) |
83 return | 82 return |
84 | 83 |
85 shutil.copy(source, dest) | 84 # Strip trailing slashes on the source so rsync copies the source as a |
| 85 # directory. |
| 86 source = source.rstrip('/') |
| 87 |
| 88 subprocess.check_call( |
| 89 ['rsync', '--recursive', '--perms', '--links', source, dest]) |
86 | 90 |
87 | 91 |
88 def main(): | 92 def main(): |
89 parser = argparse.ArgumentParser( | 93 parser = argparse.ArgumentParser( |
90 description='copy source to destination for the creation of a bundle') | 94 description='copy source to destination for the creation of a bundle') |
91 parser.add_argument('source', help='path to source file or directory') | 95 parser.add_argument('source', help='path to source file or directory') |
92 parser.add_argument('dest', help='path to destination') | 96 parser.add_argument('dest', help='path to destination') |
93 args = parser.parse_args() | 97 args = parser.parse_args() |
94 | 98 |
95 copy_file(args.source, args.dest) | 99 copy_file(args.source, args.dest) |
96 | 100 |
97 if __name__ == '__main__': | 101 if __name__ == '__main__': |
98 main() | 102 main() |
OLD | NEW |