import re
import shutil
import time
+ import types
try:
import cPickle as pickle
except ImportError:
# END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END OF IMPORTS -- END
# ===========================================================================
+def _unicode_encode(s):
+ if isinstance(s, unicode):
+ s = s.encode('utf_8', 'replace')
+ return s
+
+def _unicode_decode(s):
+ if not isinstance(s, unicode) and isinstance(s, basestring):
+ s = unicode(s, encoding='utf_8', errors='replace')
+ return s
+
+class _unicode_func_wrapper(object):
+ """
+ Wraps a function, converts arguments from unicode to bytes,
+ and return values to unicode from bytes.
+ """
+ __slots__ = ('_func',)
+
+ def __init__(self, func):
+ self._func = func
+
+ def __call__(self, *args, **kwargs):
+
+ wrapped_args = [_unicode_encode(x) for x in args]
+ if kwargs:
+ wrapped_kwargs = dict((_unicode_encode(k), _unicode_encode(v)) \
+ for k, v in kwargs.iteritems())
+ else:
+ wrapped_kwargs = {}
+
+ rval = self._func(*wrapped_args, **wrapped_kwargs)
+
+ if isinstance(rval, (basestring, list, tuple)):
+ if isinstance(rval, basestring):
+ rval = _unicode_decode(rval)
+ elif isinstance(rval, list):
+ rval = [_unicode_decode(x) for x in rval]
+ elif isinstance(rval, tuple):
+ rval = tuple(_unicode_decode(x) for x in rval)
+
+ return rval
+
+class _unicode_module_wrapper(object):
+ """
+ Wraps a module and wraps all functions with _unicode_func_wrapper.
+ """
+ __slots__ = ('_mod',)
+
+ def __init__(self, mod):
+ object.__setattr__(self, '_mod', mod)
+
+ def __getattribute__(self, attr):
+ result = getattr(object.__getattribute__(self, '_mod'), attr)
+ if isinstance(result, type):
+ pass
+ elif type(result) is types.ModuleType:
+ result = _unicode_module_wrapper(result)
+ elif hasattr(result, '__call__'):
+ result = _unicode_func_wrapper(result)
+ return result
+
+if sys.hexversion >= 0x3000000:
+ def _unicode_module_wrapper(mod):
+ return mod
+
def _shell_quote(s):
"""
Quote a string in double-quotes and use backslashes to
from portage import listdir, dep_expand, digraph, flatten, key_expand, \
doebuild_environment, doebuild, env_update, prepare_build_dirs, \
- abssymlink, movefile, _movefile, bsd_chflags, cpv_getkey
+ abssymlink, movefile, _movefile, bsd_chflags, cpv_getkey, \
+ _unicode_module_wrapper
from portage.cache.mappings import slot_dict_class
import sys
from itertools import izip
+os = _unicode_module_wrapper(os)
+
try:
import cPickle as pickle
except ImportError: