# -*- coding: utf-8 -*-

##############################################################################################
#                  This file is deprecated because Python 2.x is deprecated                  #
#                     A Python 3.x version of this file can be found at:                     #
#                                                                                            #
# https://github.com/Guymer/PyGuymer3/blob/master/find_program_version.py                    #
##############################################################################################

def find_program_version(prog = "python"):
    # Import modules ...
    import os
    import re
    import subprocess

    # Check if it is FreeBSD ...
    proc = subprocess.Popen(
        [
            "type",
            "pkg"
        ],
        stderr = open(os.devnull, "wt"),
        stdout = open(os.devnull, "wt")
    )
    proc.wait()
    if proc.returncode == 0:
        # Find raw string ...
        proc = subprocess.Popen(
            [
                "pkg",
                "info",
                prog
            ],
            stderr = subprocess.PIPE,
            stdout = subprocess.PIPE
        )
        stdout, stderr = proc.communicate()
        if proc.returncode != 0:
            raise Exception("\"pkg\" command failed")
    else:
        # Check if it is MacPorts ...
        proc = subprocess.Popen(
            [
                "type",
                "port"
            ],
            stderr = open(os.devnull, "wt"),
            stdout = open(os.devnull, "wt")
        )
        proc.wait()
        if proc.returncode == 0:
            # Find raw string ...
            proc = subprocess.Popen(
                [
                    "port",
                    "info",
                    "--version",
                    prog
                ],
                stderr = subprocess.PIPE,
                stdout = subprocess.PIPE
            )
            stdout, stderr = proc.communicate()
            if proc.returncode != 0:
                raise Exception("\"port\" command failed")
        else:
            # Check if it is OpenSUSE ...
            proc = subprocess.Popen(
                [
                    "type",
                    "zypper"
                ],
                stderr = open(os.devnull, "wt"),
                stdout = open(os.devnull, "wt")
            )
            proc.wait()
            if proc.returncode == 0:
                # Find raw string ...
                proc = subprocess.Popen(
                    [
                        "zypper",
                        "--disable-repositories",
                        "info",
                        prog
                    ],
                    stderr = subprocess.PIPE,
                    stdout = subprocess.PIPE
                )
                stdout, stderr = proc.communicate()
                if proc.returncode != 0:
                    raise Exception("\"zypper\" command failed")
            else:
                raise Exception("neither \"pkg\" nor \"port\" nor \"zypper\" have been found")

    # Find clean string ...
    for line in stdout.decode("utf-8").splitlines():
        match = re.match(r"^[Vv][Ee][Rr][Ss][Ii][Oo][Nn].+$", line)
        if match is None:
            continue
        return match.group(0).split(":")[1].strip()

    # Create final catch-all ...
    raise Exception("failed to extract version number for {0:s}".format(prog))

# -*- coding: utf-8 -*-

##############################################################################################
#                  This file is deprecated because Python 2.x is deprecated                  #
#                     A Python 3.x version of this file can be found at:                     #
#                                                                                            #
# https://github.com/Guymer/PyGuymer3/blob/master/find_program_version.py                    #
##############################################################################################

def find_program_version(prog = "python"):
    # Import modules ...
    import os
    import re
    import subprocess

    # Check if it is FreeBSD ...
    proc = subprocess.Popen(
        [
            "type",
            "pkg"
        ],
        stderr = open(os.devnull, "wt"),
        stdout = open(os.devnull, "wt")
    )
    proc.wait()
    if proc.returncode == 0:
        # Find raw string ...
        proc = subprocess.Popen(
            [
                "pkg",
                "info",
                prog
            ],
            stderr = subprocess.PIPE,
            stdout = subprocess.PIPE
        )
        stdout, stderr = proc.communicate()
        if proc.returncode != 0:
            raise Exception("\"pkg\" command failed")
    else:
        # Check if it is MacPorts ...
        proc = subprocess.Popen(
            [
                "type",
                "port"
            ],
            stderr = open(os.devnull, "wt"),
            stdout = open(os.devnull, "wt")
        )
        proc.wait()
        if proc.returncode == 0:
            # Find raw string ...
            proc = subprocess.Popen(
                [
                    "port",
                    "info",
                    "--version",
                    prog
                ],
                stderr = subprocess.PIPE,
                stdout = subprocess.PIPE
            )
            stdout, stderr = proc.communicate()
            if proc.returncode != 0:
                raise Exception("\"port\" command failed")
        else:
            # Check if it is OpenSUSE ...
            proc = subprocess.Popen(
                [
                    "type",
                    "zypper"
                ],
                stderr = open(os.devnull, "wt"),
                stdout = open(os.devnull, "wt")
            )
            proc.wait()
            if proc.returncode == 0:
                # Find raw string ...
                proc = subprocess.Popen(
                    [
                        "zypper",
                        "--disable-repositories",
                        "info",
                        prog
                    ],
                    stderr = subprocess.PIPE,
                    stdout = subprocess.PIPE
                )
                stdout, stderr = proc.communicate()
                if proc.returncode != 0:
                    raise Exception("\"zypper\" command failed")
            else:
                raise Exception("neither \"pkg\" nor \"port\" nor \"zypper\" have been found")

    # Find clean string ...
    for line in stdout.decode("utf-8").splitlines():
        match = re.match(r"^[Vv][Ee][Rr][Ss][Ii][Oo][Nn].+$", line)
        if match is None:
            continue
        return match.group(0).split(":")[1].strip()

    # Create final catch-all ...
    raise Exception("failed to extract version number for {0:s}".format(prog))

# -*- coding: utf-8 -*- ############################################################################################## #                  This file is deprecated because Python 2.x is deprecated                  # #                     A Python 3.x version of this file can be found at:                     # #                                                                                            # # https://github.com/Guymer/PyGuymer3/blob/master/find_program_version.py                    # ############################################################################################## # Import modules ... # Check if it is FreeBSD ... # Find raw string ... # Check if it is MacPorts ... # Find raw string ... # Check if it is OpenSUSE ... # Find raw string ... # Find clean string ... # Create final catch-all ...