# Path: functional_harness/env_general.py
# @contextmanager
# def env_vars(new: Dict[str, str]) -> Generator[None, None, None]:
#     """Context manager to temporarily change environment variables
# 
#     :param new: Items to be added to :any:`os.environ` for the lifetime of the
#         context manager.
#     """
#     old_vals = {}
#     try:
#         for key, val in new.items():
#             if key in os.environ:
#                 old_vals[key] = os.environ[key]
#             os.environ[key] = val
# 
#         yield
#     finally:
#         for key, val in old_vals.items():
#             os.environ[key] = val


import logging, os, random, shutil, subprocess, tempfile  # nosec
from contextlib import contextmanager
from distutils.spawn import find_executable
from .env_general import env_vars
from typing import Dict, Generator, List, Tuple
        while len(magic_cookie) < 32:
            magic_cookie += hex(random.getrandbits(128))[2:34].encode('ascii')
            magic_cookie = magic_cookie[:32]
        assert len(magic_cookie) == 32, len(magic_cookie)  # nosec
        xauthfile = os.path.join(tempdir, 'Xauthority')
        env = {'XAUTHORITY': xauthfile}

        open(xauthfile, 'w').close()  # create empty file

        # Convert `screens` into the format Xorg servers expect
        screen_argv = []
        for screen_num, screen_geom in screens.items():
            if 'Xvfb' in argv[0]:
                screen_argv.extend(['-screen', '%d' % screen_num, screen_geom])
            elif 'Xephyr' in argv[0]:
                screen_argv.extend(['-screen', screen_geom])
            else:
                raise ValueError("Unrecognized X server. Cannot infer format "
                                 "for specifying screen geometry.")

        # Initialize an X server on a free display number
        x_server, display_num = _init_x_server(argv + screen_argv)

        # Set up the environment and authorization
        env['DISPLAY'] = ':%s' % display_num.decode('utf8')
        subprocess.check_call(  # nosec
            ['xauth', 'add', env['DISPLAY'], '.', magic_cookie],
            env=env)
        # FIXME: This xauth call once had a random failure. Retry.


# Path: functional_harness/env_general.py
# @contextmanager
# def env_vars(new: Dict[str, str]) -> Generator[None, None, None]:
#     """Context manager to temporarily change environment variables
# 
#     :param new: Items to be added to :any:`os.environ` for the lifetime of the
#         context manager.
#     """
#     old_vals = {}
#     try:
#         for key, val in new.items():
#             if key in os.environ:
#                 old_vals[key] = os.environ[key]
#             os.environ[key] = val
# 
#         yield
#     finally:
#         for key, val in old_vals.items():
#             os.environ[key] = val


# Path: functional_harness/x_server.py
import logging, os, random, shutil, subprocess, tempfile  # nosec
from contextlib import contextmanager
from distutils.spawn import find_executable
from .env_general import env_vars
from typing import Dict, Generator, List, Tuple

        while len(magic_cookie) < 32:
            magic_cookie += hex(random.getrandbits(128))[2:34].encode('ascii')
            magic_cookie = magic_cookie[:32]
        assert len(magic_cookie) == 32, len(magic_cookie)  # nosec
        xauthfile = os.path.join(tempdir, 'Xauthority')
        env = {'XAUTHORITY': xauthfile}

        open(xauthfile, 'w').close()  # create empty file

        # Convert `screens` into the format Xorg servers expect
        screen_argv = []
        for screen_num, screen_geom in screens.items():
            if 'Xvfb' in argv[0]:
                screen_argv.extend(['-screen', '%d' % screen_num, screen_geom])
            elif 'Xephyr' in argv[0]:
                screen_argv.extend(['-screen', screen_geom])
            else:
                raise ValueError("Unrecognized X server. Cannot infer format "
                                 "for specifying screen geometry.")

        # Initialize an X server on a free display number
        x_server, display_num = _init_x_server(argv + screen_argv)

        # Set up the environment and authorization
        env['DISPLAY'] = ':%s' % display_num.decode('utf8')
        subprocess.check_call(  # nosec
            ['xauth', 'add', env['DISPLAY'], '.', magic_cookie],
            env=env)
        # FIXME: This xauth call once had a random failure. Retry.


        with env_vars(env):