phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [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 | |
bpastene | e51030a | 2016-03-22 02:52:28 +0900 | [diff] [blame] | 6 | """Make a symlink and optionally touch a file (to handle dependencies). |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 7 | |
bpastene | e51030a | 2016-03-22 02:52:28 +0900 | [diff] [blame] | 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 | """ |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 15 | |
| 16 | import errno |
| 17 | import optparse |
| 18 | import os.path |
eseidel | 05da2ed | 2015-07-02 04:09:40 +0900 | [diff] [blame] | 19 | import shutil |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 20 | import sys |
| 21 | |
| 22 | |
| 23 | def Main(argv): |
| 24 | parser = optparse.OptionParser() |
| 25 | parser.add_option('-f', '--force', action='store_true') |
| 26 | parser.add_option('--touch') |
| 27 | |
mostynb@opera.com | 9f6c0ea | 2013-05-29 06:49:11 +0900 | [diff] [blame] | 28 | options, args = parser.parse_args(argv[1:]) |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 29 | if len(args) < 2: |
| 30 | parser.error('at least two arguments required.') |
| 31 | |
| 32 | target = args[-1] |
| 33 | sources = args[:-1] |
| 34 | for s in sources: |
| 35 | t = os.path.join(target, os.path.basename(s)) |
agrieve | fabc024 | 2015-07-16 05:13:15 +0900 | [diff] [blame] | 36 | if len(sources) == 1 and not os.path.isdir(target): |
| 37 | t = target |
bpastene | e51030a | 2016-03-22 02:52:28 +0900 | [diff] [blame] | 38 | t = os.path.expanduser(t) |
bpastene | b63719c | 2016-01-26 09:31:50 +0900 | [diff] [blame] | 39 | if os.path.realpath(t) == s: |
| 40 | continue |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 41 | try: |
| 42 | os.symlink(s, t) |
| 43 | except OSError, e: |
| 44 | if e.errno == errno.EEXIST and options.force: |
eseidel | 05da2ed | 2015-07-02 04:09:40 +0900 | [diff] [blame] | 45 | if os.path.isdir(t): |
| 46 | shutil.rmtree(t, ignore_errors=True) |
| 47 | else: |
| 48 | os.remove(t) |
phajdan.jr@chromium.org | a15170a | 2013-01-23 02:57:14 +0900 | [diff] [blame] | 49 | os.symlink(s, t) |
| 50 | else: |
| 51 | raise |
| 52 | |
| 53 | |
| 54 | if options.touch: |
| 55 | with open(options.touch, 'w') as f: |
| 56 | pass |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | sys.exit(Main(sys.argv)) |