From ac4a2962e18b3350931763ce7873777a6dfe612e Mon Sep 17 00:00:00 2001 From: ademhilmibozkurt Date: Sun, 22 Dec 2024 09:07:21 +0300 Subject: [PATCH] conflicts are resolved, setup refactored, homebrew edited, macports seperated --- homebrew.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ macports.py | 51 ++++++++++++++++++ setup.py | 150 +++++++-------------------------------------------- 3 files changed, 223 insertions(+), 130 deletions(-) create mode 100644 homebrew.py create mode 100644 macports.py diff --git a/homebrew.py b/homebrew.py new file mode 100644 index 00000000..c2e8f366 --- /dev/null +++ b/homebrew.py @@ -0,0 +1,152 @@ +from re import search +from sys import platform +from subprocess import check_output, CalledProcessError + + +class HomeBrew: + def __init__(self): + self._temp_libdirs = [] + self._temp_incdirs = [] + self._libdirs = None + self._incdirs = None + self._lines = [] + self._homebrew_version = None + self._netsnmp_version = None + self._openssl_version = None + self._check_brew_isinstalled() + + @property + def libdirs(self): + return self._libdirs + + @property + def incdirs(self): + return self._incdirs + + @property + def homebrew_version(self): + return self._homebrew_version + + @property + def homebrew_netsnmp_version(self): + return self._netsnmp_version + + @property + def homebrew_openssl_version(self): + return self._openssl_version + + @property + def get_lines(self): + return self._lines + + def _check_brew_isinstalled(self): + # Check if brew is installed via: `brew --version` it should return something like: `Homebrew 4.4.5` + try: + homebrew_version = check_output("brew --version", shell=True).decode() + self._homebrew_version = homebrew_version + except CalledProcessError: + print("Homebrew isn't installed...") + pass + + else: + if search(r"Homebrew (\d+\.\d+\.\d+)", homebrew_version): + self._check_netsnmp_isinstalled(self) + self._add_homebrew_platform_info(self, self._lines) + self._get_homebrew_net_snmp_info(self) + + def _check_netsnmp_isinstalled(self) -> list[str]: + # Check if net-snmp is installed via Brew + try: + brew = check_output("brew list net-snmp 2>/dev/null", shell=True).decode() + + except CalledProcessError: + print("net-snmp isn't installed via HomeBrew...") + pass + + else: + lines = brew.splitlines() + self._lines = lines + + # extract brew version here... + pattern = r"/opt/homebrew/Cellar/net-snmp/(\d+\.\d+\.\d+)/" + match = search(pattern, lines[0]) + if match: + homebrew_netsnmp_version = match.group(1) + + temp_include_dir = list(filter(lambda l: "include/net-snmp" in l, lines))[0] + + self._temp_incdirs.append( + temp_include_dir[: temp_include_dir.index("include/net-snmp") + 7] + ) + + self._netsnmp_version = homebrew_netsnmp_version + + # no need try-except. check_output may throw CalledProcessError + def _add_homebrew_platform_info(self, lines: list[str]) -> None: + if platform == "darwin": + lib_dir = list(filter(lambda l: "lib/libnetsnmp.dylib" in l, lines))[0] + self._temp_libdirs.append( + lib_dir[: lib_dir.index("lib/libnetsnmp.dylib") + 3] + ) + + def _get_homebrew_net_snmp_info(self): + # The homebrew version also depends on the Openssl keg + try: + brew = check_output( + "brew info net-snmp", shell=True + ).decode() # this may cause error + + self._netsnmp_version = brew + self._get_openssl_ver(self, brew) + self._append_openssl_paths(self, self._openssl_ver) + + except CalledProcessError: + print("A brew command failed...") + pass + + def _get_openssl_ver(self, brew) -> list: + # The homebrew version also depends on the Openssl keg + openssl_ver = list( + filter( + lambda o: "openssl" in o, + *map( + str.split, + filter( + lambda l: "openssl" in l, + str(brew.replace("'", "")).split("\n"), + ), + ), + ) + )[0] + self._openssl_version = openssl_ver + + def _append_openssl_paths(self, openssl_ver) -> None: + try: + brew = check_output( + "brew info {0}".format(openssl_ver), shell=True + ).decode() + + except CalledProcessError: + print("A brew command failed...") + pass + + else: + # As of 06/04/2024 brew info openssl spits out lines. the fifth one is what we care about + # This works for now, but we need a better solution + # ==> openssl@3: stable 3.3.0 (bottled) + # Cryptography and SSL/TLS Toolkit + # https://openssl.org/ + # Installed + # /opt/homebrew/Cellar/openssl@3/3.3.0 (6,977 files, 32.4MB) * + # Poured from bottle using the formulae.brew.sh API on 2024-06-04 at 21:17:37 + # From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/o/openssl@3.rb + # License: Apache-2.0 + # ... + temp = brew.split("\n") + temp_path = str(temp[4].split("(")[0]).strip() + + self._temp_libdirs.append(temp_path + "/lib") + self._temp_incdirs.append(temp_path + "/include") + + self._libdirs = self._libdirs + self._temp_libdirs + self._incdirs = self._incdirs + self._temp_incdirs diff --git a/macports.py b/macports.py new file mode 100644 index 00000000..29138400 --- /dev/null +++ b/macports.py @@ -0,0 +1,51 @@ +from re import search +from subprocess import check_output, CalledProcessError + + +class MacPorts: + def __init__(): + pass + + # Install Helpers + + +def is_macports_installed(): + """ + Checks if MacPorts is installed on the system. + + Returns: + str: The MacPorts version if installed, "" otherwise. + """ + try: + # Check if the `port` command is available and get the version + version_output = check_output("port version", shell=True).decode() + # Extract version using regex. It'll match something like: Version: `2.10.5`. + match = search(r"Version:\s+(\d+\.\d+\.\d+)", version_output) + if match: + return match.group(1) + else: + return "" + except CalledProcessError: + return "" + + +def is_net_snmp_installed_macports(): + """ + Checks if any version of net-snmp is installed via MacPorts. + + Returns: + str: The net-snmp version if installed, "" otherwise. + """ + + try: + # Use `port installed` to check for the package + macports_output = check_output("port installed net-snmp", shell=True).decode() + # Use regex to match and extract the version + pattern = r"net-snmp @(\d+\.\d+\.\d+[_+a-zA-Z0-9]*) \(active\)" + match = search(pattern, macports_output) + if match: + return match.group(1) + else: + return "" + except CalledProcessError: + return "" diff --git a/setup.py b/setup.py index 5cb0b620..82d2e597 100644 --- a/setup.py +++ b/setup.py @@ -6,48 +6,8 @@ from setuptools import setup, Extension from re import search - -# Install Helpers -def is_macports_installed(): - """ - Checks if MacPorts is installed on the system. - - Returns: - str: The MacPorts version if installed, "" otherwise. - """ - try: - # Check if the `port` command is available and get the version - version_output = check_output("port version", shell=True).decode() - # Extract version using regex. It'll match something like: Version: `2.10.5`. - match = search(r"Version:\s+(\d+\.\d+\.\d+)", version_output) - if match: - return match.group(1) - else: - return "" - except CalledProcessError: - return "" - - -def is_net_snmp_installed_macports(): - """ - Checks if any version of net-snmp is installed via MacPorts. - - Returns: - str: The net-snmp version if installed, "" otherwise. - """ - - try: - # Use `port installed` to check for the package - macports_output = check_output("port installed net-snmp", shell=True).decode() - # Use regex to match and extract the version - pattern = r"net-snmp @(\d+\.\d+\.\d+[_+a-zA-Z0-9]*) \(active\)" - match = search(pattern, macports_output) - if match: - return match.group(1) - else: - return "" - except CalledProcessError: - return "" +from macports import MacPorts +from homebrew import HomeBrew # Determine if a base directory has been provided with the --basedir option @@ -113,85 +73,14 @@ def is_net_snmp_installed_macports(): libdirs = [flag[2:] for flag in s_split(netsnmp_libs) if flag[:2] == "-L"] incdirs = ["ezsnmp/include/"] - try: - # Check if brew is installed via: `brew --version` it should return something like: `Homebrew 4.4.5` - homebrew_version = check_output("brew --version", shell=True).decode() - if search(r"Homebrew (\d+\.\d+\.\d+)", homebrew_version): - # Check if net-snmp is installed via Brew - try: - brew = check_output( - "brew list net-snmp 2>/dev/null", shell=True - ).decode() - lines = brew.splitlines() - # extract brew version here... - pattern = r"/opt/homebrew/Cellar/net-snmp/(\d+\.\d+\.\d+)/" - match = search(pattern, lines[0]) - if match: - version = match.group(1) - homebrew_netsnmp_version = version - - temp_include_dir = list( - filter(lambda l: "include/net-snmp" in l, lines) - )[0] - temp_incdirs = [] - temp_libdirs = [] - temp_incdirs.append( - temp_include_dir[: temp_include_dir.index("include/net-snmp") + 7] - ) - - if platform == "darwin": - lib_dir = list( - filter(lambda l: "lib/libnetsnmp.dylib" in l, lines) - )[0] - temp_libdirs.append( - lib_dir[: lib_dir.index("lib/libnetsnmp.dylib") + 3] - ) - - # The homebrew version also depends on the Openssl keg - brew = check_output("brew info net-snmp", shell=True).decode() - homebrew_openssl_version = list( - filter( - lambda o: "openssl" in o, - *map( - str.split, - filter( - lambda l: "openssl" in l, - str(brew.replace("'", "")).split("\n"), - ), - ), - ) - )[0] - - brew = check_output( - "brew info {0}".format(homebrew_openssl_version), shell=True - ).decode() - temp = brew.split("\n") - # As of 06/04/2024 brew info openssl spits out lines. the fifth one is what we care about - # This works for now, but we need a better solution - # ==> openssl@3: stable 3.3.0 (bottled) - # Cryptography and SSL/TLS Toolkit - # https://openssl.org/ - # Installed - # /opt/homebrew/Cellar/openssl@3/3.3.0 (6,977 files, 32.4MB) * - # Poured from bottle using the formulae.brew.sh API on 2024-06-04 at 21:17:37 - # From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/o/openssl@3.rb - # License: Apache-2.0 - # ... - # print(temp) - temp_path = str(temp[4].split("(")[0]).strip() - - temp_libdirs.append(temp_path + "/lib") - temp_incdirs.append(temp_path + "/include") - - libdirs = libdirs + temp_libdirs - incdirs = incdirs + temp_incdirs - - except CalledProcessError: - print("A brew command failed...") - - except CalledProcessError: - homebrew_version = None - print("Homebrew is not installed...") + homebrew = HomeBrew() + # Make HomeBrew() getters return None or "" if libdirs or incdirs aren't found. + if homebrew.libdirs and homebrew.incdirs: + libdirs = homebrew.libdirs + incdirs = homebrew.incdirs + homebrew_version = homebrew.homebrew_version + homebrew_netsnmp_version = homebrew.homebrew_netsnmp_version + homebrew_openssl_version = homebrew.homebrew_openssl_version # Add in system includes instead of Homebrew ones netsnmp_incdir = None @@ -208,15 +97,16 @@ def is_net_snmp_installed_macports(): incdirs = incdirs + [netsnmp_incdir] break - macports_version = is_macports_installed() - macports_netsnmp_version = is_net_snmp_installed_macports() - # macports_openssl_version = is_openssl_installed_macports() + macports = MacPorts() + macports_version = macports.is_macports_installed() + macports_netsnmp_version = macports.is_net_snmp_installed_macports() + # macports_openssl_version = is_openssl_installed_macports() - if macports_version and macports_netsnmp_version: - for dir in libdirs: - if "/opt/local/lib" in dir: - netsnmp_incdir = dir.replace("lib", "include") - incdirs = incdirs + [netsnmp_incdir] + if macports_version and macports_netsnmp_version: + for dir in libdirs: + if "/opt/local/lib" in dir: + netsnmp_incdir = dir.replace("lib", "include") + incdirs = incdirs + [netsnmp_incdir] print(f"in_tree: {in_tree}") print(f"compile_args: {compile_args}") @@ -228,7 +118,7 @@ def is_net_snmp_installed_macports(): print(f"homebrew_openssl_version: {homebrew_openssl_version}") print(f"macports_version: {str(macports_version).strip()}") print(f"macports_netsnmp_version: {macports_netsnmp_version}") -print(f"macports_openssl_version: {macports_openssl_version}") +# print(f"macports_openssl_version: {macports_openssl_version}") print(f"libs: {libs}") print(f"libdirs: {libdirs}") print(f"incdirs: {incdirs}")