1# -*- coding:utf-8 -*-
2# Copyright 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Local version of the standard six module.
17
18Since this module is often unavailable by default on distros (or only available
19for specific versions), manage our own little copy.
20"""
21
22from __future__ import print_function
23
24import os
25import sys
26
27_path = os.path.realpath(__file__ + '/../..')
28if sys.path[0] != _path:
29    sys.path.insert(0, _path)
30del _path
31
32
33# Our attempts to wrap things below for diff versions of python confuse pylint.
34# pylint: disable=import-error,no-name-in-module,unused-import
35
36
37try:
38    import configparser
39except ImportError:
40    import ConfigParser as configparser
41
42
43# We allow mock to be disabled as it's not needed by non-unittest code.
44try:
45    import unittest.mock as mock
46except ImportError:
47    try:
48        import mock
49    except ImportError:
50        pass
51
52
53if sys.version_info.major < 3:
54    # pylint: disable=basestring-builtin,undefined-variable
55    string_types = basestring
56else:
57    string_types = str
58
59
60def setenv(var, val):
61    """Set |var| in the environment to |val|.
62
63    Python 2 wants ASCII strings, not unicode.
64    Python 3 only wants unicode strings.
65    """
66    try:
67        os.environ[var] = val
68    except UnicodeEncodeError:
69        os.environ[var] = val.encode('utf-8')
70