summaryrefslogtreecommitdiff
path: root/reverse_path.py
diff options
context:
space:
mode:
author Dan Willemsen <dwillemsen@google.com> 2016-03-22 14:08:17 -0700
committer Dan Willemsen <dwillemsen@google.com> 2016-03-22 19:53:36 -0700
commit6ac18ecb84332b6e84f498434812541f58a64ed4 (patch)
treeaf18bd7447c199f1c99fe0dc9b880aeb30dc8b3a /reverse_path.py
parent7253e0b8a486797347b8bdec43aa98ce135f3ad7 (diff)
Improve BUILDDIR handling with symlinks
If BUILDDIR is a local symlink to another directory in the same parent directory (out -> out.angler), then using out and .. as relative paths to get back and forth work. But if BUILDDIR is a symlink to another directory altogether (out -> /mnt/sdd/out.master), then we shouldn't be relying on relative paths (so that the source directory can still be moved). Change-Id: I946c8116090410ab2b935eafba9b6e96f5f2f1dd
Diffstat (limited to 'reverse_path.py')
-rwxr-xr-xreverse_path.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/reverse_path.py b/reverse_path.py
new file mode 100755
index 000000000..7b7d6217b
--- /dev/null
+++ b/reverse_path.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+
+import os
+import sys
+
+# Find the best reverse path to reference the current directory from another
+# directory. We use this to find relative paths to and from the source and build
+# directories.
+#
+# If the directory is given as an absolute path, return an absolute path to the
+# current directory.
+#
+# If there's a symlink involved, and the same relative path would not work if
+# the symlink was replace with a regular directory, then return an absolute
+# path. This handles paths like out -> /mnt/ssd/out
+#
+# For symlinks that can use the same relative path (out -> out.1), just return
+# the relative path. That way out.1 can be renamed as long as the symlink is
+# updated.
+#
+# For everything else, just return the relative path. That allows the source and
+# output directories to be moved as long as they stay in the same position
+# relative to each other.
+def reverse_path(path):
+ if path.startswith("/"):
+ return os.path.abspath('.')
+
+ realpath = os.path.relpath(os.path.realpath('.'), os.path.realpath(path))
+ relpath = os.path.relpath('.', path)
+
+ if realpath != relpath:
+ return os.path.abspath('.')
+
+ return relpath
+
+
+if __name__ == '__main__':
+ print(reverse_path(sys.argv[1]))