xref: /aosp_15_r20/external/angle/build/symlink.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/env python3
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.
5
6description = """
7Make a symlink.
8"""
9usage = "%prog [options] source[ source ...] linkname"
10epilog = """\
11A symlink to source is created at linkname. If multiple sources are specified,
12then linkname is assumed to be a directory, and will contain all the links to
13the sources (basenames identical to their source).
14
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
17have the old contents.
18"""
19
20import errno
21import optparse
22import os
23import shutil
24import subprocess
25import sys
26
27
28def Main(argv):
29  parser = optparse.OptionParser(usage=usage, description=description,
30                                 epilog=epilog)
31  parser.add_option('-f', '--force', action='store_true')
32
33  options, args = parser.parse_args(argv[1:])
34  if len(args) < 2:
35    parser.error('at least two arguments required.')
36
37  target = args[-1]
38  sources = args[:-1]
39  for s in sources:
40    t = os.path.join(target, os.path.basename(s))
41    if len(sources) == 1 and not os.path.isdir(target):
42      t = target
43    t = os.path.expanduser(t)
44    if os.path.realpath(t) == os.path.realpath(s):
45      continue
46    try:
47      # N.B. Python 2.x does not have os.symlink for Windows.
48      #   Python 3 has os.symlink for Windows, but requires either the admin-
49      #   granted privilege SeCreateSymbolicLinkPrivilege or, as of Windows 10
50      #   1703, that Developer Mode be enabled. Hard links and junctions do not
51      #   require any extra privileges to create.
52      if os.name == 'nt':
53        # mklink does not tolerate /-delimited path names.
54        t = t.replace('/', '\\')
55        s = s.replace('/', '\\')
56        # N.B. This tool only handles file hardlinks, not directory junctions.
57        subprocess.check_output(['cmd.exe', '/c', 'mklink', '/H', t, s],
58                                stderr=subprocess.STDOUT)
59      else:
60        os.symlink(s, t)
61    except OSError as e:
62      if e.errno == errno.EEXIST and options.force:
63        if os.path.isdir(t):
64          shutil.rmtree(t, ignore_errors=True)
65        else:
66          os.remove(t)
67        os.symlink(s, t)
68      else:
69        raise
70    except subprocess.CalledProcessError as e:
71      # Since subprocess.check_output does not return an easily checked error
72      # number, in the 'force' case always assume it is 'file already exists'
73      # and retry.
74      if options.force:
75        if os.path.isdir(t):
76          shutil.rmtree(t, ignore_errors=True)
77        else:
78          os.remove(t)
79        subprocess.check_output(e.cmd, stderr=subprocess.STDOUT)
80      else:
81        raise
82
83
84if __name__ == '__main__':
85  sys.exit(Main(sys.argv))
86