Finally fix and finish docker_info()

This commit is contained in:
--global 2022-12-15 09:05:40 -06:00
parent 49f1ca76d2
commit 4adebb0adc

View file

@ -11,36 +11,31 @@ def docker_info():
raw_info = getoutput('docker ps')
# Header: "CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES" (with way more spaces)
header = raw_info[:raw_info.find('\n')+1]
header_temp = header
header_indices = {}
while ' ' in header_temp: # Split header
temp = header_temp[header_temp.rfind(' ')+2:].strip()
header_indices[temp] = header.find(temp)
header_temp = header_temp[:header_temp.rfind(' ')].strip()
header_indices[header[:header.find(' ')]] = 0
header_indices = {'CONTAINER ID': header.find('CONTAINER ID'), 'IMAGE': header.find('IMAGE'), 'COMMAND': header.find('COMMAND'), \
'CREATED': header.find('CREATED'), \
'STATUS': header.find('STATUS'), 'PORTS': header.find('PORTS'), 'NAMES': header.find('NAMES')}
raw_info = raw_info[raw_info.find('\n')+1:] # Remove header (example above)
# Remove header (example above)
raw_info = raw_info[raw_info.find('\n')+1:]
info = {}
# Find container names
containers = []
for line in raw_info:
for line in raw_info.split('\n'):
containers.append(line.strip()[line.strip().rfind(' ')+1:])
# Fill in info
for i in range(len(containers)):
info[containers[i]] = {}
for column in header_indices:
end_i = header_indices[column] + len(column)
info[containers[i]][column] = raw_info[i][header_indices[column]:end_i]
debug = True
if debug:
print('Raw info:\n', raw_info, '\n')
print('Header:\n', header, '\n')
print('Header indices:\n', header_indices, '\n')
print('Containers:\n', containers, '\n')
print('Info:\n', info, '\n')
header_indices_keys = list(header_indices)
for j in range(len(header_indices_keys)): # TODO: Fix
start_i = header_indices[header_indices_keys[j]]
if j+1 != len(header_indices):
end_i = header_indices[header_indices_keys[j+1]]
else:
end_i = len(header)
info[containers[i]][header_indices[header_indices_keys[j]]] = \
raw_info.split('\n')[i][start_i:end_i].strip()
return info