From cc6e80cab018c21d702a13f333dfc9aa6e0beac9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20O=C5=BCarowski?= <piotr@debian.org>
Date: Sun, 12 Jun 2011 22:20:23 +0200
Subject: [PATCH] deal with original symlinks more carefully

closes: 627969, thanks to Leonid Borisenko for the original patch
---
 dh_python2 | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/dh_python2 b/dh_python2
index 4608186..41d6e69 100755
--- a/dh_python2
+++ b/dh_python2
@@ -28,7 +28,7 @@ import re
 import sys
 from filecmp import dircmp, cmpfiles, cmp as fcmp
 from optparse import OptionParser, SUPPRESS_HELP
-from os.path import isdir, islink, exists, join, realpath
+from os.path import isabs, isdir, islink, exists, join, normpath, realpath
 from shutil import rmtree, copy as fcopy
 from stat import ST_MODE, S_IXUSR, S_IXGRP, S_IXOTH
 sys.path.insert(1, '/usr/share/python/')
@@ -169,7 +169,7 @@ def move_to_pyshared(dir1):
 
     for i in os.listdir(dir1):
         fpath1 = join(dir1, i)
-        if isdir(fpath1):
+        if isdir(fpath1) and not islink(fpath1):
             if any(fn for fn in os.listdir(fpath1) if fext(fn) != 'so'):
                 # at least one file that is not an extension
                 move_to_pyshared(join(dir1, i))
@@ -180,7 +180,16 @@ def move_to_pyshared(dir1):
             if not exists(fpath2):
                 if not exists(dstdir):
                     os.makedirs(dstdir)
-                os.rename(fpath1, fpath2)
+                if islink(fpath1):
+                    fpath1_target = os.readlink(fpath1)
+                    if isabs(fpath1_target):
+                        os.symlink(fpath1_target, fpath2)
+                    else:
+                        fpath1_target = normpath(join(dir1, fpath1_target))
+                        relative_symlink(fpath1_target, fpath2)
+                    os.remove(fpath1)
+                else:
+                    os.rename(fpath1, fpath2)
                 relative_symlink(fpath2, fpath1)
 
 
@@ -233,7 +242,7 @@ def share_2x(dir1, dir2, dc=None):
     # dir1 starts with debian/packagename/usr/lib/pythonX.Y/*-packages/
     dstdir = join(debian, package, 'usr/share/pyshared/', \
                   '/'.join(dir1.split('/')[6:]))
-    if not exists(dstdir):
+    if not exists(dstdir) and not islink(dir1):
         os.makedirs(dstdir)
     if dc is None:  # guess/copy mode
         if not exists(dir2):
@@ -241,11 +250,16 @@ def share_2x(dir1, dir2, dc=None):
         common_dirs = []
         common_files = []
         for i in os.listdir(dir1):
-            if isdir(join(dir1, i)):
+            subdir1 = join(dir1, i)
+            if isdir(subdir1) and not islink(subdir1):
                 common_dirs.append([i, None])
             else:
                 # directories with .so files will be blocked earlier
                 common_files.append(i)
+    elif islink(dir1):
+        # skip this symlink in pyshared (dpkg has problems with symlinks anyway)
+        common_dirs = []
+        common_files = []
     else:
         common_dirs = dc.subdirs.iteritems()
         common_files = dc.common_files
-- 
GitLab