Commits

illume committed a586e0a

Adding python3 compat.py module from pygame. cmd_bdist_mpkg.py and pkg.py converted to be py3k compat.

Comments (0)

Files changed (3)

bdist_mpkg/cmd_bdist_mpkg.py

 
 from bdist_mpkg import pkg, tools, plists
 from bdist_mpkg.util import copy_tree
+from bdist_mpkg.compat import _unicode
+
+
 
 INSTALL_SCHEME_DESCRIPTIONS = dict(
-    purelib = u'(Required) Pure Python modules and packages',
-    platlib = u'(Required) Python modules, extensions, and packages',
-    headers = u'(Optional) Header files for development',
-    scripts = u'(Optional) Scripts to use from the Unix shell',
-    data    = u'(Optional) Additional data files (sometimes documentation)',
+    purelib = _unicode('(Required) Pure Python modules and packages'),
+    platlib = _unicode('(Required) Python modules, extensions, and packages'),
+    headers = _unicode('(Optional) Header files for development'),
+    scripts = _unicode('(Optional) Scripts to use from the Unix shell'),
+    data    = _unicode('(Optional) Additional data files (sometimes documentation)'),
 )
 
 class bdist_mpkg (Command):
         rval = self.command_schemes
         if rval is None:
             rval = {}
-            for scheme, cmd in self.scheme_command.iteritems():
+            for scheme, cmd in list(self.scheme_command.items()):
                 rval.setdefault(cmd, []).append(scheme)
             self.command_schemes = rval
         return rval
 
         if self.custom_schemes is None:
             self.custom_schemes = {}
-        for scheme, desc in self.custom_schemes.iteritems():
+        for scheme, desc in list(self.custom_schemes.items()):
             if 'description' in desc:
                 self.scheme_descriptions[scheme] = desc['description']
             if 'prefix' in desc:
                 self.scheme_copy[scheme] = desc['source']
 
         install = self.get_finalized_command('install')
