diff --git a/__init__.py b/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/homebrew.py b/homebrew.py new file mode 100644 index 00000000..4f1606a2 --- /dev/null +++ b/homebrew.py @@ -0,0 +1,138 @@ +from re import search +from sys import platform +from subprocess import check_output, CalledProcessError + + +class HomeBrew: + def __init__(self): + self._libdirs = None + self._incdirs = 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 + + 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): + lines: list[str] = self._check_netsnmp_isinstalled(self) + self._add_homebrew_platform_info(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() + include_dir = list(filter(lambda l: "include/net-snmp" in l, lines))[0] + self._incdirs.append( + include_dir[: include_dir.index("include/net-snmp") + 7] + ) + + self.get_lines(lines) + return lines + + def get_lines(self, lines): + return lines + + # 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._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 + openssl_ver = self._get_openssl_ver(self, brew) + self._append_openssl_paths(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 + + return 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._libdirs.append(temp_path + "/lib") + self._incdirs.append(temp_path + "/include") + + print(f"libdirs: {self._libdirs}") + print(f"incdirs: {self._incdirs}") + print(f"openssl_ver: {openssl_ver}") diff --git a/setup.py b/setup.py index ab46bf56..5b3fe8e9 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ from setuptools.command.build_ext import build_ext as BuildCommand import setuptools.command.build as build from setuptools import dist -from re import search +from homebrew import HomeBrew # Determine if a base directory has been provided with the --basedir option basedir = None @@ -67,83 +67,24 @@ libdirs = [flag[2:] for flag in s_split(netsnmp_libs) if flag[:2] == "-L"] incdirs = ["ezsnmp/include/"] - print(f"libs: {libs}") - print(f"libdirs: {libdirs}") - print(f"incdirs: {incdirs}") - - 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() - include_dir = list(filter(lambda l: "include/net-snmp" in l, lines))[0] - incdirs.append(include_dir[: include_dir.index("include/net-snmp") + 7]) - - if platform == "darwin": - lib_dir = list( - filter(lambda l: "lib/libnetsnmp.dylib" in l, lines) - )[0] - 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() - openssl_ver = 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(openssl_ver), 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() - - libdirs.append(temp_path + "/lib") - incdirs.append(temp_path + "/include") - - print(f"libdirs: {libdirs}") - print(f"incdirs: {incdirs}") - print(f"openssl_ver: {openssl_ver}") - - except CalledProcessError: - print("A brew command failed...") - pass - - except CalledProcessError: - print("Homebrew isn't installed...") - pass + hb = HomeBrew() + if hb.libdirs and hb.incdirs: + libdirs = hb.libdirs + incdirs = hb.incdirs + homebrew_version = hb.homebrew_version + homebrew_netsnmp_version = hb.homebrew_netsnmp_version + homebrew_openssl_version = hb.homebrew_openssl_version print(f"in_tree: {in_tree}") print(f"compile_args: {compile_args}") print(f"link_args: {link_args}") print(f"platform: {platform}") -print(f"netsnmp_version: {netsnmp_version}") -print(f"homebrew_version: {homebrew_version}") +print(f"homebrew_version: {str(homebrew_version).strip()}") +print(f"homebrew_netsnmp_version: {homebrew_netsnmp_version}") +print(f"homebrew_openssl_version: {homebrew_openssl_version}") +print(f"libs: {libs}") +print(f"libdirs: {libdirs}") +print(f"incdirs: {incdirs}") class RelinkLibraries(BuildCommand): @@ -156,6 +97,9 @@ class RelinkLibraries(BuildCommand): Non-brew installations and non-macOS systems will not be affected. """ + def __init__(): + pass + def run(self): BuildCommand.run(self) if platform == "darwin": # Newer Net-SNMP dylib may not be linked to properly @@ -165,6 +109,8 @@ def run(self): ).decode() except CalledProcessError: return + + lines = hb.get_lines() lib_dir = list(filter(lambda l: "lib/libnetsnmp.dylib" in l, lines))[0] b = build.build(dist.Distribution()) # Dynamically determine build path b.finalize_options()