2 # Copyright 2013 The Chromium Authors
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 Make a symlink and optionally touch a file (to handle dependencies).
9 usage = "%prog [options] source[ source ...] linkname"
11 A symlink to source is created at linkname. If multiple sources are specified,
12 then linkname is assumed to be a directory, and will contain all the links to
13 the sources (basenames identical to their source).
15 On Windows, this will use hard links (mklink /H) to avoid requiring elevation.
16 This means that if the original is deleted and replaced, the link will still
17 have the old contents.
29 parser = optparse.OptionParser(usage=usage, description=description,
31 parser.add_option('-f', '--force', action='store_true')
32 parser.add_option('--touch')
34 options, args = parser.parse_args(argv[1:])
36 parser.error('at least two arguments required.')
41 t = os.path.join(target, os.path.basename(s))
42 if len(sources) == 1 and not os.path.isdir(target):
44 t = os.path.expanduser(t)
45 if os.path.realpath(t) == os.path.realpath(s):
48 # N.B. Python 2.x does not have os.symlink for Windows.
49 # Python 3 has os.symlink for Windows, but requires either the admin-
50 # granted privilege SeCreateSymbolicLinkPrivilege or, as of Windows 10
51 # 1703, that Developer Mode be enabled. Hard links and junctions do not
52 # require any extra privileges to create.
54 # mklink does not tolerate /-delimited path names.
55 t = t.replace('/', '\\')
56 s = s.replace('/', '\\')
57 # N.B. This tool only handles file hardlinks, not directory junctions.
58 subprocess.check_output(['cmd.exe', '/c', 'mklink', '/H', t, s],
59 stderr=subprocess.STDOUT)
63 if e.errno == errno.EEXIST and options.force:
65 shutil.rmtree(t, ignore_errors=True)
71 except subprocess.CalledProcessError as e:
72 # Since subprocess.check_output does not return an easily checked error
73 # number, in the 'force' case always assume it is 'file already exists'
77 shutil.rmtree(t, ignore_errors=True)
80 subprocess.check_output(e.cmd, stderr=subprocess.STDOUT)
86 os.makedirs(os.path.dirname(options.touch), exist_ok=True)
87 with open(options.touch, 'w'):
91 if __name__ == '__main__':
92 sys.exit(Main(sys.argv))