def METHOD_NAME(self, build=True):
    ei_cmd = self.get_finalized_command("egg_info")
    if build:
        self.build_in_place()
    else:
        ei_cmd.run()
    old_path = sys.path[:]
    old_modules = sys.modules.copy()
    try:
        sys.path.insert(0, pkg_resources.normalize_path(ei_cmd.egg_base))
        pkg_resources.working_set.__init__()
        pkg_resources.add_activation_listener(lambda dist: dist.activate())
        pkg_resources.require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
        yield
    finally:
        sys.path[:] = old_path
        sys.modules.clear()
        sys.modules.update(old_modules)
        pkg_resources.working_set.__init__()