def cd_ctx(directory):
    """
    Context manager. Stores current dir, then changes to given directory.
    At the end it changes back.
    :param directory:
    :return:
    """
    prevdir = os.path.abspath(os.curdir)
    if os.path.isdir(directory):
        os.chdir(directory)
    yield
    os.chdir(prevdir)