| [email protected] | a2e338ed | 2013-01-22 17:57:14 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 | ||||
| 4 | # found in the LICENSE file. | ||||
| 5 | |||||
| 6 | """Make a symlink and optionally touch a file (to handle dependencies).""" | ||||
| 7 | |||||
| 8 | |||||
| 9 | import errno | ||||
| 10 | import optparse | ||||
| 11 | import os.path | ||||
| eseidel | add100b | 2015-07-01 19:09:40 | [diff] [blame] | 12 | import shutil |
| [email protected] | a2e338ed | 2013-01-22 17:57:14 | [diff] [blame] | 13 | import sys |
| 14 | |||||
| 15 | |||||
| 16 | def Main(argv): | ||||
| 17 | parser = optparse.OptionParser() | ||||
| 18 | parser.add_option('-f', '--force', action='store_true') | ||||
| 19 | parser.add_option('--touch') | ||||
| 20 | |||||
| [email protected] | 4afc8d67 | 2013-05-28 21:49:11 | [diff] [blame] | 21 | options, args = parser.parse_args(argv[1:]) |
| [email protected] | a2e338ed | 2013-01-22 17:57:14 | [diff] [blame] | 22 | if len(args) < 2: |
| 23 | parser.error('at least two arguments required.') | ||||
| 24 | |||||
| 25 | target = args[-1] | ||||
| 26 | sources = args[:-1] | ||||
| 27 | for s in sources: | ||||
| 28 | t = os.path.join(target, os.path.basename(s)) | ||||
| agrieve | 6cc97ff4 | 2015-07-15 20:13:15 | [diff] [blame^] | 29 | if len(sources) == 1 and not os.path.isdir(target): |
| 30 | t = target | ||||
| [email protected] | a2e338ed | 2013-01-22 17:57:14 | [diff] [blame] | 31 | try: |
| 32 | os.symlink(s, t) | ||||
| 33 | except OSError, e: | ||||
| 34 | if e.errno == errno.EEXIST and options.force: | ||||
| eseidel | add100b | 2015-07-01 19:09:40 | [diff] [blame] | 35 | if os.path.isdir(t): |
| 36 | shutil.rmtree(t, ignore_errors=True) | ||||
| 37 | else: | ||||
| 38 | os.remove(t) | ||||
| [email protected] | a2e338ed | 2013-01-22 17:57:14 | [diff] [blame] | 39 | os.symlink(s, t) |
| 40 | else: | ||||
| 41 | raise | ||||
| 42 | |||||
| 43 | |||||
| 44 | if options.touch: | ||||
| 45 | with open(options.touch, 'w') as f: | ||||
| 46 | pass | ||||
| 47 | |||||
| 48 | |||||
| 49 | if __name__ == '__main__': | ||||
| 50 | sys.exit(Main(sys.argv)) | ||||