Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize setup.py #262

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
138 changes: 138 additions & 0 deletions homebrew.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
from re import search
from sys import platform
from subprocess import check_output, CalledProcessError


class HomeBrew:
carlkidcrypto marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self):
self._libdirs = None
self._incdirs = None
self._check_brew_isinstalled()
carlkidcrypto marked this conversation as resolved.
Show resolved Hide resolved

@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/[email protected]
# 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}")
92 changes: 19 additions & 73 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 @@ -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/[email protected]
# 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):
Expand All @@ -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
Expand All @@ -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()
Expand Down