Migrate from codecs.open() to io.open().
authorZac Medico <zmedico@gentoo.org>
Sun, 10 Jul 2011 23:26:24 +0000 (16:26 -0700)
committerZac Medico <zmedico@gentoo.org>
Sun, 10 Jul 2011 23:55:05 +0000 (16:55 -0700)
The io.open() function is the same as the built-in open() function in
python3, and its implementation is optimized in python-2.7 and later.
In addition to the possible performance improvement, this also allows
us to avoid any future compatibility issues with codecs.open() that
may arise if it is delegated to the built-in open() function as
discussed in PEP 400.

The main caveat involved with io.open() is that TextIOWrapper.write()
raises TypeError if given raw bytes, unlike the streams returned from
codecs.open(). This is mainly an issue for python2 since literal
strings are raw bytes. We handle this by wrapping TextIOWrapper.write()
arguments with our _unicode_decode() function. Also, the
atomic_ofstream class overrides the write() method in python2 so that
it performs automatic coercion to unicode when necessary.

34 files changed:
bin/binhost-snapshot
bin/egencache
bin/repoman
pym/_emerge/Binpkg.py
pym/_emerge/EbuildFetcher.py
pym/_emerge/EbuildMetadataPhase.py
pym/_emerge/depgraph.py
pym/_emerge/emergelog.py
pym/_emerge/resolver/output_helpers.py
pym/portage/cache/flat_hash.py
pym/portage/cache/flat_list.py
pym/portage/cvstree.py
pym/portage/dbapi/bintree.py
pym/portage/dbapi/porttree.py
pym/portage/dbapi/vartree.py
pym/portage/elog/messages.py
pym/portage/elog/mod_save.py
pym/portage/elog/mod_save_summary.py
pym/portage/env/loaders.py
pym/portage/glsa.py
pym/portage/manifest.py
pym/portage/news.py
pym/portage/output.py
pym/portage/package/ebuild/_config/LocationsManager.py
pym/portage/package/ebuild/deprecated_profile_check.py
pym/portage/package/ebuild/doebuild.py
pym/portage/package/ebuild/fetch.py
pym/portage/repository/config.py
pym/portage/tests/ebuild/test_spawn.py
pym/portage/update.py
pym/portage/util/ExtractKernelVersion.py
pym/portage/util/__init__.py
pym/portage/util/env_update.py
pym/repoman/utilities.py

index 825a1167221f5b7326db9ee0c600487715700359..9d2697d032b828fb485a145451a68e25e7d9fe33 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/python
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
+import io
 import optparse
 import os
 import sys
@@ -109,7 +109,7 @@ def main(argv):
        if not (os.WIFEXITED(ret) and os.WEXITSTATUS(ret) == os.EX_OK):
                return 1
 
