blob: 40f38eb119db0cf195fb5aa08e176d3b659dee6d [file] [log] [blame]
Ondřej Šmalec13aa60d2020-01-23 12:56:18 +01001#!/usr/bin/env python3
2import yaml
3import sys
4import argparse
5
6
7class ModifiedParser(argparse.ArgumentParser):
8 """
9 modified error handling to print help
10 """
11 def error(self, message):
12 sys.stderr.write('error: %s\n' % message)
13 self.print_help()
14 sys.exit(2)
15
16
17def create_blacklist(config_file):
18 """
19 Generate a list of images which needs to be excluded from docker_image_list
20 :param config_file: application_configuration file where images are.
21 :return:
22 """
23 with open(config_file, 'r') as f:
24 file = yaml.load(f, Loader=yaml.SafeLoader)
25
26 blacklist=[]
27 for name, _ in file['runtime_images'].items():
28 path = file['runtime_images'][name]['path']
29 blacklist.append(path[1:])
30 return blacklist
31
32
33def should_remove_line(line, blacklist):
34 """
35 Helping function to match image in blacklist
36 :param line: line in datalist file
37 :param blacklist: list of images to be removed
38 :return
39 """
40 return any([image in line for image in blacklist])
41
42
43def update_datalist(config_file, datalist):
44 """
45 remove local images from datalist.
46 :param config_file: application_configuration file where images are.
47 :param datalist: docker_image_list to be updated
48 :return:
49 """
50 blacklist = create_blacklist(config_file)
51 data = []
52 with open(datalist, 'r') as f:
53 for line in f:
54 if not should_remove_line(line, blacklist):
55 data.append(line)
56 with open(datalist, 'w') as f:
57 for line in data:
58 f.write(line)
59
60
61def run_cli():
62 """
63 Run as cli tool
64 """
65
66 parser = ModifiedParser(description='Remove runtime images from docker_image_list')
67
68 parser.add_argument('config_file',
69 help='application_configuration file where images are')
70 parser.add_argument('datalist',
71 help='docker_image_list from where images should be removed')
72 args = parser.parse_args()
73
74 update_datalist(args.config_file, args.datalist)
75
76
77if __name__ == '__main__':
78 run_cli()