host.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import os
  15. import shutil
  16. import sys
  17. import tempfile
  18. class Host:
  19. def __init__(self):
  20. self.stdin = sys.stdin
  21. self.stdout = sys.stdout
  22. self.stderr = sys.stderr
  23. def chdir(self, *comps):
  24. return os.chdir(self.join(*comps))
  25. def getcwd(self):
  26. return os.getcwd()
  27. def join(self, *comps):
  28. return os.path.join(*comps)
  29. def mkdtemp(self, **kwargs):
  30. return tempfile.mkdtemp(**kwargs)
  31. def print(self, msg='', end='\n', file=None):
  32. file = file or self.stdout
  33. file.write(str(msg) + end)
  34. file.flush()
  35. def rmtree(self, path):
  36. shutil.rmtree(path, ignore_errors=True)
  37. def read_text_file(self, path):
  38. with open(path, 'rb') as fp:
  39. return fp.read().decode('utf8')
  40. def write_text_file(self, path, contents):
  41. with open(path, 'wb') as f:
  42. f.write(contents.encode('utf8'))