From 49f1ca76d29af8ecd352b1ac733b2ca946ea382e Mon Sep 17 00:00:00 2001 From: askiiart Date: Wed, 14 Dec 2022 22:23:07 -0600 Subject: [PATCH] Move stuff to a separate file, & fix big bugs --- basic_management.py | 21 +++++++++++--------- docker_parser.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 docker_parser.py diff --git a/basic_management.py b/basic_management.py index 68e9b0b..d8bdacb 100644 --- a/basic_management.py +++ b/basic_management.py @@ -10,7 +10,7 @@ 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')] + header = raw_info[:raw_info.find('\n')+1] header_temp = header header_indices = {} while ' ' in header_temp: # Split header @@ -28,21 +28,24 @@ def docker_info(): containers.append(line.strip()[line.strip().rfind(' ')+1:]) # Fill in info - for container in containers: - info[container] = {} + for i in range(len(containers)): + info[containers[i]] = {} for column in header_indices: end_i = header_indices[column] + len(column) - info[container][column] = header[header_indices[column]:end_i] + info[containers[i]][column] = raw_info[i][header_indices[column]:end_i] debug = True if debug: - print('Raw info:\n', raw_info) - print('Header:\n', header) - print('Header indices:\n', header_indices) - print('Containers:\n', containers) - print('Info:\n', info) + 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') return info +if '\n' not in getoutput('docker ps'): + print('No containers running') + exit() docker_info() diff --git a/docker_parser.py b/docker_parser.py new file mode 100644 index 0000000..b50c0dd --- /dev/null +++ b/docker_parser.py @@ -0,0 +1,48 @@ +from subprocess import getoutput +import pprint + +class NoContainersError(Exception): + pass + +class DockerParser: + def containers_info(raw_info=getoutput('docker container list')): + """ + Gets info about all the Docker containers + :return: Nested dict of containers info + """ + if '\n' not in raw_info: + raise(NoContainersError('A Docker container is required to run this program. Please create a docker container and try again.')) + 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 + + raw_info = raw_info[raw_info.find('\n')+1:] # Remove header (example above) + info = {} + + # Find container names + containers = [] + 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] + + return info + + +if __name__ == '__main__': + try: + pprint.pprint(DockerParser.containers_info()) + except NoContainersError as e: + print(e)