Skip to content
Snippets Groups Projects
Verified Commit e5b65c65 authored by Renato Alves's avatar Renato Alves :seedling:
Browse files

ENH Add symlink_[absolute|relative].py for symlink handling

parent b1b05f83
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,8 @@ EMBL utilities
``mv_keep_parents`` - equivalent to ``cp --parents src/*.txt dest/`` but moving instead of copying. Passing ``-k`` will keep empty directories in ``src/``.
``symlink_relative.py`` and ``symlink_absolute.py`` - convert symlinks to relative/absolute respectively.
Examples
========
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
def handle_files(files):
for file in files:
if os.path.islink(file):
real = os.path.realpath(file)
old = file + ".old"
os.rename(file, old)
os.symlink(real, file)
os.remove(old)
print(file, "is now absolute pointing at", real)
else:
print(file, "is not a symlink or is broken")
def usage():
print("This script is useful to transform relative symlinks into absolute.")
print("As a side-effect if the symlink points to chain of symlinks these "
"will be resolved such that the new symlink points directly to the "
"original file")
print("\nSee also: symlink_relative.py\n")
print("\nUsage:")
print(" ", sys.argv[0], "<symlink1> [<symlink2> ...]\n")
def main():
files = sys.argv[1:]
if not files:
usage()
sys.exit(1)
handle_files(files)
if __name__ == "__main__":
main()
# vim: ai sts=4 et sw=4
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
def handle_files(files):
for file in files:
if os.path.islink(file):
real = os.path.realpath(file)
dest = os.path.realpath(os.path.dirname(file))
relative = os.path.relpath(real, dest)
old = file + ".old"
os.rename(file, old)
os.symlink(relative, file)
os.remove(old)
print(file, "is now relative pointing at", relative)
else:
print(file, "is not a symlink or is broken")
def usage():
print("This script is useful to transform absolute symlinks into relative "
"with regards to the location the symlink is stored in.")
print("As a side-effect if the symlink points to chain of symlinks these "
"will be resolved such that the new symlink points directly to the "
"original file")
print("\nSee also: symlink_absolute.py\n")
print("\nUsage:")
print(" ", sys.argv[0], "<symlink1> [<symlink2> ...]\n")
def main():
files = sys.argv[1:]
if not files:
usage()
sys.exit(1)
handle_files(files)
if __name__ == "__main__":
main()
# vim: ai sts=4 et sw=4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment