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