Skip to content

Commit

Permalink
homebrew.py added, homebrew functions seperated with setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ademhilmibozkurt committed Dec 12, 2024
1 parent 7d0673e commit 8829e55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 99 deletions.
135 changes: 39 additions & 96 deletions homebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,61 @@
from subprocess import check_output, CalledProcessError

class HomeBrew:
def __init__(self, libdirs: list[str], incdirs: list[str], openssl_ver: str):
def __init__(self, libdirs: list[str], incdirs: list[str]):
self._libdirs = libdirs
self._incdirs = incdirs
self._openssl_ver = openssl_ver

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()
if search(r"Homebrew (\d+\.\d+\.\d+)", homebrew_version):
pass


except CalledProcessError:
print("Homebrew isn't installed...")
pass

def check_netsnmp_isinstalled(self) -> None:
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()
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])
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._openssl_ver = self.get_openssl_ver(brew)
openssl_ver = self.get_openssl_ver(self, brew)
self.append_openssl_paths(self, openssl_ver)
except CalledProcessError:
print("A brew command failed...")
pass

# no need try-except. check_output may throw CalledProcessError
def brew_platform_info(self) -> 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_openssl_ver(self, brew) -> list:
# The homebrew version also depends on the Openssl keg
openssl_ver = list(
Expand All @@ -64,74 +75,15 @@ def get_openssl_ver(self, brew) -> list:

return openssl_ver

def append_openssl_paths(self) -> None:
def append_openssl_paths(self, openssl_ver) -> None:
try:
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/[email protected]
# License: Apache-2.0
# ...
# print(temp)
temp_path = str(temp[4].split("(")[0]).strip()

self._libdirs.append(temp_path + "/lib")
self._incdirs.append(temp_path + "/include")
brew = check_output("brew info {0}".format(openssl_ver), shell=True).decode()

except CalledProcessError:
print("A brew command failed...")
pass

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")
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)
Expand All @@ -143,21 +95,12 @@ def append_openssl_paths(self) -> None:
# From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/o/[email protected]
# License: Apache-2.0
# ...
# print(temp)
temp = brew.split("\n")
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
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}")
13 changes: 10 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +71,9 @@
print(f"libdirs: {libdirs}")
print(f"incdirs: {incdirs}")

hb = HomeBrew(libdirs=libdirs, incdirs=incdirs)
hb.check_brew_isinstalled()

print(f"in_tree: {in_tree}")
print(f"compile_args: {compile_args}")
print(f"link_args: {link_args}")
Expand All @@ -89,6 +92,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
Expand All @@ -98,6 +104,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()
Expand Down Expand Up @@ -130,7 +138,6 @@ def run(self):
shell=True,
)

# this lines must stay for setup
setup(
ext_modules=[
Extension(
Expand Down Expand Up @@ -187,4 +194,4 @@ def run(self):
extra_link_args=link_args,
),
],
)
)

0 comments on commit 8829e55

Please sign in to comment.