forked from carlkidcrypto/ezsnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomebrew.py
152 lines (125 loc) · 5.06 KB
/
homebrew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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