-        for scheme, description in INSTALL_SCHEME_DESCRIPTIONS.iteritems():
+        for scheme, description in list(INSTALL_SCHEME_DESCRIPTIONS.items()):
             self.scheme_command.setdefault(scheme, 'install')
             self.scheme_descriptions.setdefault(scheme, description)
             self.scheme_map.setdefault(scheme,
         pass
 
     def run_subprojects(self):
-        for scheme,setupfile in self.scheme_subprojects.iteritems():
+        for scheme,setupfile in list(self.scheme_subprojects.items()):
             self.run_subproject(scheme, setupfile)
 
     def run_subproject(self, scheme, setupfile):
         return os.path.join(self.dist_dir, self.metapackagename)
 
     def get_schemes(self):
-        return self.scheme_map.keys()
+        return list(self.scheme_map.keys())
 
     def get_scheme_prefix(self, scheme):
         return self.scheme_map.get(scheme)
             return None
         files, common, prefix = self.get_scheme_root(scheme)
         if prefix is not None:
-            description += u'\nInstalled to: ' + tools.unicode_path(prefix)
+            description += _unicode('\nInstalled to: ' + tools.unicode_path(prefix))
         return description
 
     def get_metapackage(self):
                 self.run_command(command)
 
     def run_copy(self):
-        for scheme, source in self.scheme_copy.iteritems():
+        for scheme, source in list(self.scheme_copy.items()):
             log.info("copying files for scheme %s" % (scheme,))
             target = self.get_scheme_install_target(scheme)
             self.copy_tree(source, target)

bdist_mpkg/compat.py

+"""Python 2.x/3.x compatibility tools"""
+
+import sys
+
+__all__ = ['geterror', 'long_', 'xrange_', 'ord_', 'unichr_',
+           'unicode_', 'raw_input_', 'as_bytes', 'as_unicode']
+
+def geterror ():
+    return sys.exc_info()[1]
+
+try:
+    long_ = long
+except NameError:
+    long_ = int
+
+try:
+    xrange_ = xrange
+except NameError:
+    xrange_ = range
+
+def get_BytesIO():
+    try:
+        from cStringIO import StringIO as BytesIO
+    except ImportError:
+        from io import BytesIO
+    return BytesIO
+
+def get_StringIO():
+    try:
+        from cStringIO import StringIO
+    except ImportError:
+        from io import StringIO
+    return StringIO
+
+def ord_(o):
+    try:
+        return ord(o)
+    except TypeError:
+        return o
+
+try:
+    unichr_ = unichr
+except NameError:
+    unichr_ = chr
+
+try:
+    unicode_ = unicode
+except NameError:
+    unicode_ = str
+
+try:
+    bytes_ = bytes
+except NameError:
+    bytes_ = str
+
+try:
+    raw_input_ = raw_input
+except NameError:
+    raw_input_ = input
+
+if sys.platform == 'win32':
+    filesystem_errors = "replace"
+elif sys.version_info >= (3, 0, 0):
+    filesystem_errors = "surrogateescape"
+else:
+    filesystem_errors = "strict"
+    
+def filesystem_encode(u):
+    return u.encode(sys.getfilesystemencoding(), filesystem_errors)
+
+# Represent escaped bytes and strings in a portable way.
+#
+# as_bytes: Allow a Python 3.x string to represent a bytes object.
+#   e.g.: as_bytes("a\x01\b") == b"a\x01b" # Python 3.x
+#         as_bytes("a\x01\b") == "a\x01b"  # Python 2.x
+# as_unicode: Allow a Python "r" string to represent a unicode string.
+#   e.g.: as_unicode(r"Bo\u00F6tes") == u"Bo\u00F6tes" # Python 2.x
+#         as_unicode(r"Bo\u00F6tes") == "Bo\u00F6tes"  # Python 3.x
+try:
+    eval("u'a'")
+    def as_bytes(string):
+        """ '<binary literal>' => '<binary literal>' """
+        return string
+        
+    def as_unicode(rstring):
+        """ r'<Unicode literal>' => u'<Unicode literal>' """
+        return rstring.decode('unicode_escape', 'strict')
+except SyntaxError:
+    def as_bytes(string):
+        """ '<binary literal>' => b'<binary literal>' """
+        return string.encode('latin-1', 'strict')
+        
+    def as_unicode(rstring):
+        """ r'<Unicode literal>' => '<Unicode literal>' """
+        return rstring.encode('ascii', 'strict').decode('unicode_escape',
+                                                        'stict')
+
+

bdist_mpkg/pkg.py

 import os
 import sys
-from cStringIO import StringIO
 from distutils.dir_util import mkpath
 from distutils.file_util import copy_file
 
 from bdist_mpkg.util import copy_tree
 from bdist_mpkg.templates import InstallationCheck
 
-def write_template((script, strings), dest, mkpath=mkpath):
+from bdist_mpkg.compat import get_StringIO
+from bdist_mpkg.compat import _unicode
+StringIO = get_StringIO()
+
+
+def write_template(script_strings, dest, mkpath=mkpath):
+    (script, strings) = script_strings
+    
     spath = os.path.join(dest, 'InstallationCheck')
     f = open(spath, 'w')
     f.write(script.encode('utf8'))
     f.close()
-    os.chmod(spath, os.stat(spath)[0] | 0111)
+    #os.chmod(spath, os.stat(spath)[0] | 0111)
+    os.chmod(spath, os.stat(spath)[0] | 73) # 73 == 0111
     lproj = os.path.join(dest, 'English.lproj')
     mkpath(lproj)
     spath = os.path.join(lproj, 'InstallationCheck.strings')
     if description is None:
         description = dist.get_description()
     if not description:
-        description = u'%s %s' % (name, version)
+        description = _unicode('%s %s' % (name, version))
 
     mkpath(os.path.join(pkgdir, 'Contents', 'Resources'))
     if not dry_run:
         write_sizes(count, size, compressed, pkgdir)
 
     if admin:
-        auth = u'AdminAuthorization'
+        auth = _unicode('AdminAuthorization')
     else:
-        auth = u'RootAuthorization'
+        auth = _unicode('RootAuthorization')
 
     ninfo = plists.pkg_info(name, version)
     ninfo.update(dict(