Patchwork [2/3] scripts: add lnr (link relative)

login
register
mail settings
Submitter Ross Burton
Date March 3, 2014, 8:23 p.m.
Message ID <1393878215-27447-2-git-send-email-ross.burton@intel.com>
Download mbox | patch
Permalink /patch/67903/
State Accepted
Commit 1412dda5609a544d46f4b67bd3a27bbe2c7f4fea
Headers show

Comments

Ross Burton - March 3, 2014, 8:23 p.m.
lnr is a simple script to generate relative symlinks from absolute paths,
similar to "ln -r" but without requiring coreutils 8.16 (Ubuntu 12.04 and others
currently ship 8.13).

Signed-off-by: Ross Burton <ross.burton@intel.com>
---
 scripts/lnr |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 scripts/lnr

Patch

diff --git a/scripts/lnr b/scripts/lnr
new file mode 100755
index 0000000..9dacebe
--- /dev/null
+++ b/scripts/lnr
@@ -0,0 +1,21 @@ 
+#! /usr/bin/env python
+
+# Create a *relative* symlink, just like ln --relative does but without needing
+# coreutils 8.16.
+
+import sys, os
+
+if len(sys.argv) != 3:
+   print "$ lnr TARGET LINK_NAME"
+   sys.exit(1)
+
+target = sys.argv[1]
+linkname = sys.argv[2]
+
+if os.path.isabs(target):
+   if not os.path.isabs(linkname):
+      linkname = os.path.abspath(linkname)
+   start = os.path.dirname(linkname)
+   target = os.path.relpath(target, start)
+
+os.symlink(target, linkname)