Explorar o código

[core] relative path for pth symlink.

Abraham Elmahrek %!s(int64=12) %!d(string=hai) anos
pai
achega
f32f477d61
Modificáronse 2 ficheiros con 35 adicións e 4 borrados
  1. 26 1
      tools/app_reg/common.py
  2. 9 3
      tools/app_reg/pth.py

+ 26 - 1
tools/app_reg/common.py

@@ -17,7 +17,9 @@
 
 import glob
 import logging
-import os.path
+import os
+import sys
+from posixpath import curdir, sep, pardir, join
 
 # The root of the Hue installation
 INSTALL_ROOT = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
@@ -48,3 +50,26 @@ def _get_python_lib_dir():
 
 def _get_python_site_packages_dir():
   return os.path.join(_get_python_lib_dir(), 'site-packages')
+
+
+# Creates os.path.relpath for Python 2.4 and 2.5
+if not hasattr(os.path, 'relpath'):
+  # default to posixpath definition
+  # no windows support
+  def relpath(path, start=os.path.curdir):
+    """Return a relative version of a path"""
+
+    if not path:
+      raise ValueError("no path specified")
+
+    start_list = os.path.abspath(start).split(os.path.sep)
+    path_list = os.path.abspath(path).split(os.path.sep)
+
+    # Work out how much of the filepath is shared by start and path.
+    i = len(os.path.commonprefix([start_list, path_list]))
+
+    rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
+    if not rel_list:
+        return os.path.curdir
+    return os.path.join(*rel_list)
+  os.path.relpath = relpath

+ 9 - 3
tools/app_reg/pth.py

@@ -99,9 +99,15 @@ class PthFile(object):
     tmp_path = self._path + '.new'
     file(tmp_path, 'w').write('\n'.join(sorted(self._entries)))
     os.rename(tmp_path, self._path)
-    LOG.info('=== Saved %s' % (self._path,))
-    if not os.path.exists(self._symlink_path):
-      os.symlink(self._path, self._symlink_path)
+    LOG.info('=== Saved %s' % self._path)
+    rel_symlink_path = os.path.relpath(self._path, os.path.dirname(self._symlink_path))
+
+    # overwrite symlink if the path it points to is different from desired PTH.
+    # relpath defined in common.py for python 2.4 and 2.5
+    if not os.path.exists(self._symlink_path) or os.path.realpath(self._symlink_path) != rel_symlink_path:
+      os.remove(self._symlink_path)
+      os.symlink(rel_symlink_path, self._symlink_path)
+      LOG.debug('=== Create symbolic link at %s to %s' % (self._symlink_path, rel_symlink_path))
 
   def sync(self, apps):
     """Sync the .pth file with the installed apps"""