symlink.py 3.0 KB

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