Make lafilefixer handle files in binary mode, in order to avoid potential
authorZac Medico <zmedico@gentoo.org>
Mon, 12 Jul 2010 10:24:03 +0000 (03:24 -0700)
committerZac Medico <zmedico@gentoo.org>
Mon, 12 Jul 2010 10:24:03 +0000 (03:24 -0700)
character encoding issues.

pym/portage/package/ebuild/doebuild.py
pym/portage/tests/lafilefixer/test_lafilefixer.py
pym/portage/util/lafilefixer.py

index d3abeeae095b8808788e11e6ad7d266f9f70953b..ab474f2416b563c9f5b03791e655a1f533274391 100644 (file)
@@ -1567,6 +1567,7 @@ def _post_src_install_uid_fix(mysettings, out=None):
                size = 0
                counted_inodes = set()
                lafilefixing_announced = False
+               lafilefixing = "lafilefixing" in mysettings.features
 
                for parent, dirs, files in os.walk(destdir):
                        try:
@@ -1606,11 +1607,11 @@ def _post_src_install_uid_fix(mysettings, out=None):
                                else:
                                        fpath = os.path.join(parent, fname)
 
-                               if "lafilefixing" in mysettings["FEATURES"] and \
+                               if lafilefixing and \
                                        fname.endswith(".la") and os.path.isfile(fpath):
