summaryrefslogtreecommitdiff
path: root/calculate-relative
diff options
context:
space:
mode:
authorMatthew Sackman <matthew@lshift.net>2009-08-14 13:40:30 +0100
committerMatthew Sackman <matthew@lshift.net>2009-08-14 13:40:30 +0100
commite84abd31f517f05e50e9c61c57d95f2f70a1bd46 (patch)
treeb029db82f1cfcb638b75f51f4a801a3ed8c08b94 /calculate-relative
parent345d4d0ceb837d6a69a91b26d63349fc83837152 (diff)
parent98206e0cc2dd7f6ba24a6b23f99b6f37cbf6d6e0 (diff)
downloadrabbitmq-server-git-e84abd31f517f05e50e9c61c57d95f2f70a1bd46.tar.gz
merge bug20342 into default
Diffstat (limited to 'calculate-relative')
-rwxr-xr-xcalculate-relative45
1 files changed, 45 insertions, 0 deletions
diff --git a/calculate-relative b/calculate-relative
new file mode 100755
index 0000000000..3c3e2b1ff6
--- /dev/null
+++ b/calculate-relative
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+#
+# relpath.py
+# R.Barran 30/08/2004
+# Retrieved from http://code.activestate.com/recipes/302594/
+
+import os
+import sys
+
+def relpath(target, base=os.curdir):
+ """
+ Return a relative path to the target from either the current dir or an optional base dir.
+ Base can be a directory specified either as absolute or relative to current dir.
+ """
+
+ if not os.path.exists(target):
+ raise OSError, 'Target does not exist: '+target
+
+ if not os.path.isdir(base):
+ raise OSError, 'Base is not a directory or does not exist: '+base
+
+ base_list = (os.path.abspath(base)).split(os.sep)
+ target_list = (os.path.abspath(target)).split(os.sep)
+
+ # On the windows platform the target may be on a completely different drive from the base.
+ if os.name in ['nt','dos','os2'] and base_list[0] <> target_list[0]:
+ raise OSError, 'Target is on a different drive to base. Target: '+target_list[0].upper()+', base: '+base_list[0].upper()
+
+ # Starting from the filepath root, work out how much of the filepath is
+ # shared by base and target.
+ for i in range(min(len(base_list), len(target_list))):
+ if base_list[i] <> target_list[i]: break
+ else:
+ # If we broke out of the loop, i is pointing to the first differing path elements.
+ # If we didn't break out of the loop, i is pointing to identical path elements.
+ # Increment i so that in all cases it points to the first differing path elements.
+ i+=1
+
+ rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:]
+ if (len(rel_list) == 0):
+ return "."
+ return os.path.join(*rel_list)
+
+if __name__ == "__main__":
+ print(relpath(sys.argv[1], sys.argv[2]))