blob: 5a261dcad93e1bddc77b42312f552a12dc8c131f [file] [log] [blame]
[email protected]a2e338ed2013-01-22 17:57:141#!/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
bpastenea516b282016-03-21 17:52:286"""Make a symlink and optionally touch a file (to handle dependencies).
[email protected]a2e338ed2013-01-22 17:57:147
bpastenea516b282016-03-21 17:52:288Usage:
9 symlink.py [options] sources... target
10
11A sym link to source is created at target. If multiple sources are specfied,
12then target is assumed to be a directory, and will contain all the links to
13the sources (basenames identical to their source).
14"""
[email protected]a2e338ed2013-01-22 17:57:1415
16import errno
17import optparse
18import os.path
eseideladd100b2015-07-01 19:09:4019import shutil
[email protected]a2e338ed2013-01-22 17:57:1420import sys
21
22
23def Main(argv):
24 parser = optparse.OptionParser()
25 parser.add_option('-f', '--force', action='store_true')
26 parser.add_option('--touch')
27
[email protected]4afc8d672013-05-28 21:49:1128 options, args = parser.parse_args(argv[1:])
[email protected]a2e338ed2013-01-22 17:57:1429 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))
agrieve6cc97ff42015-07-15 20:13:1536 if len(sources) == 1 and not os.path.isdir(target):
37 t = target
bpastenea516b282016-03-21 17:52:2838 t = os.path.expanduser(t)
bpastenee1758d32016-01-26 00:31:5039 if os.path.realpath(t) == s:
40 continue
[email protected]a2e338ed2013-01-22 17:57:1441 try:
42 os.symlink(s, t)
43 except OSError, e:
44 if e.errno == errno.EEXIST and options.force:
eseideladd100b2015-07-01 19:09:4045 if os.path.isdir(t):
46 shutil.rmtree(t, ignore_errors=True)
47 else:
48 os.remove(t)
[email protected]a2e338ed2013-01-22 17:57:1449 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
59if __name__ == '__main__':
60 sys.exit(Main(sys.argv))