| 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: |
| 9 symlink.py [options] sources... target |
| 10 |
| 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 |
| 13 the sources (basenames identical to their source). |
| 14 """ |
| 8 | 15 |
| 9 import errno | 16 import errno |
| 10 import optparse | 17 import optparse |
| 11 import os.path | 18 import os.path |
| 12 import shutil | 19 import shutil |
| 13 import sys | 20 import sys |
| 14 | 21 |
| 15 | 22 |
| 16 def Main(argv): | 23 def Main(argv): |
| 17 parser = optparse.OptionParser() | 24 parser = optparse.OptionParser() |
| 18 parser.add_option('-f', '--force', action='store_true') | 25 parser.add_option('-f', '--force', action='store_true') |
| 19 parser.add_option('--touch') | 26 parser.add_option('--touch') |
| 20 | 27 |
| 21 options, args = parser.parse_args(argv[1:]) | 28 options, args = parser.parse_args(argv[1:]) |
| 22 if len(args) < 2: | 29 if len(args) < 2: |
| 23 parser.error('at least two arguments required.') | 30 parser.error('at least two arguments required.') |
| 24 | 31 |
| 25 target = args[-1] | 32 target = args[-1] |
| 26 sources = args[:-1] | 33 sources = args[:-1] |
| 27 for s in sources: | 34 for s in sources: |
| 28 t = os.path.join(target, os.path.basename(s)) | 35 t = os.path.join(target, os.path.basename(s)) |
| 29 if len(sources) == 1 and not os.path.isdir(target): | 36 if len(sources) == 1 and not os.path.isdir(target): |
| 30 t = target | 37 t = target |
| 38 t = os.path.expanduser(t) |
| 31 if os.path.realpath(t) == s: | 39 if os.path.realpath(t) == s: |
| 32 continue | 40 continue |
| 33 try: | 41 try: |
| 34 os.symlink(s, t) | 42 os.symlink(s, t) |
| 35 except OSError, e: | 43 except OSError, e: |
| 36 if e.errno == errno.EEXIST and options.force: | 44 if e.errno == errno.EEXIST and options.force: |
| 37 if os.path.isdir(t): | 45 if os.path.isdir(t): |
| 38 shutil.rmtree(t, ignore_errors=True) | 46 shutil.rmtree(t, ignore_errors=True) |
| 39 else: | 47 else: |
| 40 os.remove(t) | 48 os.remove(t) |
| 41 os.symlink(s, t) | 49 os.symlink(s, t) |
| 42 else: | 50 else: |
| 43 raise | 51 raise |
| 44 | 52 |
| 45 | 53 |
| 46 if options.touch: | 54 if options.touch: |
| 47 with open(options.touch, 'w') as f: | 55 with open(options.touch, 'w') as f: |
| 48 pass | 56 pass |
| 49 | 57 |
| 50 | 58 |
| 51 if __name__ == '__main__': | 59 if __name__ == '__main__': |
| 52 sys.exit(Main(sys.argv)) | 60 sys.exit(Main(sys.argv)) |
| OLD | NEW |