From 4adebb0adc6caeedeef1d6f051f9198aa3a952a7 Mon Sep 17 00:00:00 2001 From: --global <--global> Date: Thu, 15 Dec 2022 09:05:40 -0600 Subject: [PATCH] Finally fix and finish docker_info() --- basic_management.py | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/basic_management.py b/basic_management.py index d8bdacb..c4f2c87 100644 --- a/basic_management.py +++ b/basic_management.py @@ -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