2013-01-22 21:57:14 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2019-02-07 01:57:24 +03:00
|
|
|
description = """
|
|
|
|
Make a symlink and optionally touch a file (to handle dependencies).
|
|
|
|
"""
|
|
|
|
usage = "%prog [options] source[ source ...] linkname"
|
|
|
|
epilog = """
|
|
|
|
A sym link to source is created at linkname. If multiple sources are specfied,
|
|
|
|
then linkname is assumed to be a directory, and will contain all the links to
|
2016-03-21 20:52:28 +03:00
|
|
|
the sources (basenames identical to their source).
|
2019-02-07 01:57:24 +03:00
|
|
|
|
|
|
|
On Windows, this will use hard links (mklink /H) to avoid requiring elevation.
|
|
|
|
This means that if the original is deleted and replaced, the link will still
|
|
|
|
have the old contents. This is not expected to interfere with the Chromium
|
|
|
|
build.
|
2016-03-21 20:52:28 +03:00
|
|
|
"""
|
2013-01-22 21:57:14 +04:00
|
|
|
|
|
|
|
import errno
|
|
|
|
import optparse
|
|
|
|
import os.path
|
2015-07-01 22:09:40 +03:00
|
|
|
import shutil
|
2019-02-07 01:57:24 +03:00
|
|
|
import subprocess
|
2013-01-22 21:57:14 +04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def Main(argv):
|
2019-02-07 01:57:24 +03:00
|
|
|
parser = optparse.OptionParser(usage=usage, description=description,
|
|
|
|
epilog=epilog)
|
2013-01-22 21:57:14 +04:00
|
|
|
parser.add_option('-f', '--force', action='store_true')
|
|
|
|
parser.add_option('--touch')
|
|
|
|
|
2013-05-29 01:49:11 +04:00
|
|
|
options, args = parser.parse_args(argv[1:])
|
2013-01-22 21:57:14 +04:00
|
|
|
if len(args) < 2:
|
|
|
|
parser.error('at least two arguments required.')
|
|
|
|
|
|
|
|
target = args[-1]
|
|
|
|
sources = args[:-1]
|
|
|
|
for s in sources:
|
|
|
|
t = os.path.join(target, os.path.basename(s))
|
2015-07-15 23:13:15 +03:00
|
|
|
if len(sources) == 1 and not os.path.isdir(target):
|
|
|
|
t = target
|
2016-03-21 20:52:28 +03:00
|
|
|
t = os.path.expanduser(t)
|
2019-02-07 01:57:24 +03:00
|
|
|
if os.path.realpath(t) == os.path.realpath(s):
|
2016-01-26 03:31:50 +03:00
|
|
|
continue
|
2013-01-22 21:57:14 +04:00
|
|
|
try:
|
2019-02-07 01:57:24 +03:00
|
|
|
# N.B. Python 2.x does not have os.symlink for Windows.
|
|
|
|
# Python 3 has os.symlink for Windows, but requires either the admin-
|
|
|
|
# granted privilege SeCreateSymbolicLinkPrivilege or, as of Windows 10
|
|
|
|
# 1703, that Developer Mode be enabled. Hard links and junctions do not
|
|
|
|
# require any extra privileges to create.
|
|
|
|
if os.name == 'nt':
|
|
|
|
# mklink does not tolerate /-delimited path names.
|
|
|
|
t = t.replace('/', '\\')
|
|
|
|
s = s.replace('/', '\\')
|
|
|
|
# N.B. This tool only handles file hardlinks, not directory junctions.
|
|
|
|
subprocess.check_output(['cmd.exe', '/c', 'mklink', '/H', t, s],
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
else:
|
|
|
|
os.symlink(s, t)
|
2019-09-26 22:20:53 +03:00
|
|
|
except OSError as e:
|
2013-01-22 21:57:14 +04:00
|
|
|
if e.errno == errno.EEXIST and options.force:
|
2015-07-01 22:09:40 +03:00
|
|
|
if os.path.isdir(t):
|
|
|
|
shutil.rmtree(t, ignore_errors=True)
|
|
|
|
else:
|
|
|
|
os.remove(t)
|
2013-01-22 21:57:14 +04:00
|
|
|
os.symlink(s, t)
|
|
|
|
else:
|
|
|
|
raise
|
2019-09-26 22:20:53 +03:00
|
|
|
except subprocess.CalledProcessError as e:
|
2019-02-07 01:57:24 +03:00
|
|
|
# Since subprocess.check_output does not return an easily checked error
|
|
|
|
# number, in the 'force' case always assume it is 'file already exists'
|
|
|
|
# and retry.
|
|
|
|
if options.force:
|
|
|
|
if os.path.isdir(t):
|
|
|
|
shutil.rmtree(t, ignore_errors=True)
|
|
|
|
else:
|
|
|
|
os.remove(t)
|
|
|
|
subprocess.check_output(e.cmd, stderr=subprocess.STDOUT)
|
|
|
|
else:
|
|
|
|
raise
|
2013-01-22 21:57:14 +04:00
|
|
|
|
|
|
|
|
|
|
|
if options.touch:
|
|
|
|
with open(options.touch, 'w') as f:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(Main(sys.argv))
|