import os
import subprocess
import sys

from virtinst import log

def get_gst_plugin_whitelist():
    """
    Return the installed GStreamer plugin names, excluding the obsolete
    Python 3.6 plugin which crashes a Python 3.9 virt-manager process.
    """
    log.debug("Cygwin: enumerating GStreamer plugins")

    try:
        result = subprocess.run(
            ["gst-inspect-1.0"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            check=True,
        )
    except (OSError, subprocess.CalledProcessError) as err:
        log.debug("Cygwin: failed to enumerate GStreamer plugins: %s", err)
        return []

    plugins = set()

    for line in result.stdout.splitlines():
        if ":" not in line:
            continue

        name = line.split(":", 1)[0].strip()

        if not name:
            continue

        # gst-inspect summary, not a plugin
        if name == "Total count":
            continue

        # Static plugin, not dynamically loaded.
        if name == "staticelements":
            continue

        # Known incompatible CPython 3.6 GStreamer plugin.
        if name == "python":
            continue

        plugins.add(name)

    plugins = sorted(plugins)

    log.debug(
        "Cygwin: found %d usable GStreamer plugins",
        len(plugins),
    )

    return plugins


def install_gst_plugin_whitelist(plugins):
    if not plugins:
        log.debug("Cygwin: GStreamer plugin whitelist is empty, not installing")
        return False

    value = os.pathsep.join(plugins)
    os.environ["GST_PLUGIN_LOADING_WHITELIST"] = value

    log.debug(
        "Cygwin: installed GST_PLUGIN_LOADING_WHITELIST with %d plugins",
        len(plugins),
    )

    return True


def apply_gst_plugin_whitelist():
    if sys.platform != "cygwin":
        return

    if "GST_PLUGIN_LOADING_WHITELIST" in os.environ:
        log.debug(
            "Cygwin: GST_PLUGIN_LOADING_WHITELIST already set, leaving unchanged"
        )
        return

    plugins = get_gst_plugin_whitelist()
    install_gst_plugin_whitelist(plugins)
import os
import subprocess
import sys

from virtinst import log

def get_gst_plugin_whitelist():
    """
    Return the installed GStreamer plugin names, excluding the obsolete
    Python 3.6 plugin which crashes a Python 3.9 virt-manager process.
    """
    log.debug("Cygwin: enumerating GStreamer plugins")

    try:
        result = subprocess.run(
            ["gst-inspect-1.0"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            check=True,
        )
    except (OSError, subprocess.CalledProcessError) as err:
        log.debug("Cygwin: failed to enumerate GStreamer plugins: %s", err)
        return []

    plugins = set()

    for line in result.stdout.splitlines():
        if ":" not in line:
            continue

        name = line.split(":", 1)[0].strip()

        if not name:
            continue

        # gst-inspect summary, not a plugin
        if name == "Total count":
            continue

        # Static plugin, not dynamically loaded.
        if name == "staticelements":
            continue

        # Known incompatible CPython 3.6 GStreamer plugin.
        if name == "python":
            continue

        plugins.add(name)

    plugins = sorted(plugins)

    log.debug(
        "Cygwin: found %d usable GStreamer plugins",
        len(plugins),
    )

    return plugins


def install_gst_plugin_whitelist(plugins):
    if not plugins:
        log.debug("Cygwin: GStreamer plugin whitelist is empty, not installing")
        return False

    value = os.pathsep.join(plugins)
    os.environ["GST_PLUGIN_LOADING_WHITELIST"] = value

    log.debug(
        "Cygwin: installed GST_PLUGIN_LOADING_WHITELIST with %d plugins",
        len(plugins),
    )

    return True


def apply_gst_plugin_whitelist():
    if sys.platform != "cygwin":
        return

    if "GST_PLUGIN_LOADING_WHITELIST" in os.environ:
        log.debug(
            "Cygwin: GST_PLUGIN_LOADING_WHITELIST already set, leaving unchanged"
        )
        return

    plugins = get_gst_plugin_whitelist()
    install_gst_plugin_whitelist(plugins)
import os
import subprocess
import sys

from virtinst import log

def get_gst_plugin_whitelist():
    """
    Return the installed GStreamer plugin names, excluding the obsolete
    Python 3.6 plugin which crashes a Python 3.9 virt-manager process.
    """
    log.debug("Cygwin: enumerating GStreamer plugins")

    try:
        result = subprocess.run(
            ["gst-inspect-1.0"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            check=True,
        )
    except (OSError, subprocess.CalledProcessError) as err:
        log.debug("Cygwin: failed to enumerate GStreamer plugins: %s", err)
        return []

    plugins = set()

    for line in result.stdout.splitlines():
        if ":" not in line:
            continue

        name = line.split(":", 1)[0].strip()

        if not name:
            continue

        # gst-inspect summary, not a plugin
        if name == "Total count":
            continue

        # Static plugin, not dynamically loaded.
        if name == "staticelements":
            continue

        # Known incompatible CPython 3.6 GStreamer plugin.
        if name == "python":
            continue

        plugins.add(name)

    plugins = sorted(plugins)

    log.debug(
        "Cygwin: found %d usable GStreamer plugins",
        len(plugins),
    )

    return plugins


def install_gst_plugin_whitelist(plugins):
    if not plugins:
        log.debug("Cygwin: GStreamer plugin whitelist is empty, not installing")
        return False

    value = os.pathsep.join(plugins)
    os.environ["GST_PLUGIN_LOADING_WHITELIST"] = value

    log.debug(
        "Cygwin: installed GST_PLUGIN_LOADING_WHITELIST with %d plugins",
        len(plugins),
    )

    return True


def apply_gst_plugin_whitelist():
    if sys.platform != "cygwin":
        return

    if "GST_PLUGIN_LOADING_WHITELIST" in os.environ:
        log.debug(
            "Cygwin: GST_PLUGIN_LOADING_WHITELIST already set, leaving unchanged"
        )
        return

    plugins = get_gst_plugin_whitelist()
    install_gst_plugin_whitelist(plugins)
import os
import subprocess
import sys

from virtinst import log

def get_gst_plugin_whitelist():
    """
    Return the installed GStreamer plugin names, excluding the obsolete
    Python 3.6 plugin which crashes a Python 3.9 virt-manager process.
    """
    log.debug("Cygwin: enumerating GStreamer plugins")

    try:
        result = subprocess.run(
            ["gst-inspect-1.0"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            universal_newlines=True,
            check=True,
        )
    except (OSError, subprocess.CalledProcessError) as err:
        log.debug("Cygwin: failed to enumerate GStreamer plugins: %s", err)
        return []

    plugins = set()

    for line in result.stdout.splitlines():
        if ":" not in line:
            continue

        name = line.split(":", 1)[0].strip()

        if not name:
            continue

        # gst-inspect summary, not a plugin
        if name == "Total count":
            continue

        # Static plugin, not dynamically loaded.
        if name == "staticelements":
            continue

        # Known incompatible CPython 3.6 GStreamer plugin.
        if name == "python":
            continue

        plugins.add(name)

    plugins = sorted(plugins)

    log.debug(
        "Cygwin: found %d usable GStreamer plugins",
        len(plugins),
    )

    return plugins


def install_gst_plugin_whitelist(plugins):
    if not plugins:
        log.debug("Cygwin: GStreamer plugin whitelist is empty, not installing")
        return False

    value = os.pathsep.join(plugins)
    os.environ["GST_PLUGIN_LOADING_WHITELIST"] = value

    log.debug(
        "Cygwin: installed GST_PLUGIN_LOADING_WHITELIST with %d plugins",
        len(plugins),
    )

    return True


def apply_gst_plugin_whitelist():
    if sys.platform != "cygwin":
        return

    if "GST_PLUGIN_LOADING_WHITELIST" in os.environ:
        log.debug(
            "Cygwin: GST_PLUGIN_LOADING_WHITELIST already set, leaving unchanged"
        )
        return

    plugins = get_gst_plugin_whitelist()
    install_gst_plugin_whitelist(plugins)
