# HG changeset patch # User Matt Mackall # Date 2006-12-30 02:04:31 # Node ID 0b740dcf0cf1a0304db330d6ebecd84c97afd087 # Parent 315d47991fd432d52cb0df2a5d93aaf66459d5d9 symlinks: add basic symlink functions to util.py diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -787,6 +787,9 @@ if os.name == 'nt': def set_exec(f, mode): pass + def set_link(f, mode): + pass + def set_binary(fd): msvcrt.setmode(fd.fileno(), os.O_BINARY) @@ -873,6 +876,30 @@ else: else: os.chmod(f, s & 0666) + def is_link(f): + """check whether a file is a symlink""" + return (os.lstat(f).st_mode & 0120000 == 0120000) + + def set_link(f, mode): + """make a file a symbolic link/regular file + + if a file is changed to a link, its contents become the link data + if a link is changed to a file, its link data become its contents + """ + + m = is_link(f) + if m == bool(mode): + return + + if mode: # switch file to link + data = file(f).read() + os.unlink(f) + os.symlink(data, f) + else: + data = os.readlink(f) + os.unlink(f) + file(f, "w").write(data) + def set_binary(fd): pass