-                                       f = codecs.open(_unicode_encode(os.path.realpath(fpath),
+                                       f = open(_unicode_encode(fpath,
                                                encoding=_encodings['fs'], errors='strict'),
-                                               mode='r', encoding=_encodings['content'], errors='replace')
+                                               mode='rb')
                                        contents = f.read()
                                        f.close()
                                        try:
@@ -1619,7 +1620,7 @@ def _post_src_install_uid_fix(mysettings, out=None):
                                                needs_update = False
                                                if not lafilefixing_announced:
                                                        lafilefixing_announced = True
-                                                       writemsg("Fixing .la files\n")
+                                                       writemsg("Fixing .la files\n", fd=out)
                                                msg = "   %s is not a valid libtool archive, skipping\n" % fpath[len(destdir):]
                                                qa_msg = "QA Notice: invalid .la file found: %s, %s" % (fpath[len(destdir):], e)
                                                writemsg(msg)
@@ -1627,11 +1628,11 @@ def _post_src_install_uid_fix(mysettings, out=None):
                                        if needs_update:
                                                if not lafilefixing_announced:
                                                        lafilefixing_announced = True
-                                                       writemsg("Fixing .la files\n")
-                                               writemsg("   %s\n" % fpath[len(destdir):])
-                                               f = codecs.open(_unicode_encode(fpath,
+                                                       writemsg("Fixing .la files\n", fd=out)
+                                               writemsg("   %s\n" % fpath[len(destdir):], fd=out)
+                                               f = open(_unicode_encode(fpath,
                                                        encoding=_encodings['fs'], errors='strict'),
-                                                       mode='w', encoding=_encodings['content'], errors='replace')
+                                                       mode='wb')
                                                f.write(new_contents)
                                                f.close()
 
index 5e4e840f34b9bfb8fc4ebed037bf009b92080af5..71ed31b64d448a307431ded32dc7e3fda20bdf18 100644 (file)
@@ -8,118 +8,118 @@ from portage.exception import InvalidData
 class test_lafilefixer(TestCase):
 
        def get_test_cases_clean(self):
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -lm'\n" + \
-                       "current=6\n" + \
-                       "age=0\n" + \
-                       "revision=2\n" + \
-                       "installed=yes\n" + \
-                       "dlopen=''\n" + \
-                       "dlpreopen=''\n" + \
-                       "libdir='/usr/lib64'\n"
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -lm'\n" + \
-                       "current=6\n" + \
-                       "age=0\n" + \
-                       "revision=2\n" + \
-                       "installed=yes\n" + \
-                       "dlopen=''\n" + \
-                       "dlpreopen=''\n" + \
-                       "libdir='/usr/lib64'\n"
-               yield "dependency_libs=' liba.la /usr/lib64/bar.la -lc'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -lm'\n" + \
+                       b"current=6\n" + \
+                       b"age=0\n" + \
+                       b"revision=2\n" + \
+                       b"installed=yes\n" + \
+                       b"dlopen=''\n" + \
+                       b"dlpreopen=''\n" + \
+                       b"libdir='/usr/lib64'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -lm'\n" + \
+                       b"current=6\n" + \
+                       b"age=0\n" + \
+                       b"revision=2\n" + \
+                       b"installed=yes\n" + \
+                       b"dlopen=''\n" + \
+                       b"dlpreopen=''\n" + \
+                       b"libdir='/usr/lib64'\n"
+               yield b"dependency_libs=' liba.la /usr/lib64/bar.la -lc'\n"
 
        def get_test_cases_update(self):
                #.la -> -l*
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc'\n", \
-                       "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -L/usr/lib64 -la -lb -lc'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc'\n", \
+                       b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -L/usr/lib64 -la -lb -lc'\n"
                #move stuff into inherited_linker_flags
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la -pthread /usr/lib64/libb.la -lc'\n" + \
-                       "inherited_linker_flags=''\n", \
-                       "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -L/usr/lib64 -la -lb -lc'\n" + \
-                       "inherited_linker_flags=' -pthread'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la -pthread /usr/lib64/libb.la -lc'\n" + \
+                       b"inherited_linker_flags=''\n", \
+                       b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -L/usr/lib64 -la -lb -lc'\n" + \
+                       b"inherited_linker_flags=' -pthread'\n"
                #reorder 
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la -R/usr/lib64 /usr/lib64/libb.la -lc'\n", \
-                       "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -R/usr/lib64 -L/usr/lib64 -la -lb -lc'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la -R/usr/lib64 /usr/lib64/libb.la -lc'\n", \
+                       b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -R/usr/lib64 -L/usr/lib64 -la -lb -lc'\n"
                #remove duplicates from dependency_libs (the original version didn't do it for inherited_linker_flags)
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libc.la -pthread -mt" + \
-                       " -L/usr/lib -R/usr/lib64 -lc /usr/lib64/libb.la -lc'\n" +\
-                       "inherited_linker_flags=' -pthread -pthread'\n", \
-                       "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' -R/usr/lib64 -L/usr/lib64 -L/usr/lib -la -lc -lb'\n" +\
-                       "inherited_linker_flags=' -pthread -pthread -mt'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libc.la -pthread -mt" + \
+                       b" -L/usr/lib -R/usr/lib64 -lc /usr/lib64/libb.la -lc'\n" +\
+                       b"inherited_linker_flags=' -pthread -pthread'\n", \
+                       b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' -R/usr/lib64 -L/usr/lib64 -L/usr/lib -la -lc -lb'\n" +\
+                       b"inherited_linker_flags=' -pthread -pthread -mt'\n"
                #-L rewriting
-               yield "dependency_libs=' -L/usr/X11R6/lib'\n", \
-                       "dependency_libs=' -L/usr/lib'\n"
-               yield "dependency_libs=' -L/usr/local/lib'\n", \
-                       "dependency_libs=' -L/usr/lib'\n"
-               yield "dependency_libs=' -L/usr/lib64/pkgconfig/../..'\n", \
-                       "dependency_libs=' -L/usr'\n"
-               yield "dependency_libs=' -L/usr/lib/pkgconfig/..'\n", \
-                       "dependency_libs=' -L/usr/lib'\n"
-               yield "dependency_libs=' -L/usr/lib/pkgconfig/../.. -L/usr/lib/pkgconfig/..'\n", \
-                       "dependency_libs=' -L/usr -L/usr/lib'\n"
+               yield b"dependency_libs=' -L/usr/X11R6/lib'\n", \
+                       b"dependency_libs=' -L/usr/lib'\n"
+               yield b"dependency_libs=' -L/usr/local/lib'\n", \
+                       b"dependency_libs=' -L/usr/lib'\n"
+               yield b"dependency_libs=' -L/usr/lib64/pkgconfig/../..'\n", \
+                       b"dependency_libs=' -L/usr'\n"
+               yield b"dependency_libs=' -L/usr/lib/pkgconfig/..'\n", \
+                       b"dependency_libs=' -L/usr/lib'\n"
+               yield b"dependency_libs=' -L/usr/lib/pkgconfig/../.. -L/usr/lib/pkgconfig/..'\n", \
+                       b"dependency_libs=' -L/usr -L/usr/lib'\n"
 
        def get_test_cases_broken(self):
-               yield ""
+               yield b""
                #no dependency_libs
-               yield "dlname='libfoo.so.1'\n" + \
-                       "current=6\n" + \
-                       "age=0\n" + \
-                       "revision=2\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"current=6\n" + \
+                       b"age=0\n" + \
+                       b"revision=2\n"
                #borken dependency_libs
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc' \n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc' \n"
                        #borken dependency_libs
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc\n"
                #crap in dependency_libs
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n"
                #dependency_libs twice
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n" +\
-                       "dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n" +\
+                       b"dependency_libs=' /usr/lib64/liba.la /usr/lib64/libb.la -lc /-lstdc++'\n"
                #inherited_linker_flags twice
-               yield "dlname='libfoo.so.1'\n" + \
-                       "library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
-                       "old_library='libpdf.a'\n" + \
-                       "inherited_linker_flags=''\n" +\
-                       "inherited_linker_flags=''\n"
+               yield b"dlname='libfoo.so.1'\n" + \
+                       b"library_names='libfoo.so.1.0.2 libfoo.so.1 libfoo.so'\n" + \
+                       b"old_library='libpdf.a'\n" + \
+                       b"inherited_linker_flags=''\n" +\
+                       b"inherited_linker_flags=''\n"
 
        def testlafilefixer(self):
                from portage.util.lafilefixer import _parse_lafile_contents, rewrite_lafile
index 90cc435417b166e5ffdf7bac8cfdccfa8072bac3..3c39a49f5637cf8f48fe6600447589d7ba8387d7 100644 (file)
@@ -1,9 +1,10 @@
 # Copyright 2010 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
+import os as _os
 import re
 
-from portage import os
+from portage import _unicode_decode
 from portage.exception import InvalidData
 
 #########################################################
@@ -32,18 +33,18 @@ from portage.exception import InvalidData
 #########################################################
 
 #These regexes are used to parse the interesting entries in the la file
-dep_libs_re = re.compile("dependency_libs='(?P<value>[^']*)'$")
-inh_link_flags_re = re.compile("inherited_linker_flags='(?P<value>[^']*)'$")
+dep_libs_re = re.compile(b"dependency_libs='(?P<value>[^']*)'$")
+inh_link_flags_re = re.compile(b"inherited_linker_flags='(?P<value>[^']*)'$")
 
 #regexes for replacing stuff in -L entries. 
 #replace 'X11R6/lib' and 'local/lib' with 'lib', no idea what's this about.
-X11_local_sub = re.compile("X11R6/lib|local/lib")
+X11_local_sub = re.compile(b"X11R6/lib|local/lib")
 #get rid of the '..'
-pkgconfig_sub1 = re.compile("usr/lib[^/]*/pkgconfig/\.\./\.\.")
-pkgconfig_sub2 = re.compile("(?P<usrlib>usr/lib[^/]*)/pkgconfig/\.\.")
+pkgconfig_sub1 = re.compile(b"usr/lib[^/]*/pkgconfig/\.\./\.\.")
+pkgconfig_sub2 = re.compile(b"(?P<usrlib>usr/lib[^/]*)/pkgconfig/\.\.")
 
 #detect flags that should go into inherited_linker_flags instead of dependency_libs
-flag_re = re.compile("-mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe|-threads")
+flag_re = re.compile(b"-mt|-mthreads|-kthread|-Kthread|-pthread|-pthreads|--thread-safe|-threads")
 
 def _parse_lafile_contents(contents):
        """
@@ -53,7 +54,7 @@ def _parse_lafile_contents(contents):
        dep_libs = None
        inh_link_flags = None
 
-       for line in contents.split("\n"):
+       for line in contents.split(b"\n"):
                m = dep_libs_re.match(line)
                if m:
                        if dep_libs is not None:
@@ -95,44 +96,44 @@ def rewrite_lafile(contents):
 
        #Check entries in 'dependency_libs'.
        for dep_libs_entry in dep_libs.split():
-               if dep_libs_entry.startswith("-l"):
+               if dep_libs_entry.startswith(b"-l"):
                        #-lfoo, keep it
                        if dep_libs_entry not in new_dep_libs:
                                new_dep_libs.append(dep_libs_entry)
 
-               elif dep_libs_entry.endswith(".la"):
+               elif dep_libs_entry.endswith(b".la"):
                        #Two cases:
                        #1) /usr/lib64/libfoo.la, turn it into -lfoo and append -L/usr/lib64 to libladir
                        #2) libfoo.la, keep it
-                       dir, file = os.path.split(dep_libs_entry)
+                       dir, file = _os.path.split(dep_libs_entry)
 
-                       if not dir or not file.startswith("lib"):
+                       if not dir or not file.startswith(b"lib"):
                                if dep_libs_entry not in new_dep_libs:
                                        new_dep_libs.append(dep_libs_entry)
                        else:
                                #/usr/lib64/libfoo.la -> -lfoo
-                               lib = "-l" + file[3:-3]
+                               lib = b"-l" + file[3:-3]
                                if lib not in new_dep_libs:
                                        new_dep_libs.append(lib)
                                #/usr/lib64/libfoo.la -> -L/usr/lib64
-                               ladir = "-L" + dir
+                               ladir = b"-L" + dir
                                if ladir not in libladir:
                                        libladir.append(ladir)
 
-               elif dep_libs_entry.startswith("-L"):
+               elif dep_libs_entry.startswith(b"-L"):
                        #Do some replacement magic and store them in 'libladir'.
                        #This allows us to place all -L entries at the beginning
                        #of 'dependency_libs'.
                        ladir = dep_libs_entry
                        
-                       ladir = X11_local_sub.sub("lib", ladir)
-                       ladir = pkgconfig_sub1.sub("usr", ladir)
-                       ladir = pkgconfig_sub2.sub("\g<usrlib>", ladir)
+                       ladir = X11_local_sub.sub(b"lib", ladir)
+                       ladir = pkgconfig_sub1.sub(b"usr", ladir)
+                       ladir = pkgconfig_sub2.sub(b"\g<usrlib>", ladir)
                        
                        if ladir not in libladir:
                                libladir.append(ladir)
 
-               elif dep_libs_entry.startswith("-R"):
+               elif dep_libs_entry.startswith(b"-R"):
                        if dep_libs_entry not in librpath:
                                librpath.append(dep_libs_entry)
 
@@ -148,17 +149,17 @@ def rewrite_lafile(contents):
 
                else:
                        raise InvalidData("Error: Unexpected entry '%s' in 'dependency_libs'" \
-                               % dep_libs_entry)
+                               % _unicode_decode(dep_libs_entry))
 
        #What should 'dependency_libs' and 'inherited_linker_flags' look like?
-       expected_dep_libs = ""
+       expected_dep_libs = b""
        for x in (librpath, libladir, new_dep_libs):
                if x:
-                       expected_dep_libs += " " + " ".join(x)
+                       expected_dep_libs += b" " + b" ".join(x)
 
-       expected_inh_link_flags = ""
+       expected_inh_link_flags = b""
        if new_inh_link_flags:
-               expected_inh_link_flags += " " + " ".join(new_inh_link_flags)
+               expected_inh_link_flags += b" " + b" ".join(new_inh_link_flags)
 
        #Don't touch the file if we don't need to, otherwise put the expected values into
        #'contents' and write it into the la file.
@@ -166,11 +167,11 @@ def rewrite_lafile(contents):
                (inh_link_flags is None or expected_inh_link_flags == inh_link_flags):
                return False, None
 
-       contents = re.sub("dependency_libs='" + dep_libs + "'", \
-               "dependency_libs='" + expected_dep_libs + "'" , contents)
+       contents = re.sub(b"dependency_libs='" + dep_libs + b"'", \
+               b"dependency_libs='" + expected_dep_libs + b"'" , contents)
 
        if inh_link_flags is not None:
-               contents = re.sub("inherited_linker_flags='" + inh_link_flags + "'", \
-                       "inherited_linker_flags='" + expected_inh_link_flags + "'" , contents)
+               contents = re.sub(b"inherited_linker_flags='" + inh_link_flags + b"'", \
+                       b"inherited_linker_flags='" + expected_inh_link_flags + b"'" , contents)
 
        return True, contents