[go: nahoru, domu]

blob: ad938072d59e0b91a6677e1785eb7769bb210874 [file] [log] [blame]
Takuto Ikuta3dab32e02023-01-12 18:52:001#!/usr/bin/env python3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2013 The Chromium Authors
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:143# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Jon Kunkeec89a09e2019-02-06 22:57:246description = """
7Make a symlink and optionally touch a file (to handle dependencies).
8"""
9usage = "%prog [options] source[ source ...] linkname"
Nico Weber4054b0c2021-03-28 18:23:2210epilog = """\
11A symlink to source is created at linkname. If multiple sources are specified,
Jon Kunkeec89a09e2019-02-06 22:57:2412then linkname is assumed to be a directory, and will contain all the links to
bpastenea516b282016-03-21 17:52:2813the sources (basenames identical to their source).
Jon Kunkeec89a09e2019-02-06 22:57:2414
15On Windows, this will use hard links (mklink /H) to avoid requiring elevation.
16This means that if the original is deleted and replaced, the link will still
Nico Weber4054b0c2021-03-28 18:23:2217have the old contents.
bpastenea516b282016-03-21 17:52:2818"""
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1419
20import errno
21import optparse
22import os.path
eseideladd100b2015-07-01 19:09:4023import shutil
Jon Kunkeec89a09e2019-02-06 22:57:2424import subprocess
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1425import sys
26
27
28def Main(argv):
Jon Kunkeec89a09e2019-02-06 22:57:2429 parser = optparse.OptionParser(usage=usage, description=description,
30 epilog=epilog)
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1431 parser.add_option('-f', '--force', action='store_true')
32 parser.add_option('--touch')
33
mostynb@opera.com4afc8d672013-05-28 21:49:1134 options, args = parser.parse_args(argv[1:])
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1435 if len(args) < 2:
36 parser.error('at least two arguments required.')
37
38 target = args[-1]
39 sources = args[:-1]
40 for s in sources:
41 t = os.path.join(target, os.path.basename(s))
agrieve6cc97ff2015-07-15 20:13:1542 if len(sources) == 1 and not os.path.isdir(target):
43 t = target
bpastenea516b282016-03-21 17:52:2844 t = os.path.expanduser(t)
Jon Kunkeec89a09e2019-02-06 22:57:2445 if os.path.realpath(t) == os.path.realpath(s):
bpastenee1758d32016-01-26 00:31:5046 continue
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1447 try:
Jon Kunkeec89a09e2019-02-06 22:57:2448 # 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.
53 if os.name == 'nt':
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)
60 else:
61 os.symlink(s, t)
Raul Tambref7d4453b2019-09-26 19:20:5362 except OSError as e:
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1463 if e.errno == errno.EEXIST and options.force:
eseideladd100b2015-07-01 19:09:4064 if os.path.isdir(t):
65 shutil.rmtree(t, ignore_errors=True)
66 else:
67 os.remove(t)
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1468 os.symlink(s, t)
69 else:
70 raise
Raul Tambref7d4453b2019-09-26 19:20:5371 except subprocess.CalledProcessError as e:
Jon Kunkeec89a09e2019-02-06 22:57:2472 # Since subprocess.check_output does not return an easily checked error
73 # number, in the 'force' case always assume it is 'file already exists'
74 # and retry.
75 if options.force:
76 if os.path.isdir(t):
77 shutil.rmtree(t, ignore_errors=True)
78 else:
79 os.remove(t)
80 subprocess.check_output(e.cmd, stderr=subprocess.STDOUT)
81 else:
82 raise
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1483
84
85 if options.touch:
Joshua Pawlicki45c33fb72023-02-13 19:52:4386 os.makedirs(os.path.dirname(options.touch), exist_ok=True)
Nico Weber4054b0c2021-03-28 18:23:2287 with open(options.touch, 'w'):
phajdan.jr@chromium.orga2e338ed2013-01-22 17:57:1488 pass
89
90
91if __name__ == '__main__':
92 sys.exit(Main(sys.argv))