kivy-ios/kivy_ios/context_managers.py
Richard Larkin 7cb14fc7da
Fix/host setuptools3 (#533)
* status

* ♻️ Extract context managers

* 🎨 Fix typo

*  Use python_prefix

*  Remove unused import

* 📦 Trigger pipeline

Co-authored-by: richard <richard@dotmodus>
2020-07-26 21:07:10 +02:00

46 lines
1.1 KiB
Python

"""
This module houses context managers to assist in the managing of state during
kivy-ios builds.
"""
from logging import getLogger
from contextlib import contextmanager
from os import getcwd, chdir, environ
from os.path import expanduser
logger = getLogger(__name__)
@contextmanager
def cd(newdir):
"""
Set the current working directory to `newdir` for the duration of the
context.
"""
prevdir = getcwd()
logger.info("cd {}".format(newdir))
chdir(expanduser(newdir))
try:
yield
finally:
logger.info("cd {}".format(prevdir))
chdir(prevdir)
@contextmanager
def python_path(newdir):
"""
Set the PYTHONPATH environmnet variable to `newdir` for the duraiton of the
context.
"""
prevdir = environ.get("PYTHONPATH")
logger.debug("Setting PYTHONPATH to {}".format(newdir))
environ["PYTHONPATH"] = newdir
try:
yield
finally:
logger.debug("Setting PYTHONPATH to {}".format(prevdir))
if prevdir is None:
environ.pop("PYTHONPATH")
else:
environ["PYTHONPATH"] = prevdir