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 Usage: | 8 Usage: |
9 symlink.py [options] sources... target | 9 symlink.py [options] sources... target |
10 | 10 |
11 A sym link to source is created at target. If multiple sources are specfied, | 11 A sym link to source is created at target. If multiple sources are specfied, |
12 then target is assumed to be a directory, and will contain all the links to | 12 then target is assumed to be a directory, and will contain all the links to |
13 the sources (basenames identical to their source). | 13 the sources (basenames identical to their source). |
14 """ | 14 """ |
15 | 15 |
16 import errno | 16 import errno |
17 import optparse | 17 import optparse |
18 import os.path | 18 import os.path |
19 import shutil | 19 import shutil |
20 import sys | 20 import sys |
21 | 21 |
22 | 22 |
23 def Main(argv): | 23 def Main(argv): |
24 parser = optparse.OptionParser() | 24 parser = optparse.OptionParser() |
25 parser.add_option('-f', '--force', action='store_true') | 25 parser.add_option('-f', '--force', action='store_true') |
26 parser.add_option('--touch') | 26 parser.add_option('--touch') |
27 parser.add_option('--update-target-mtimes', action='store_true') | |
28 | 27 |
29 options, args = parser.parse_args(argv[1:]) | 28 options, args = parser.parse_args(argv[1:]) |
30 if len(args) < 2: | 29 if len(args) < 2: |
31 parser.error('at least two arguments required.') | 30 parser.error('at least two arguments required.') |
32 | 31 |
33 target = args[-1] | 32 target = args[-1] |
34 sources = args[:-1] | 33 sources = args[:-1] |
35 for s in sources: | 34 for s in sources: |
36 t = os.path.join(target, os.path.basename(s)) | 35 t = os.path.join(target, os.path.basename(s)) |
37 if len(sources) == 1 and not os.path.isdir(target): | 36 if len(sources) == 1 and not os.path.isdir(target): |
38 t = target | 37 t = target |
39 t = os.path.expanduser(t) | 38 t = os.path.expanduser(t) |
40 if os.path.realpath(t) == s: | 39 if os.path.realpath(t) == s: |
41 continue | 40 continue |
42 try: | 41 try: |
43 os.symlink(s, t) | 42 os.symlink(s, t) |
44 except OSError, e: | 43 except OSError, e: |
45 if e.errno == errno.EEXIST and options.force: | 44 if e.errno == errno.EEXIST and options.force: |
46 if os.path.isdir(t): | 45 if os.path.isdir(t): |
47 shutil.rmtree(t, ignore_errors=True) | 46 shutil.rmtree(t, ignore_errors=True) |
48 else: | 47 else: |
49 os.remove(t) | 48 os.remove(t) |
50 os.symlink(s, t) | 49 os.symlink(s, t) |
51 else: | 50 else: |
52 raise | 51 raise |
53 if options.update_target_mtimes: | |
54 # Work-around for ninja bug: | |
55 # https://github.com/ninja-build/ninja/issues/1186 | |
56 os.utime(s, None) | |
57 | 52 |
58 | 53 |
59 if options.touch: | 54 if options.touch: |
60 with open(options.touch, 'w') as f: | 55 with open(options.touch, 'w') as f: |
61 pass | 56 pass |
62 | 57 |
63 | 58 |
64 if __name__ == '__main__': | 59 if __name__ == '__main__': |
65 sys.exit(Main(sys.argv)) | 60 sys.exit(Main(sys.argv)) |
OLD | NEW |