OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Make a symlink and optionally touch a file (to handle dependencies).""" | 6 """Make a symlink and optionally touch a file (to handle dependencies).""" |
7 | 7 |
8 | 8 |
9 import errno | 9 import errno |
10 import optparse | 10 import optparse |
11 import os.path | 11 import os.path |
12 import shutil | 12 import shutil |
13 import sys | 13 import sys |
14 | 14 |
15 | 15 |
16 def Main(argv): | 16 def Main(argv): |
17 parser = optparse.OptionParser() | 17 parser = optparse.OptionParser() |
18 parser.add_option('-f', '--force', action='store_true') | 18 parser.add_option('-f', '--force', action='store_true') |
19 parser.add_option('--touch') | 19 parser.add_option('--touch') |
20 | 20 |
21 options, args = parser.parse_args(argv[1:]) | 21 options, args = parser.parse_args(argv[1:]) |
22 if len(args) < 2: | 22 if len(args) < 2: |
23 parser.error('at least two arguments required.') | 23 parser.error('at least two arguments required.') |
24 | 24 |
25 target = args[-1] | 25 target = args[-1] |
26 sources = args[:-1] | 26 sources = args[:-1] |
27 for s in sources: | 27 for s in sources: |
28 t = os.path.join(target, os.path.basename(s)) | 28 t = os.path.join(target, os.path.basename(s)) |
| 29 if len(sources) == 1 and not os.path.isdir(target): |
| 30 t = target |
29 try: | 31 try: |
30 os.symlink(s, t) | 32 os.symlink(s, t) |
31 except OSError, e: | 33 except OSError, e: |
32 if e.errno == errno.EEXIST and options.force: | 34 if e.errno == errno.EEXIST and options.force: |
33 if os.path.isdir(t): | 35 if os.path.isdir(t): |
34 shutil.rmtree(t, ignore_errors=True) | 36 shutil.rmtree(t, ignore_errors=True) |
35 else: | 37 else: |
36 os.remove(t) | 38 os.remove(t) |
37 os.symlink(s, t) | 39 os.symlink(s, t) |
38 else: | 40 else: |
39 raise | 41 raise |
40 | 42 |
41 | 43 |
42 if options.touch: | 44 if options.touch: |
43 with open(options.touch, 'w') as f: | 45 with open(options.touch, 'w') as f: |
44 pass | 46 pass |
45 | 47 |
46 | 48 |
47 if __name__ == '__main__': | 49 if __name__ == '__main__': |
48 sys.exit(Main(sys.argv)) | 50 sys.exit(Main(sys.argv)) |
OLD | NEW |