github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/tools/noms/symlink.py (about)

     1  #!/usr/bin/python
     2  
     3  # Copyright 2016 Attic Labs, Inc. All rights reserved.
     4  # Licensed under the Apache License, version 2.0:
     5  # http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  import os
     8  
     9  class LinkError(Exception):
    10      """Raised when forcing a symlink fails for a non-OS reason."""
    11      pass
    12  
    13  def Force(source, linkName):
    14      """
    15      Force forces linkName to be a symlink to source, as long as its not a dir.
    16      Creates a symlink from linkName to source, clobbering linkName as long as its not a directory.
    17      """
    18      if not os.path.lexists(linkName):
    19          os.symlink(source, linkName)
    20          return
    21  
    22      if os.path.islink(linkName) or os.path.isfile(linkName):
    23          os.remove(linkName)
    24          os.symlink(source, linkName)
    25          return
    26  
    27      raise LinkError("Refusing to clobber " + linkName)