Skip to content

Commit

Permalink
conflicts are resolved, setup refactored, homebrew edited, macports s…
Browse files Browse the repository at this point in the history
…eperated
  • Loading branch information
ademhilmibozkurt committed Dec 22, 2024
1 parent db4bccb commit ac4a296
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 130 deletions.
152 changes: 152 additions & 0 deletions homebrew.py
Original file line number Diff line number Diff line change
@@ -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/[email protected]
# 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
51 changes: 51 additions & 0 deletions macports.py
Original file line number Diff line number Diff line change
@@ -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 ""
150 changes: 20 additions & 130 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/[email protected]
# 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
Expand All @@ -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}")
Expand All @@ -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}")
Expand Down

0 comments on commit ac4a296

Please sign in to comment.