-       infile = codecs.open(portage._unicode_encode(src_pkgs_index,
+       infile = io.open(portage._unicode_encode(src_pkgs_index,
                encoding=portage._encodings['fs'], errors='strict'),
                mode='r', encoding=portage._encodings['repo.content'],
                errors='strict')
index 5307cd5a202d5156feff7959bfd823dab27290e6..1b4265df146c20501d4ff67717e496fba0d44b31 100755 (executable)
@@ -20,7 +20,7 @@ try:
 except KeyboardInterrupt:
        sys.exit(128 + signal.SIGINT)
 
-import codecs
+import io
 import logging
 import optparse
 import subprocess
@@ -391,10 +391,10 @@ class GenUseLocalDesc(object):
                                        output = open(_unicode_encode(desc_path,
                                                encoding=_encodings['fs'], errors='strict'), 'r+b')
                                else:
-                                       output = codecs.open(_unicode_encode(desc_path,
+                                       output = io.open(_unicode_encode(desc_path,
                                                encoding=_encodings['fs'], errors='strict'),
                                                mode='w', encoding=_encodings['repo.content'],
-                                               errors='replace')
+                                               errors='backslashreplace')
                        except IOError as e:
                                if not self._preserve_comments or \
                                        os.path.isfile(desc_path):
@@ -413,10 +413,10 @@ class GenUseLocalDesc(object):
                                        level=logging.WARNING, noiselevel=-1)
                                self._preserve_comments = False
                                try:
-                                       output = codecs.open(_unicode_encode(desc_path,
+                                       output = io.open(_unicode_encode(desc_path,
                                                encoding=_encodings['fs'], errors='strict'),
                                                mode='w', encoding=_encodings['repo.content'],
-                                               errors='replace')
+                                               errors='backslashreplace')
                                except IOError as e:
                                        writemsg_level(
                                                "ERROR: failed to open output file %s: %s\n" \
@@ -437,18 +437,18 @@ class GenUseLocalDesc(object):
 
                        # Finished probing comments in binary mode, now append
                        # in text mode.
-                       output = codecs.open(_unicode_encode(desc_path,
+                       output = io.open(_unicode_encode(desc_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='a', encoding=_encodings['repo.content'],
-                               errors='replace')
-                       output.write('\n')
+                               errors='backslashreplace')
+                       output.write(_unicode_decode('\n'))
                else:
-                       output.write('''
+                       output.write(_unicode_decode('''
 # This file is deprecated as per GLEP 56 in favor of metadata.xml. Please add
 # your descriptions to your package's metadata.xml ONLY.
 # * generated automatically using egencache *
 
-'''.lstrip())
+'''.lstrip()))
 
                # The cmp function no longer exists in python3, so we'll
                # implement our own here under a slightly different name
@@ -522,7 +522,8 @@ class GenUseLocalDesc(object):
                                                                resatoms = sorted(reskeys, key=cmp_sort_key(atomcmp))
                                                                resdesc = resdict[reskeys[resatoms[-1]]]
 
-                                               output.write('%s:%s - %s\n' % (cp, flag, resdesc))
+                                               output.write(_unicode_decode(
+                                                       '%s:%s - %s\n' % (cp, flag, resdesc)))
 
                output.close()
 
@@ -609,9 +610,9 @@ class GenChangeLogs(object):
 
        def generate_changelog(self, cp):
                try:
-                       output = codecs.open('ChangeLog',
+                       output = io.open('ChangeLog',
                                mode='w', encoding=_encodings['repo.content'],
-                               errors='replace')
+                               errors='backslashreplace')
                except IOError as e:
                        writemsg_level(
                                "ERROR: failed to open ChangeLog for %s: %s\n" % (cp,e,),
@@ -619,7 +620,7 @@ class GenChangeLogs(object):
                        self.returncode |= 2
                        return
 
-               output.write(('''
+               output.write(_unicode_decode('''
 # ChangeLog for %s
 # Copyright 1999-%s Gentoo Foundation; Distributed under the GPL v2
 # $Header: $
@@ -688,10 +689,11 @@ class GenChangeLogs(object):
                        # Reverse the sort order for headers.
                        for c in reversed(changed):
                                if c.startswith('+') and c.endswith('.ebuild'):
-                                       output.write('*%s (%s)\n' % (c[1:-7], date))
+                                       output.write(_unicode_decode(
+                                               '*%s (%s)\n' % (c[1:-7], date)))
                                        wroteheader = True
                        if wroteheader:
-                               output.write('\n')
+                               output.write(_unicode_decode('\n'))
 
                        # strip '<cp>: ', '[<cp>] ', and similar
                        body[0] = re.sub(r'^\W*' + re.escape(cp) + r'\W+', '', body[0])
@@ -711,10 +713,13 @@ class GenChangeLogs(object):
 
                        # don't break filenames on hyphens
                        self._wrapper.break_on_hyphens = False
-                       output.write(self._wrapper.fill('%s; %s %s:' % (date, author, ', '.join(changed))))
+                       output.write(_unicode_decode(
+                               self._wrapper.fill(
+                               '%s; %s %s:' % (date, author, ', '.join(changed)))))
                        # but feel free to break commit messages there
                        self._wrapper.break_on_hyphens = True
-                       output.write('\n%s\n\n' % '\n'.join([self._wrapper.fill(x) for x in body]))
+                       output.write(_unicode_decode(
+                               '\n%s\n\n' % '\n'.join(self._wrapper.fill(x) for x in body)))
 
                output.close()
 
index d1d393a82399d2ccdb6e9dcdd2cea5f3f4d27923..3e02036816653deb638c7b28a8770a8299064e02 100755 (executable)
@@ -9,10 +9,10 @@
 from __future__ import print_function
 
 import calendar
-import codecs
 import copy
 import errno
 import formatter
+import io
 import logging
 import optparse
 import re
@@ -700,7 +700,7 @@ for path in portdb.porttrees:
 
        desc_path = os.path.join(path, 'profiles', 'profiles.desc')
        try:
-               desc_file = codecs.open(_unicode_encode(desc_path,
+               desc_file = io.open(_unicode_encode(desc_path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace')
        except EnvironmentError:
@@ -1209,7 +1209,7 @@ for x in scanlist:
                        continue
                try:
                        line = 1
-                       for l in codecs.open(_unicode_encode(os.path.join(checkdir, y),
+                       for l in io.open(_unicode_encode(os.path.join(checkdir, y),
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content']):
                                line +=1
@@ -1822,7 +1822,7 @@ for x in scanlist:
                                pkg.mtime = None
                try:
                        # All ebuilds should have utf_8 encoding.
-                       f = codecs.open(_unicode_encode(full_path,
+                       f = io.open(_unicode_encode(full_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'])
                        try:
@@ -2319,7 +2319,7 @@ else:
        commitmessage = options.commitmsg
        if options.commitmsgfile:
                try:
-                       f = codecs.open(_unicode_encode(options.commitmsgfile,
+                       f = io.open(_unicode_encode(options.commitmsgfile,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='replace')
                        commitmessage = f.read()
index 84eda21ba8dc8bf6e5856da9f2eb9fdf5822ab35..b83341941ed74d709affd544e4563006daf837f2 100644 (file)
@@ -14,8 +14,9 @@ from portage.util import writemsg
 import portage
 from portage import os
 from portage import _encodings
+from portage import _unicode_decode
 from portage import _unicode_encode
-import codecs
+import io
 import logging
 from portage.output import colorize
 
@@ -239,20 +240,22 @@ class Binpkg(CompositeTask):
                        else:
                                continue
 
-                       f = codecs.open(_unicode_encode(os.path.join(infloc, k),
+                       f = io.open(_unicode_encode(os.path.join(infloc, k),
                                encoding=_encodings['fs'], errors='strict'),
-                               mode='w', encoding=_encodings['content'], errors='replace')
+                               mode='w', encoding=_encodings['content'],
+                               errors='backslashreplace')
                        try:
-                               f.write(v + "\n")
+                               f.write(_unicode_decode(v + "\n"))
                        finally:
                                f.close()
 
                # Store the md5sum in the vdb.
-               f = codecs.open(_unicode_encode(os.path.join(infloc, 'BINPKGMD5'),
+               f = io.open(_unicode_encode(os.path.join(infloc, 'BINPKGMD5'),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='w', encoding=_encodings['content'], errors='strict')
                try:
-                       f.write(str(portage.checksum.perform_md5(pkg_path)) + "\n")
+                       f.write(_unicode_decode(
+                               str(portage.checksum.perform_md5(pkg_path)) + "\n"))
                finally:
                        f.close()
 
index 215024165be2d452c7ef606588727236ad8422b0..51d2f5a104c6b84c4a32428d7c364719cf85cc3a 100644 (file)
@@ -1,10 +1,11 @@
-# Copyright 1999-2010 Gentoo Foundation
+# Copyright 1999-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import traceback
 
 from _emerge.SpawnProcess import SpawnProcess
 import copy
+import io
 import signal
 import sys
 import portage
@@ -12,7 +13,6 @@ from portage import os
 from portage import _encodings
 from portage import _unicode_encode
 from portage import _unicode_decode
-import codecs
 from portage.elog.messages import eerror
 from portage.package.ebuild.fetch import fetch
 from portage.util._pty import _create_pty_or_pipe
@@ -160,12 +160,13 @@ class EbuildFetcher(SpawnProcess):
                # fetch code will be skipped, so we need to generate equivalent
                # output here.
                if self.logfile is not None:
-                       f = codecs.open(_unicode_encode(self.logfile,
+                       f = io.open(_unicode_encode(self.logfile,
                                encoding=_encodings['fs'], errors='strict'),
-                               mode='a', encoding=_encodings['content'], errors='replace')
+                               mode='a', encoding=_encodings['content'],
+                               errors='backslashreplace')
                        for filename in uri_map:
-                               f.write((' * %s size ;-) ...' % \
-                                       filename).ljust(73) + '[ ok ]\n')
+                               f.write(_unicode_decode((' * %s size ;-) ...' % \
+                                       filename).ljust(73) + '[ ok ]\n'))
                        f.close()
 
                return True
index 284622d69a2017e39862e3620d69b66c7672cc3d..e53298bae0e1b05fa7dd13ddeba3cd84a6631a36 100644 (file)
@@ -11,7 +11,7 @@ from portage import _encodings
 from portage import _unicode_decode
 from portage import _unicode_encode
 import fcntl
-import codecs
+import io
 
 class EbuildMetadataPhase(SubProcess):
 
@@ -37,7 +37,7 @@ class EbuildMetadataPhase(SubProcess):
                if eapi is None and \
                        'parse-eapi-ebuild-head' in settings.features:
                        eapi = portage._parse_eapi_ebuild_head(
-                               codecs.open(_unicode_encode(ebuild_path,
+                               io.open(_unicode_encode(ebuild_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
                                errors='replace'))
index d5971d1ba8beb1799ad37a7eb7897f9e384495d7..d0b8fb72292ba84c1c6be05fea259aafde7a89cb 100644 (file)
@@ -3,10 +3,9 @@
 
 from __future__ import print_function
 
-import codecs
 import difflib
 import errno
-import gc
+import io
 import logging
 import re
 import stat
@@ -5937,7 +5936,7 @@ class depgraph(object):
                def write_changes(root, changes, file_to_write_to):
                        file_contents = None
                        try:
-                               file_contents = codecs.open(
+                               file_contents = io.open(
                                        _unicode_encode(file_to_write_to,
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='r', encoding=_encodings['content'],
index 9cac3b222aa951647b06ab837072871b4190151b..a195c6f8457b8394499edab8c7d05253ec012856 100644 (file)
@@ -3,12 +3,13 @@
 
 from __future__ import print_function
 
-import codecs
+import io
 import sys
 import time
 import portage
 from portage import os
 from portage import _encodings
+from portage import _unicode_decode
 from portage import _unicode_encode
 from portage.data import secpass
 from portage.output import xtermTitle
@@ -36,7 +37,7 @@ def emergelog(xterm_titles, mystr, short_msg=None):
        try:
                file_path = os.path.join(_emerge_log_dir, 'emerge.log')
                existing_log = os.path.isfile(file_path)
-               mylogfile = codecs.open(_unicode_encode(file_path,
+               mylogfile = io.open(_unicode_encode(file_path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='a', encoding=_encodings['content'],
                        errors='backslashreplace')
@@ -50,7 +51,8 @@ def emergelog(xterm_titles, mystr, short_msg=None):
                        # seek because we may have gotten held up by the lock.
                        # if so, we may not be positioned at the end of the file.
                        mylogfile.seek(0, 2)
-                       mylogfile.write(str(time.time())[:10]+": "+mystr+"\n")
+                       mylogfile.write(_unicode_decode(
+                               str(time.time())[:10]+": "+mystr+"\n"))
                        mylogfile.flush()
                finally:
                        if mylock:
index 874660f69dffa76d1b8be39eda7e86db82ca655b..b7e73766cc1f69133613273ea976beb815e34670 100644 (file)
@@ -7,7 +7,7 @@ in output.py
 __all__ = (
        )
 
-import codecs
+import io
 import re
 import sys
 
@@ -502,7 +502,7 @@ def _calc_changelog(ebuildpath,current,next):
                next = next[:-3]
        changelogpath = os.path.join(os.path.split(ebuildpath)[0],'ChangeLog')
        try:
-               changelog = codecs.open(_unicode_encode(changelogpath,
+               changelog = io.open(_unicode_encode(changelogpath,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace'
                ).read()
index 6be0fe4b203372b64f9eb2194373af7e60a8aeb5..b6bc0744ec9381ab512d6edc0de7a5a805902b69 100644 (file)
@@ -1,21 +1,26 @@
-# Copyright: 2005 Gentoo Foundation
+# Copyright: 2005-2011 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
 # Author(s): Brian Harring (ferringb@gentoo.org)
-# License: GPL2
 
-import codecs
 from portage.cache import fs_template
 from portage.cache import cache_errors
 import errno
+import io
 import stat
 import sys
 import os as _os
 from portage import os
 from portage import _encodings
+from portage import _unicode_decode
 from portage import _unicode_encode
 
 if sys.hexversion >= 0x3000000:
        long = int
 
+# Coerce to unicode, in order to prevent TypeError when writing
+# raw bytes to TextIOWrapper with python2.
+_setitem_fmt = _unicode_decode("%s=%s\n")
+
 class database(fs_template.FsBased):
 
        autocommits = True
@@ -35,7 +40,7 @@ class database(fs_template.FsBased):
                # Don't use os.path.join, for better performance.
                fp = self.location + _os.sep + cpv
                try:
-                       myf = codecs.open(_unicode_encode(fp,
+                       myf = io.open(_unicode_encode(fp,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
                                errors='replace')
@@ -68,7 +73,7 @@ class database(fs_template.FsBased):
                s = cpv.rfind("/")
                fp = os.path.join(self.location,cpv[:s],".update.%i.%s" % (os.getpid(), cpv[s+1:]))
                try:
-                       myf = codecs.open(_unicode_encode(fp,
+                       myf = io.open(_unicode_encode(fp,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='w', encoding=_encodings['repo.content'],
                                errors='backslashreplace')
@@ -76,7 +81,7 @@ class database(fs_template.FsBased):
                        if errno.ENOENT == e.errno:
                                try:
                                        self._ensure_dirs(cpv)
-                                       myf = codecs.open(_unicode_encode(fp,
+                                       myf = io.open(_unicode_encode(fp,
                                                encoding=_encodings['fs'], errors='strict'),
                                                mode='w', encoding=_encodings['repo.content'],
                                                errors='backslashreplace')
@@ -90,7 +95,7 @@ class database(fs_template.FsBased):
                                v = values.get(k)
                                if not v:
                                        continue
-                               myf.write("%s=%s\n" % (k, v))
+                               myf.write(_setitem_fmt % (k, v))
                finally:
                        myf.close()
                self._ensure_access(fp)
index eb755839873b37d85b7664ac40dbb2292aa4b1d4..7288307536f9e3776317ccd636d804de0a710e5c 100644 (file)
@@ -1,16 +1,24 @@
+# Copyright 2005-2011 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
 from portage.cache import fs_template
 from portage.cache import cache_errors
 from portage import os
 from portage import _encodings
+from portage import _unicode_decode
 from portage import _unicode_encode
-import codecs
 import errno
+import io
 import stat
 import sys
 
 if sys.hexversion >= 0x3000000:
        long = int
 
+# Coerce to unicode, in order to prevent TypeError when writing
+# raw bytes to TextIOWrapper with python2.
+_setitem_fmt = _unicode_decode("%s\n")
+
 # store the current key order *here*.
 class database(fs_template.FsBased):
 
@@ -36,7 +44,7 @@ class database(fs_template.FsBased):
        def _getitem(self, cpv):
                d = {}
                try:
-                       myf = codecs.open(_unicode_encode(os.path.join(self.location, cpv),
+                       myf = io.open(_unicode_encode(os.path.join(self.location, cpv),
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
                                errors='replace')
@@ -60,7 +68,7 @@ class database(fs_template.FsBased):
                s = cpv.rfind("/")
                fp=os.path.join(self.location,cpv[:s],".update.%i.%s" % (os.getpid(), cpv[s+1:]))
                try:
-                       myf = codecs.open(_unicode_encode(fp,
+                       myf = io.open(_unicode_encode(fp,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='w', encoding=_encodings['repo.content'],
                                errors='backslashreplace')
@@ -68,7 +76,7 @@ class database(fs_template.FsBased):
                        if errno.ENOENT == e.errno:
                                try:
                                        self._ensure_dirs(cpv)
-                                       myf = codecs.open(_unicode_encode(fp,
+                                       myf = io.open(_unicode_encode(fp,
                                                encoding=_encodings['fs'], errors='strict'),
                                                mode='w', encoding=_encodings['repo.content'],
                                                errors='backslashreplace')
@@ -79,7 +87,7 @@ class database(fs_template.FsBased):
                
 
                for x in self.auxdbkey_order:
-                       myf.write(values.get(x,"")+"\n")
+                       myf.write(_setitem_fmt % (values.get(x, ""),))
 
                myf.close()
                self._ensure_access(fp, mtime=values["_mtime_"])
index de580f57e55e25f36704d5a1c69f9ada77bb22b1..9ba22f315bb757f1569dd9828dbcc71fb28ad5af 100644 (file)
@@ -4,7 +4,7 @@
 
 from __future__ import print_function
 
-import codecs
+import io
 import re
 import stat
 import sys
@@ -53,7 +53,7 @@ def isadded(entries, path):
        filename=os.path.basename(path)
 
        try:
-               myfile = codecs.open(
+               myfile = io.open(
                        _unicode_encode(os.path.join(basedir, 'CVS', 'Entries'),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='strict')
@@ -207,7 +207,7 @@ def getentries(mydir,recursive=0):
        if not os.path.exists(mydir):
                return entries
        try:
-               myfile = codecs.open(_unicode_encode(myfn,
+               myfile = io.open(_unicode_encode(myfn,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='strict')
                mylines=myfile.readlines()
index ebec11fdd4359e27684fd063d517d39f868945d5..62fc6235410d4448e42829cf043082bd4b1656b9 100644 (file)
@@ -34,6 +34,7 @@ from portage import _unicode_encode
 
 import codecs
 import errno
+import io
 import re
 import stat
 import subprocess
@@ -765,7 +766,7 @@ class binarytree(object):
                                host, parsed_url.path.lstrip("/"), "Packages")
                        pkgindex = self._new_pkgindex()
                        try:
-                               f = codecs.open(_unicode_encode(pkgindex_file,
+                               f = io.open(_unicode_encode(pkgindex_file,
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='r', encoding=_encodings['repo.content'],
                                        errors='replace')
@@ -1288,7 +1289,7 @@ class binarytree(object):
        def _load_pkgindex(self):
                pkgindex = self._new_pkgindex()
                try:
-                       f = codecs.open(_unicode_encode(self._pkgindex_file,
+                       f = io.open(_unicode_encode(self._pkgindex_file,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
                                errors='replace')
index 33c6a3b1d93e7fe89eb86402cf6f26743d836526..ecf275cd455c358d82a0e521660710962c15d06a 100644 (file)
@@ -37,8 +37,7 @@ from _emerge.EbuildMetadataPhase import EbuildMetadataPhase
 from _emerge.PollScheduler import PollScheduler
 
 import os as _os
-import codecs
-import logging
+import io
 import stat
 import sys
 import traceback
@@ -480,7 +479,7 @@ class portdbapi(dbapi):
 
                        if eapi is None and \
                                'parse-eapi-ebuild-head' in self.doebuild_settings.features:
-                               eapi = portage._parse_eapi_ebuild_head(codecs.open(
+                               eapi = portage._parse_eapi_ebuild_head(io.open(
                                        _unicode_encode(myebuild,
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='r', encoding=_encodings['repo.content'],
index 5a86291f8e848a28689093f5337d8c319b789c49..d5c055420df83afe12c757942a9425a8cbbfa74c 100644 (file)
@@ -37,7 +37,6 @@ from portage.const import CACHE_PATH, CONFIG_MEMORY_FILE, \
        PORTAGE_PACKAGE_ATOM, PRIVATE_PATH, VDB_PATH
 from portage.const import _ENABLE_DYN_LINK_MAP, _ENABLE_PRESERVE_LIBS
 from portage.dbapi import dbapi
-from portage.dep import _slot_separator
 from portage.exception import CommandNotFound, \
        InvalidData, InvalidLocation, InvalidPackageName, \
        FileNotFound, PermissionDenied, UnsupportedAPIException
@@ -54,18 +53,19 @@ from portage import _selinux_merge
 from portage import _unicode_decode
 from portage import _unicode_encode
 
-from _emerge.AsynchronousLock import AsynchronousLock
 from _emerge.EbuildBuildDir import EbuildBuildDir
 from _emerge.EbuildPhase import EbuildPhase
 from _emerge.emergelog import emergelog
 from _emerge.PollScheduler import PollScheduler
 from _emerge.MiscFunctionsProcess import MiscFunctionsProcess
 
-import codecs
+import errno
 import gc
-import re, shutil, stat, errno, subprocess
+import io
 import logging
 import os as _os
+import re
+import shutil
 import stat
 import sys
 import tempfile
@@ -692,7 +692,7 @@ class vardbapi(dbapi):
                                results.append(st[stat.ST_MTIME])
                                continue
                        try:
-                               myf = codecs.open(
+                               myf = io.open(
                                        _unicode_encode(os.path.join(mydir, x),
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='r', encoding=_encodings['repo.content'],
@@ -760,7 +760,7 @@ class vardbapi(dbapi):
                new_vdb = False
                counter = -1
                try:
-                       cfile = codecs.open(
+                       cfile = io.open(
                                _unicode_encode(self._counter_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
@@ -1417,7 +1417,7 @@ class dblink(object):
                        return self.contentscache
                pkgfiles = {}
                try:
-                       myc = codecs.open(_unicode_encode(contents_file,
+                       myc = io.open(_unicode_encode(contents_file,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
                                errors='replace')
@@ -3027,7 +3027,7 @@ class dblink(object):
                                continue
 
                        try:
-                               val = codecs.open(_unicode_encode(
+                               val = io.open(_unicode_encode(
                                        os.path.join(inforoot, var_name),
                                        encoding=_encodings['fs'], errors='strict'),
                                        mode='r', encoding=_encodings['repo.content'],
@@ -3383,10 +3383,10 @@ class dblink(object):
                # write local package counter for recording
                if counter is None:
                        counter = self.vartree.dbapi.counter_tick(mycpv=self.mycpv)
-               codecs.open(_unicode_encode(os.path.join(self.dbtmpdir, 'COUNTER'),
+               io.open(_unicode_encode(os.path.join(self.dbtmpdir, 'COUNTER'),
                        encoding=_encodings['fs'], errors='strict'),
                        'w', encoding=_encodings['repo.content'], errors='backslashreplace'
-                       ).write(str(counter))
+                       ).write(_unicode_decode(str(counter)))
 
                self.updateprotect()
 
@@ -3671,7 +3671,10 @@ class dblink(object):
                cfgfiledict_orig = cfgfiledict.copy()
 
                # open CONTENTS file (possibly overwriting old one) for recording
-               outfile = codecs.open(_unicode_encode(
+               # Use atomic_ofstream for automatic coercion of raw bytes to
+               # unicode, in order to prevent TypeError when writing raw bytes
+               # to TextIOWrapper with python2.
+               outfile = atomic_ofstream(_unicode_encode(
                        os.path.join(self.dbtmpdir, 'CONTENTS'),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='w', encoding=_encodings['repo.content'],
@@ -4119,7 +4122,7 @@ class dblink(object):
                "returns contents of a file with whitespace converted to spaces"
                if not os.path.exists(self.dbdir+"/"+name):
                        return ""
-               mydata = codecs.open(
+               mydata = io.open(
                        _unicode_encode(os.path.join(self.dbdir, name),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace'
@@ -4132,7 +4135,7 @@ class dblink(object):
        def getfile(self,fname):
                if not os.path.exists(self.dbdir+"/"+fname):
                        return ""
-               return codecs.open(_unicode_encode(os.path.join(self.dbdir, fname),
+               return io.open(_unicode_encode(os.path.join(self.dbdir, fname),
                        encoding=_encodings['fs'], errors='strict'), 
                        mode='r', encoding=_encodings['repo.content'], errors='replace'
                        ).read()
@@ -4149,7 +4152,7 @@ class dblink(object):
        def getelements(self,ename):
                if not os.path.exists(self.dbdir+"/"+ename):
                        return []
-               mylines = codecs.open(_unicode_encode(
+               mylines = io.open(_unicode_encode(
                        os.path.join(self.dbdir, ename),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace'
@@ -4161,13 +4164,13 @@ class dblink(object):
                return myreturn
 
        def setelements(self,mylist,ename):
-               myelement = codecs.open(_unicode_encode(
+               myelement = io.open(_unicode_encode(
                        os.path.join(self.dbdir, ename),
                        encoding=_encodings['fs'], errors='strict'),
                        mode='w', encoding=_encodings['repo.content'],
                        errors='backslashreplace')
                for x in mylist:
-                       myelement.write(x+"\n")
+                       myelement.write(_unicode_decode(x+"\n"))
                myelement.close()
 
        def isregular(self):
index a8a3e9a5f657b84b9f2f1e45b48fffb23d3a2cb7..6c1580a3785eb9b16f047ea30a5c917e6b5b1132 100644 (file)
@@ -1,5 +1,5 @@
 # elog/messages.py - elog core functions
-# Copyright 2006-2009 Gentoo Foundation
+# Copyright 2006-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 import portage
@@ -15,7 +15,7 @@ from portage import _encodings
 from portage import _unicode_encode
 from portage import _unicode_decode
 
-import codecs
+import io
 import sys
 
 def collect_ebuild_messages(path):
@@ -43,7 +43,7 @@ def collect_ebuild_messages(path):
                        logentries[msgfunction] = []
                lastmsgtype = None
                msgcontent = []
-               for l in codecs.open(_unicode_encode(filename,
+               for l in io.open(_unicode_encode(filename,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace'):
                        if not l:
@@ -167,7 +167,6 @@ def _make_msgfunction(level, color):
                _elog_base(level, msg,  phase=phase, key=key, color=color, out=out)
        return _elog
 
-import sys
 for f in _functions:
        setattr(sys.modules[__name__], f, _make_msgfunction(_functions[f][0], _functions[f][1]))
 del f, _functions
index ac21fb8c5f47050ec5d77bccedc6a6ea8acda98d..0f097946677ce886b35bb33a8782c57aef463066 100644 (file)
@@ -1,8 +1,8 @@
 # elog/mod_save.py - elog dispatch module
-# Copyright 2006-2007 Gentoo Foundation
+# Copyright 2006-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
+import io
 import time
 from portage import os
 from portage import _encodings
@@ -34,10 +34,10 @@ def process(mysettings, key, logentries, fulltext):
        ensure_dirs(os.path.dirname(elogfilename),
                uid=portage_uid, gid=portage_gid, mode=0o2770)
 
-       elogfile = codecs.open(_unicode_encode(elogfilename,
+       elogfile = io.open(_unicode_encode(elogfilename,
                encoding=_encodings['fs'], errors='strict'),
                mode='w', encoding=_encodings['content'], errors='backslashreplace')
-       elogfile.write(fulltext)
+       elogfile.write(_unicode_decode(fulltext))
        elogfile.close()
 
        return elogfilename
index ea8233fda93737c50f3f24ba5f6fd5cd5ac36437..8970f06d04610157df76523a96e8b0c21b0a2a35 100644 (file)
@@ -1,8 +1,8 @@
 # elog/mod_save_summary.py - elog dispatch module
-# Copyright 2006-2007 Gentoo Foundation
+# Copyright 2006-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
+import io
 import time
 from portage import os
 from portage import _encodings
@@ -21,7 +21,7 @@ def process(mysettings, key, logentries, fulltext):
 
        # TODO: Locking
        elogfilename = elogdir+"/summary.log"
-       elogfile = codecs.open(_unicode_encode(elogfilename,
+       elogfile = io.open(_unicode_encode(elogfilename,
                encoding=_encodings['fs'], errors='strict'),
                mode='a', encoding=_encodings['content'], errors='backslashreplace')
        apply_permissions(elogfilename, mode=0o60, mask=0)
@@ -30,10 +30,12 @@ def process(mysettings, key, logentries, fulltext):
        # Avoid potential UnicodeDecodeError later.
        time_str = _unicode_decode(time_str,
                encoding=_encodings['content'], errors='replace')
-       elogfile.write(_(">>> Messages generated by process %(pid)d on %(time)s for package %(pkg)s:\n\n") %
-                       {"pid": os.getpid(), "time": time_str, "pkg": key})
-       elogfile.write(fulltext)
-       elogfile.write("\n")
+       elogfile.write(_unicode_decode(
+               _(">>> Messages generated by process " +
+               "%(pid)d on %(time)s for package %(pkg)s:\n\n") %
+               {"pid": os.getpid(), "time": time_str, "pkg": key}))
+       elogfile.write(_unicode_decode(fulltext))
+       elogfile.write(_unicode_decode("\n"))
        elogfile.close()
 
        return elogfilename
index 81dd5b172c3c92e149044d26c5fc590723ee0a69..b540fbbd3a54d3bcb70e4ab55d2d0c3fddd3632a 100644 (file)
@@ -1,9 +1,9 @@
 # config.py -- Portage Config
-# Copyright 2007 Gentoo Foundation
+# Copyright 2007-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
 import errno
+import io
 import stat
 from portage import os
 from portage import _encodings
@@ -149,7 +149,7 @@ class FileLoader(DataLoader):
                func = self.lineParser
                for fn in RecursiveFileLoader(self.fname):
                        try:
-                               f = codecs.open(_unicode_encode(fn,
+                               f = io.open(_unicode_encode(fn,
                                        encoding=_encodings['fs'], errors='strict'), mode='r',
                                        encoding=_encodings['content'], errors='replace')
                        except EnvironmentError as e:
index 7e7f4976e0ba1f4a5a7c563177cd2dd0574ba2cf..a784d14e1082a47e71184dd304f753165f570768 100644 (file)
@@ -1,9 +1,9 @@
-# Copyright 2003-2010 Gentoo Foundation
+# Copyright 2003-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 from __future__ import absolute_import
 
-import codecs
+import io
 import sys
 try:
        from urllib.request import urlopen as urllib_request_urlopen
@@ -668,12 +668,12 @@ class Glsa:
                @returns:       None
                """
                if not self.isApplied():
-                       checkfile = codecs.open(
+                       checkfile = io.open(
                                _unicode_encode(os.path.join(self.config["EROOT"],
                                CACHE_PATH, "glsa"),
                                encoding=_encodings['fs'], errors='strict'), 
                                mode='a+', encoding=_encodings['content'], errors='strict')
-                       checkfile.write(self.nr+"\n")
+                       checkfile.write(_unicode_decode(self.nr + "\n"))
                        checkfile.close()
                return None
        
index 4714da03200455272c0f1e6f4d413e65ae49df2e..13efab791c3d7d24b00a6a7b13eb7659e3455d48 100644 (file)
@@ -1,8 +1,8 @@
 # Copyright 1999-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
 import errno
+import io
 
 import portage
 portage.proxy.lazyimport.lazyimport(globals(),
@@ -141,7 +141,7 @@ class Manifest(object):
                """Parse a manifest.  If myhashdict is given then data will be added too it.
                   Otherwise, a new dict will be created and returned."""
                try:
-                       fd = codecs.open(_unicode_encode(file_path,
+                       fd = io.open(_unicode_encode(file_path,
                                encoding=_encodings['fs'], errors='strict'), mode='r',
                                encoding=_encodings['repo.content'], errors='replace')
                        if myhashdict is None:
@@ -229,7 +229,7 @@ class Manifest(object):
                        update_manifest = True
                        if not force:
                                try:
-                                       f = codecs.open(_unicode_encode(self.getFullname(),
+                                       f = io.open(_unicode_encode(self.getFullname(),
                                                encoding=_encodings['fs'], errors='strict'),
                                                mode='r', encoding=_encodings['repo.content'],
                                                errors='replace')
@@ -519,7 +519,7 @@ class Manifest(object):
                mfname = self.getFullname()
                if not os.path.exists(mfname):
                        return rVal
-               myfile = codecs.open(_unicode_encode(mfname,
+               myfile = io.open(_unicode_encode(mfname,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'], errors='replace')
                lines = myfile.readlines()
index f500aa3b8cc9275482ad4201e060f7b237189bbb..866e5b0252a8af53fd277801797fd2f3864d3f5e 100644 (file)
@@ -1,12 +1,12 @@
 # portage: news management code
-# Copyright 2006-2010 Gentoo Foundation
+# Copyright 2006-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = ["NewsManager", "NewsItem", "DisplayRestriction",
        "DisplayProfileRestriction", "DisplayKeywordRestriction",
        "DisplayInstalledRestriction"]
 
-import codecs
+import io
 import logging
 import os as _os
 import re
@@ -250,7 +250,7 @@ class NewsItem(object):
                return self._valid
 
        def parse(self):
-               lines = codecs.open(_unicode_encode(self.path,
+               lines = io.open(_unicode_encode(self.path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='replace'
                        ).readlines()
index a0846b3921bad2d738bbe813c137bd3211169780..1bceb0e900dba02833332056de25e65492316346 100644 (file)
@@ -3,12 +3,12 @@
 
 __docformat__ = "epytext"
 
-import codecs
 try:
        from subprocess import getstatusoutput as subprocess_getstatusoutput
 except ImportError:
        from commands import getstatusoutput as subprocess_getstatusoutput
 import errno
+import io
 import formatter
 import re
 import sys
@@ -168,7 +168,7 @@ def _parse_color_map(config_root='/', onerror=None):
                return token
        try:
                lineno=0
-               for line in codecs.open(_unicode_encode(myfile,
+               for line in io.open(_unicode_encode(myfile,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='replace'):
                        lineno += 1
index 14cfaa67208fc1580903f8e21010211f53f6a065..c2b115bb0ec1b5f195056e2d04e7d803253b8733 100644 (file)
@@ -1,11 +1,11 @@
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = (
        'LocationsManager',
 )
 
-import codecs
+import io
 from portage import os, eapi_is_supported, _encodings, _unicode_encode
 from portage.const import CUSTOM_PROFILE_PATH, GLOBAL_CONFIG_PATH, \
        PROFILE_PATH, USER_CONFIG_PATH
@@ -90,7 +90,7 @@ class LocationsManager(object):
                parentsFile = os.path.join(currentPath, "parent")
                eapi_file = os.path.join(currentPath, "eapi")
                try:
-                       eapi = codecs.open(_unicode_encode(eapi_file,
+                       eapi = io.open(_unicode_encode(eapi_file,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'], errors='replace'
                                ).readline().strip()
index 7da5fe848cc9077116215c1f51f6115e58e35b5f..3fab4da6e4fdf7b8309ab3742971e982560c70c2 100644 (file)
@@ -1,9 +1,9 @@
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = ['deprecated_profile_check']
 
-import codecs
+import io
 
 from portage import os, _encodings, _unicode_encode
 from portage.const import DEPRECATED_PROFILE_FILE
@@ -19,7 +19,7 @@ def deprecated_profile_check(settings=None):
                DEPRECATED_PROFILE_FILE)
        if not os.access(deprecated_profile_file, os.R_OK):
                return False
-       dcontent = codecs.open(_unicode_encode(deprecated_profile_file,
+       dcontent = io.open(_unicode_encode(deprecated_profile_file,
                encoding=_encodings['fs'], errors='strict'), 
                mode='r', encoding=_encodings['content'], errors='replace').readlines()
        writemsg(colorize("BAD", _("\n!!! Your current profile is "
index 28ae459b7c7df43229bb1e32e6729ab5176e8368..53a3f9acf21a05bf599a12355de978cf9c11ab29 100644 (file)
@@ -3,9 +3,9 @@
 
 __all__ = ['doebuild', 'doebuild_environment', 'spawn', 'spawnebuild']
 
-import codecs
 import gzip
 import errno
+import io
 from itertools import chain
 import logging
 import os as _os
@@ -30,7 +30,7 @@ portage.proxy.lazyimport.lazyimport(globals(),
        'portage.util.ExtractKernelVersion:ExtractKernelVersion'
 )
 
-from portage import auxdbkeys, bsd_chflags, dep_check, \
+from portage import auxdbkeys, bsd_chflags, \
        eapi_is_supported, merge, os, selinux, \
        unmerge, _encodings, _parse_eapi_ebuild_head, _os_merge, \
        _shell_quote, _unicode_decode, _unicode_encode
@@ -39,7 +39,6 @@ from portage.const import EBUILD_SH_ENV_FILE, EBUILD_SH_ENV_DIR, \
 from portage.data import portage_gid, portage_uid, secpass, \
        uid, userpriv_groups
 from portage.dbapi.porttree import _parse_uri_map
-from portage.dbapi.virtual import fakedbapi
 from portage.dep import Atom, check_required_use, \
        human_readable_required_use, paren_enclose, use_reduce
 from portage.eapi import eapi_exports_KV, eapi_exports_merge_type, \
@@ -290,7 +289,7 @@ def doebuild_environment(myebuild, mydo, myroot=None, settings=None,
        if mydo == 'depend' and 'EAPI' not in mysettings.configdict['pkg']:
                if eapi is None and 'parse-eapi-ebuild-head' in mysettings.features:
                        eapi = _parse_eapi_ebuild_head(
-                               codecs.open(_unicode_encode(ebuild_path,
+                               io.open(_unicode_encode(ebuild_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'], errors='replace'))
 
@@ -1586,15 +1585,15 @@ def _post_src_install_uid_fix(mysettings, out):
        build_info_dir = os.path.join(mysettings['PORTAGE_BUILDDIR'],
                'build-info')
 
-       codecs.open(_unicode_encode(os.path.join(build_info_dir,
+       io.open(_unicode_encode(os.path.join(build_info_dir,
                'SIZE'), encoding=_encodings['fs'], errors='strict'),
                'w', encoding=_encodings['repo.content'],
-               errors='strict').write(str(size) + '\n')
+               errors='strict').write(_unicode_decode(str(size) + '\n'))
 
-       codecs.open(_unicode_encode(os.path.join(build_info_dir,
+       io.open(_unicode_encode(os.path.join(build_info_dir,
                'BUILD_TIME'), encoding=_encodings['fs'], errors='strict'),
                'w', encoding=_encodings['repo.content'],
-               errors='strict').write(str(int(time.time())) + '\n')
+               errors='strict').write(_unicode_decode(str(int(time.time())) + '\n'))
 
        use = frozenset(mysettings['PORTAGE_USE'].split())
        for k in _vdb_use_conditional_keys:
@@ -1620,10 +1619,10 @@ def _post_src_install_uid_fix(mysettings, out):
                        except OSError:
                                pass
                        continue
-               codecs.open(_unicode_encode(os.path.join(build_info_dir,
+               io.open(_unicode_encode(os.path.join(build_info_dir,
                        k), encoding=_encodings['fs'], errors='strict'),
                        mode='w', encoding=_encodings['repo.content'],
-                       errors='strict').write(v + '\n')
+                       errors='strict').write(_unicode_decode(v + '\n'))
 
        _reapply_bsdflags_to_image(mysettings)
 
@@ -1649,7 +1648,7 @@ def _post_src_install_soname_symlinks(mysettings, out):
                "build-info", "NEEDED.ELF.2")
 
        try:
-               lines = codecs.open(_unicode_encode(needed_filename,
+               lines = io.open(_unicode_encode(needed_filename,
                        encoding=_encodings['fs'], errors='strict'),
                        'r', encoding=_encodings['repo.content'],
                        errors='replace').readlines()
index 7bc95eb4f1d38d69536b95f9c3a3050e403edc6b..658b3eb2b2cee9c7d9ba7bcaf561ab57383db536 100644 (file)
@@ -5,8 +5,8 @@ from __future__ import print_function
 
 __all__ = ['fetch']
 
-import codecs
 import errno
+import io
 import logging
 import random
 import re
@@ -31,7 +31,7 @@ from portage.const import BASH_BINARY, CUSTOM_MIRRORS_FILE, \
        GLOBAL_CONFIG_PATH
 from portage.data import portage_gid, portage_uid, secpass, userpriv_groups
 from portage.exception import FileNotFound, OperationNotPermitted, \
-       PermissionDenied, PortageException, TryAgain
+       PortageException, TryAgain
 from portage.localization import _
 from portage.locks import lockfile, unlockfile
 from portage.manifest import Manifest
@@ -1008,7 +1008,7 @@ def fetch(myuris, mysettings, listonly=0, fetchonly=0,
                                                                # Fetch failed... Try the next one... Kill 404 files though.
                                                                if (mystat[stat.ST_SIZE]<100000) and (len(myfile)>4) and not ((myfile[-5:]==".html") or (myfile[-4:]==".htm")):
                                                                        html404=re.compile("<title>.*(not found|404).*</title>",re.I|re.M)
-                                                                       if html404.search(codecs.open(
+                                                                       if html404.search(io.open(
                                                                                _unicode_encode(myfile_path,
                                                                                encoding=_encodings['fs'], errors='strict'),
                                                                                mode='r', encoding=_encodings['content'], errors='replace'
index 4461901a470c76da366e6561e2b22765bfee010d..5538343dcc42337053425c5db3eb9b2ea06130fa 100644 (file)
@@ -1,7 +1,7 @@
 # Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
+import io
 import logging
 import re
 
@@ -10,7 +10,7 @@ try:
 except ImportError:
        from ConfigParser import SafeConfigParser, ParsingError
 from portage import os
-from portage.const import USER_CONFIG_PATH, GLOBAL_CONFIG_PATH, REPO_NAME_LOC
+from portage.const import USER_CONFIG_PATH, REPO_NAME_LOC
 from portage.env.loaders import KeyValuePairFileLoader
 from portage.util import normalize_path, writemsg, writemsg_level, shlex_split
 from portage.localization import _
@@ -133,7 +133,7 @@ class RepoConfig(object):
                """
                repo_name_path = os.path.join(repo_path, REPO_NAME_LOC)
                try:
-                       return codecs.open(
+                       return io.open(
                                _unicode_encode(repo_name_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'],
index 89a6c280778b8c9b7e7b8f840fc22ccde1bbf791..fea4738d415c2170d1ca8840877373f9d4ae9da5 100644 (file)
@@ -1,8 +1,8 @@
-# Copyright 1998-2007 Gentoo Foundation
+# Copyright 1998-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
 import errno
+import io
 import sys
 import tempfile
 from portage import os
@@ -32,7 +32,7 @@ class SpawnTestCase(TestCase):
                        proc.start()
                        os.close(null_fd)
                        self.assertEqual(proc.wait(), os.EX_OK)
-                       f = codecs.open(_unicode_encode(logfile,
+                       f = io.open(_unicode_encode(logfile,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'], errors='strict')
                        log_content = f.read()
index 4e5e78e5beda975a4f0f9f41e4fdef6c2b046664..52ab50645c5c7d88e5d9e749ec62548cc412024f 100644 (file)
@@ -1,8 +1,8 @@
 # Copyright 1999-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-import codecs
 import errno
+import io
 import re
 import stat
 import sys
@@ -13,9 +13,9 @@ from portage import _unicode_decode
 from portage import _unicode_encode
 import portage
 portage.proxy.lazyimport.lazyimport(globals(),
-       'portage.dep:Atom,dep_getkey,get_operator,isjustname,isvalidatom,' + \
+       'portage.dep:Atom,dep_getkey,isvalidatom,' + \
        'remove_slot',
-       'portage.util:ConfigProtect,grabfile,new_protect_filename,' + \
+       'portage.util:ConfigProtect,new_protect_filename,' + \
                'normalize_path,write_atomic,writemsg',
        'portage.util.listdir:_ignorecvs_dirs',
        'portage.versions:ververify'
@@ -86,7 +86,7 @@ def fixdbentries(update_iter, dbdir):
        mydata = {}
        for myfile in [f for f in os.listdir(dbdir) if f not in ignored_dbentries]:
                file_path = os.path.join(dbdir, myfile)
-               mydata[myfile] = codecs.open(_unicode_encode(file_path,
+               mydata[myfile] = io.open(_unicode_encode(file_path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['repo.content'],
                        errors='replace').read()
@@ -132,7 +132,7 @@ def grab_updates(updpath, prev_mtimes=None):
                if update_data or \
                        file_path not in prev_mtimes or \
                        long(prev_mtimes[file_path]) != mystat[stat.ST_MTIME]:
-                       content = codecs.open(_unicode_encode(file_path,
+                       content = io.open(_unicode_encode(file_path,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['repo.content'], errors='replace'
                                ).read()
@@ -253,7 +253,7 @@ def update_config_files(config_root, protect, protect_mask, update_iter, match_c
        myxfiles = recursivefiles
        for x in myxfiles:
                try:
-                       file_contents[x] = codecs.open(
+                       file_contents[x] = io.open(
                                _unicode_encode(os.path.join(abs_user_config, x),
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'],
index 1cdb8bf139c69229d0f74f722d161c628a274986..5cb9747e68eae6d144633352411d16651660c773 100644 (file)
@@ -1,9 +1,9 @@
-# Copyright 2010 Gentoo Foundation
+# Copyright 2010-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 __all__ = ['ExtractKernelVersion']
 
-import codecs
+import io
 
 from portage import os, _encodings, _unicode_encode
 from portage.util import getconfig, grabfile
@@ -22,7 +22,7 @@ def ExtractKernelVersion(base_dir):
        lines = []
        pathname = os.path.join(base_dir, 'Makefile')
        try:
-               f = codecs.open(_unicode_encode(pathname,
+               f = io.open(_unicode_encode(pathname,
                        encoding=_encodings['fs'], errors='strict'), mode='r',
                        encoding=_encodings['content'], errors='replace')
        except OSError as details:
index 5468e28c9f2457b838c536647d20c1ec2159088e..b641d3ed2a2e6597cba37e51b5a25b386812f256 100644 (file)
@@ -11,9 +11,9 @@ __all__ = ['apply_permissions', 'apply_recursive_permissions',
        'stack_dicts', 'stack_lists', 'unique_array', 'unique_everseen', 'varexpand',
        'write_atomic', 'writedict', 'writemsg', 'writemsg_level', 'writemsg_stdout']
 
-import codecs
 from copy import deepcopy
 import errno
+import io
 try:
        from itertools import filterfalse
 except ImportError:
@@ -475,7 +475,7 @@ def grablines(myfilename, recursive=0, remember_source_file=False):
                                        os.path.join(myfilename, f), recursive, remember_source_file))
        else:
                try:
-                       myfile = codecs.open(_unicode_encode(myfilename,
+                       myfile = io.open(_unicode_encode(myfilename,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'], errors='replace')
                        if remember_source_file:
@@ -1091,7 +1091,7 @@ class atomic_ofstream(ObjectProxy):
                if 'b' in mode:
                        open_func = open
                else:
-                       open_func = codecs.open
+                       open_func = io.open
                        kargs.setdefault('encoding', _encodings['content'])
                        kargs.setdefault('errors', 'backslashreplace')
 
@@ -1122,10 +1122,29 @@ class atomic_ofstream(ObjectProxy):
        def _get_target(self):
                return object.__getattribute__(self, '_file')
 
-       def __getattribute__(self, attr):
-               if attr in ('close', 'abort', '__del__'):
-                       return object.__getattribute__(self, attr)
-               return getattr(object.__getattribute__(self, '_file'), attr)
+       if sys.hexversion >= 0x3000000:
+
+               def __getattribute__(self, attr):
+                       if attr in ('close', 'abort', '__del__'):
+                               return object.__getattribute__(self, attr)
+                       return getattr(object.__getattribute__(self, '_file'), attr)
+
+       else:
+
+               # For TextIOWrapper, automatically coerce write calls to
+               # unicode, in order to avoid TypeError when writing raw
+               # bytes with python2.
+
+               def __getattribute__(self, attr):
+                       if attr in ('close', 'abort', 'write', '__del__'):
+                               return object.__getattribute__(self, attr)
+                       return getattr(object.__getattribute__(self, '_file'), attr)
+
+               def write(self, s):
+                       f = object.__getattribute__(self, '_file')
+                       if isinstance(f, io.TextIOWrapper):
+                               s = _unicode_decode(s)
+                       return f.write(s)
 
        def close(self):
                """Closes the temporary file, copies permissions (if possible),
index 3e295f00bb0214c0c5d95150254cbeb778baa82c..eb8a0d95139d1f79d163451709dc09f9bd4f8c08 100644 (file)
@@ -3,8 +3,8 @@
 
 __all__ = ['env_update']
 
-import codecs
 import errno
+import io
 import stat
 import sys
 import time
@@ -125,7 +125,7 @@ def env_update(makelinks=1, target_root=None, prev_mtimes=None, contents=None,
 
        ldsoconf_path = os.path.join(target_root, "etc", "ld.so.conf")
        try:
-               myld = codecs.open(_unicode_encode(ldsoconf_path,
+               myld = io.open(_unicode_encode(ldsoconf_path,
                        encoding=_encodings['fs'], errors='strict'),
                        mode='r', encoding=_encodings['content'], errors='replace')
                myldlines=myld.readlines()
index 9d4898e8a99d1c4c7334b1ef7de495da0a9269f6..232739ea012de623acf4d6bfe7cdfd174b144f14 100644 (file)
@@ -1,5 +1,5 @@
 # repoman: Utilities
-# Copyright 2007-2010 Gentoo Foundation
+# Copyright 2007-2011 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
 """This module contains utility functions to help repoman find ebuilds to
@@ -20,8 +20,8 @@ __all__ = [
        "check_metadata"
 ]
 
-import codecs
 import errno
+import io
 import logging
 import sys
 from portage import os
@@ -326,7 +326,7 @@ def get_commit_message_with_editor(editor, message=None):
                if not (os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == os.EX_OK):
                        return None
                try:
-                       mylines = codecs.open(_unicode_encode(filename,
+                       mylines = io.open(_unicode_encode(filename,
                                encoding=_encodings['fs'], errors='strict'),
                                mode='r', encoding=_encodings['content'], errors='replace'
                                ).readlines()