1# Copyright 2014 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import fileinput
16import os
17import shutil
18import sys
19import tempfile
20
21
22if sys.version_info[0] < 3:
23    # pylint: disable=redefined-builtin
24    str = unicode
25
26
27class Host(object):
28    def __init__(self):
29        self.stdin = sys.stdin
30        self.stdout = sys.stdout
31        self.stderr = sys.stderr
32
33    def chdir(self, *comps):
34        return os.chdir(self.join(*comps))
35
36    def fileinput(self, files=None):
37        if not files:
38            return self.stdin.readlines()
39        return fileinput.input(files)
40
41    def getcwd(self):
42        return os.getcwd()
43
44    def join(self, *comps):
45        return os.path.join(*comps)
46
47    def mkdtemp(self, **kwargs):
48        return tempfile.mkdtemp(**kwargs)
49
50    def print_(self, msg=u'', end=u'\n', stream=None):
51        stream = stream or self.stdout
52        stream.write(str(msg) + end)
53        stream.flush()
54
55    def rmtree(self, path):
56        shutil.rmtree(path, ignore_errors=True)
57
58    def write_text_file(self, path, contents):
59        with open(path, 'wb') as f:
60            f.write(contents.encode('utf8'))
61