-
Notifications
You must be signed in to change notification settings - Fork 12
/
software_station_pkg.py
278 lines (241 loc) · 8.16 KB
/
software_station_pkg.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/local/bin/python3
from subprocess import Popen, PIPE, run
import socket
import requests
def network_stat():
cmd = "netstat -rn | grep default"
netstat = run(cmd, shell=True)
return "UP" if netstat.returncode == 0 else 'DOWN'
def repo_online():
cmd = "pkg -vv | grep -B 1 'enabled.*yes' | grep url"
raw_url = Popen(
cmd,
shell=True,
stdout=PIPE,
close_fds=True,
universal_newlines=True,
encoding='utf-8'
)
server = list(filter(None, raw_url.stdout.read().split('/')))[1]
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.connect((server, 80))
except OSError:
return False
else:
s.close()
return True
def repository_is_syncing():
raw_url = Popen(
'pkg -vv | grep -B 1 "enabled.*yes" | grep url',
shell=True,
stdout=PIPE,
close_fds=True,
universal_newlines=True,
encoding='utf-8'
)
pkg_url = raw_url.stdout.read().strip().split('"')[1]
syncing_url = f'{pkg_url}/.syncing'
return True if requests.get(syncing_url).status_code == 200 else False
def sync_with_repository():
cmd = "yes | pkg update -f"
pkg_out = run(
cmd,
shell=True,
stdout=PIPE,
universal_newlines=True,
encoding='utf-8'
)
if pkg_out.returncode == 0:
if 'Newer FreeBSD version' in pkg_out.stdout:
return 'UPGRADE'
return 'SYNCED'
else:
return 'FAILLED'
def start_update_station():
cmd = "update-station check-now"
update_station = Popen(cmd, shell=True)
return True if update_station.returncode == 0 else False
def available_package_origin():
cmd = "pkg rquery -U '%o' | cut -d '/' -f1"
pkg_out = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
lst = list(set(pkg_out.stdout.read().splitlines()))
lst.sort()
return lst
# The pkg descriptions contain new lines, :, ::, etc so the delimiters
# <EOS> (end of section) and <EOL> (end of line) avoid collisions. These new
# lines also prevent the use of .splitlines, thus the normal split method.
def available_package_list():
cmd = "pkg rquery -U '%o<EOS>%n<EOS>%v<EOS>%sh<EOS>%c<EOS>%e<EOL>'"
pkg_out = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
lst = list(filter(None, set(pkg_out.stdout.read().split('<EOL>\n'))))
lst.sort()
return lst
def installed_package_origin():
cmd = "pkg query '%o' | cut -d '/' -f1"
pkg_out = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
lst = list(set(pkg_out.stdout.read().splitlines()))
lst.sort()
return lst
def installed_package_list():
cmd = "pkg query '%o<EOS>%n<EOS>%v<EOS>%sh<EOS>%c<EOS>%e<EOL>'"
pkg_out = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
lst = list(filter(None, set(pkg_out.stdout.read().split('<EOL>\n'))))
lst.sort()
return lst
def available_package_dictionary(origin_list):
pkg_list = available_package_list()
installed_pkg_list = installed_package_list()
avail = str(len(pkg_list))
pkg_dict = {'avail': avail, 'all': {}}
for origin in origin_list:
pkg_dict[origin] = {}
for pkg in pkg_list:
if pkg in installed_pkg_list:
boolean = True
else:
boolean = False
pi = pkg.split('<EOS>')
pl = pi[0].split('/')
pkg_info = {
'origin': pi[0],
'name': pi[1],
'version': pi[2],
'size': pi[3],
'comment': pi[4],
'description': pi[5],
'installed': boolean
}
pkg_dict[pl[0]].update({pi[1]: pkg_info})
pkg_dict['all'].update({pi[1]: pkg_info})
return pkg_dict
def installed_package_dictionary(origin_list):
pkg_list = installed_package_list()
avail = str(len(pkg_list))
pkg_dict = {'avail': avail, 'all': {}}
for origin in origin_list:
pkg_dict[origin] = {}
for pkg in pkg_list:
pi = pkg.split('<EOS>')
pl = pi[0].split('/')
pkg_info = {
'origin': pi[0],
'name': pi[1],
'version': pi[2],
'size': pi[3],
'comment': pi[4],
'description': pi[5],
'installed': True
}
pkg_dict[pl[0]].update({pi[1]: pkg_info})
pkg_dict['all'].update({pi[1]: pkg_info})
return pkg_dict
def search_packages(search, descriptions=False):
D_search = '-D ' if descriptions is True else ''
cmd = f"pkg search -U {D_search}-Q name {search} | grep 'Name ' " \
"| cut -d : -f2 | cut -d ' ' -f2"
output = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
lst = output.stdout.read().splitlines()
return lst
def delete_packages(pkg):
cmd = f"pkg delete -y {pkg}"
fetch = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
return fetch.stdout
def fetch_packages(pkg):
cmd = f"pkg fetch -y {pkg}"
fetch = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
return fetch.stdout
def install_packages(pkg):
cmd = f"pkg install -y {pkg}"
fetch = Popen(cmd, shell=True, stdout=PIPE, close_fds=True,
universal_newlines=True, encoding='utf-8')
return fetch.stdout
def get_pkg_to_install_output(packages):
pkg_upgrade = Popen(
f'pkg install -n {packages}',
shell=True,
stdout=PIPE,
close_fds=True,
universal_newlines=True,
encoding='utf-8'
)
return pkg_upgrade.stdout.read()
def get_pkg_to_remove_output(packages):
pkg_upgrade = Popen(
f'pkg delete -n {packages}',
shell=True,
stdout=PIPE,
close_fds=True,
universal_newlines=True,
encoding='utf-8'
)
return pkg_upgrade.stdout.read()
def get_pkg_changes_data(remove_list, install_list):
if install_list:
install_pkg = get_pkg_to_install_output(' '.join(install_list))
install_pkg_list = install_pkg.splitlines()
else:
install_pkg = 'None'
install_pkg_list = []
if remove_list:
remove_pkg = get_pkg_to_remove_output(' '.join(remove_list))
remove_pkg_list = remove_pkg.splitlines()
else:
remove_pkg = 'None'
remove_pkg_list = []
pkg_to_remove = []
pkg_to_upgrade = []
pkg_to_install = []
pkg_to_reinstall = []
stop = False
if 'REMOVED:' in remove_pkg:
for line in remove_pkg_list:
if 'REMOVED:' in line:
stop = True
elif stop is True and line == '':
stop = False
break
elif stop is True:
pkg_to_remove.append(line.strip())
if 'UPGRADED:' in install_pkg:
for line in install_pkg_list:
if 'UPGRADED:' in line:
stop = True
elif stop is True and line == '':
stop = False
break
elif stop is True:
pkg_to_upgrade.append(line.strip())
if ' INSTALLED:' in install_pkg:
for line in install_pkg_list:
if ' INSTALLED:' in line:
stop = True
elif stop is True and line == '':
stop = False
break
elif stop is True:
pkg_to_install.append(line.strip())
if 'REINSTALLED:' in install_pkg:
for line in install_pkg_list:
if 'REINSTALLED:' in line:
stop = True
elif stop is True and line == '':
stop = False
break
elif stop is True:
pkg_to_reinstall.append(line.strip())
pkg_dictionaire = {
'remove': pkg_to_remove,
'upgrade': pkg_to_upgrade,
'install': pkg_to_install,
'reinstall': pkg_to_reinstall
}
return pkg_dictionaire