-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrewinfo.py
239 lines (225 loc) · 8.56 KB
/
brewinfo.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import requests
import json
import sys
import subprocess
def get_outdated_list(brewtype='all'):
if brewtype == 'cask':
output = subprocess.run(['brew', 'outdated', '--cask', '--json'], capture_output=True, text=True)
elif brewtype == 'formula':
output = subprocess.run(['brew', 'outdated', '--formula', '--json'], capture_output=True, text=True)
else:
output = subprocess.run(['brew', 'outdated', '--json'], capture_output=True, text=True)
outdated_list = json.loads(output.stdout)
result = {"items": []}
if 'formulae' in outdated_list:
for package in outdated_list['formulae']:
name = package['name']
installed_version = package['installed_versions'][0]
current_version = package['current_version']
result["items"].append({
"title": f'{name} ({installed_version}) < {current_version}',
"subtitle": f'⏎ to run "brew upgrade {name}"',
"icon": {
"path": "icons/formula_outdated.png"
},
"arg": 'brew upgrade ' + name,
"autocomplete": name,
"quicklookurl": f'https://formulae.brew.sh/formula/{name}'
})
if 'casks' in outdated_list:
for package in outdated_list['casks']:
name = package['name']
installed_version = package['installed_versions'][0]
current_version = package['current_version']
result["items"].append({
"title": f'{name} ({installed_version}) < {current_version}',
"subtitle": f'⏎ to run "brew upgrade --cask {name}"',
"icon": {
"path": "icons/cask_outdated.png"
},
"arg": 'brew upgrade --cask ' + name,
"autocomplete": name,
"quicklookurl": f'https://formulae.brew.sh/cask/{name}'
})
if not result["items"]:
result["items"].append({
"title": "Everything is up to date",
"icon": {
"path": "icons/uptodate.png"
},
"valid": False
})
else:
result["items"].append({
"title": "Upgrade all",
"icon": {
"path": "icons/update_all.png"
},
"arg": "brew update && brew upgrade",
"autocomplete": "Upgrade all",
"valid": True
})
return result
def get_brew_leaves():
output = subprocess.run(['brew', 'leaves'], capture_output=True, text=True)
lines = output.stdout.split('\n')
result = {"items": []}
for line in lines:
if not line:
continue
result["items"].append({
"title": line,
"icon": {
"path": "icons/leaves.png"
},
"arg": line,
"autocomplete": line,
"quicklookurl": f'https://formulae.brew.sh/formula/{line}',
"mods": {
"cmd": {
"valid": True,
"subtitle": 'brew uninstall ' + line,
"arg": 'brew uninstall ' + line,
},
},
})
return result
def get_brew_list(brewtype='all'):
if brewtype == 'cask':
output = subprocess.run(['brew', 'list', '--versions', '--cask'], capture_output=True, text=True)
uninstall_command = 'brew uninstall --cask '
force_uninstall_command = 'brew uninstall --cask --force --zap '
icon_path = {"path": "icons/cask_check.png"}
elif brewtype == 'formula':
output = subprocess.run(['brew', 'list', '--versions', '--formula'], capture_output=True, text=True)
uninstall_command = 'brew uninstall '
force_uninstall_command = 'brew uninstall '
icon_path = {"path": "icons/formula_check.png"}
else:
output = subprocess.run(['brew', 'list', '--versions'], capture_output=True, text=True)
uninstall_command = 'brew uninstall '
force_uninstall_command = 'brew uninstall --force '
icon_path = {"path": "icons/check.png"}
lines = output.stdout.split('\n')
result = {"items": []}
for line in lines:
if not line:
continue
name, version = line.split(' ', 1)
result["items"].append({
"title": f'{name} - {version}',
"icon": icon_path,
"arg": name,
"autocomplete": name,
"mods": {
"alt": {
"valid": True,
"subtitle": uninstall_command + name,
"arg": uninstall_command + name,
},
"cmd": {
"valid": True,
"subtitle": force_uninstall_command + name,
"arg": force_uninstall_command + name,
},
},
})
return result
def get_all_formula_names(brewtype):
response = requests.get('https://formulae.brew.sh/api/'+brewtype+'.json')
icon_path = {"path": f"icons/{brewtype}.png"}
data = response.json()
items = []
for item in data:
if brewtype == 'cask':
name = item['name'][0]
token = item['token']
install_command = 'brew install --cask ' + token
try:
subtitle = name + ' ℹ️ '+ item['desc']
except:
subtitle = name
elif brewtype == 'formula':
token = item['name']
subtitle = item['desc']
install_command = 'brew install ' + token
formula = {
"valid": True,
"title": token,
"subtitle": subtitle,
"arg": token,
"icon": icon_path,
"autocomplete": token,
"quicklookurl": f'https://formulae.brew.sh/{brewtype}/{token}',
"match": brewtype + ' ' + token,
"mods": {
"cmd": {
"valid": True,
"subtitle": "Run install commmand: " +install_command,
"arg": install_command,
},
},
}
items.append(formula)
return items
def get_info(brewtype,formula_name):
output_data = {"items": []}
token = formula_name.lower().strip()
response = requests.get(f'https://formulae.brew.sh/api/{brewtype}/{token}.json')
info_page = f'https://formulae.brew.sh/{brewtype}/{token}'
data = response.json()
if brewtype == 'cask':
version = data['version']
auto_update = '\t🔄 Auto updates = ✅' if data['auto_updates'] == True else '\t🔄 Auto updates = ❌'
version_info = f'Newest version: {version}, {auto_update}'
elif brewtype == 'formula':
if data['versions']['bottle'] == True:
version = data['versions']['stable'] + ' (bottle)'
else:
version = data['versions']['stable']
version_info = f'Newest version: {version}'
output_data['items'].extend([
{
"valid": True,
"title": data['homepage'],
"subtitle": "Open homepage",
"arg": data['homepage'],
"icon": {"path": "icons/homepage.png"},
},
{
"valid": True,
"title": info_page,
"subtitle": "Open brew.sh info page",
"arg": info_page,
},
{
"valid": False,
"title": f"Installs (30 days): {data['analytics']['install']['30d'][token]}\t(90 days): {data['analytics']['install']['90d'][token]}\t (365 days): {data['analytics']['install']['365d'][token]}",
"icon": {"path": "icons/hot.png"},
},
{
"valid": False,
'title': version_info,
"icon": {"path": "icons/version.png"},
}
])
return output_data
if __name__ == '__main__':
output_data = {"items": []}
if sys.argv[1] == 'all':
output_data['items'].extend(get_all_formula_names(brewtype='cask'))
output_data['items'].extend(get_all_formula_names(brewtype='formula'))
elif sys.argv[1] == 'list':
# output_data = get_brew_list()
output_data['items'].extend(get_brew_list(brewtype='cask')['items'])
output_data['items'].extend(get_brew_list(brewtype='formula')['items'])
elif sys.argv[1] == 'leaves':
output_data = get_brew_leaves()
elif sys.argv[1] == 'get_info':
try:
output_data = get_info('cask',sys.argv[2])
except:
output_data = get_info('formula',sys.argv[2])
if sys.argv[1] == 'outdated':
output_data = get_outdated_list()
print(json.dumps(output_data))