2
2
"""
3
3
Rules:
4
4
1. All images with 'latest' tag would not be touched.
5
- 2. For a particular repository, the tag with the highest number would be preserved.
6
- 3. A provision is made to add exception images which would be never stopped.
5
+ 2. For a particular repository, the tag with
6
+ the highest number would be preserved.
7
+ 3. A provision is made to add exception images
8
+ which would be never stopped.
7
9
"""
8
10
9
11
import subprocess as sp
@@ -20,36 +22,31 @@ def __init__(self):
20
22
self .img_list = []
21
23
self .hash_list = []
22
24
23
-
24
25
def get_all (self ):
25
26
"""
26
27
Storing All the Docker Image Details Found on the System to a File
27
28
"""
28
- file = open ("temp.txt" ,"r+" , encoding = 'utf-8' )
29
+ file = open ("temp.txt" , "r+" , encoding = 'utf-8' )
29
30
file .truncate (0 )
30
- (sp .run ("sudo docker image list --format '{{.Repository}}~{{.Tag}}' >> temp.txt" ,
31
- shell = True , capture_output = True , check = True ))
31
+ sp .run ("sudo docker image list --format '{{.Repository}}~{{.Tag}}' >> temp.txt" ,shell = True , capture_output = True , check = True ) # noqa
32
32
file .close ()
33
33
34
-
35
-
36
- def is_excluded (self , tag ):
34
+ def is_excluded (self , tag ):
37
35
"""
38
36
Add other exceptions here
39
37
"""
40
- #Excluding all the images with Alpine, Buster, Slim & Latest Tags
38
+ # Excluding all the images with Alpine, Buster, Slim & Latest Tags
41
39
reg = r"alpine|buster|slim|latest"
42
40
flag = re .search (reg , tag )
43
41
if flag is not None :
44
42
return 0
45
43
return 1
46
44
47
-
48
45
def load_all (self ):
49
46
"""
50
47
Loading data from the File to the Python program
51
48
"""
52
- file = open ("temp.txt" , "r" , encoding = 'utf-8' )
49
+ file = open ("temp.txt" , "r" , encoding = 'utf-8' )
53
50
54
51
for line in file :
55
52
line = line .rstrip ("\n " )
@@ -59,23 +56,21 @@ def load_all(self):
59
56
regex = r"^(((\d+\.)?(\d+\.)?(\*|\d+)))(\-(dev|stage|prod))*$"
60
57
match = re .search (regex , image [1 ])
61
58
62
- img_dict = { 'Repository' :image [0 ] , 'Tag' : match .group (2 ) }
59
+ img_dict = {'Repository' : image [0 ], 'Tag' : match .group (2 )}
63
60
self .img_list .append (img_dict )
64
61
file .close ()
65
62
66
-
67
63
def man_data (self ):
68
64
"""
69
65
Manipulating Data to perform the reqd operation
70
66
"""
71
67
key = operator .itemgetter ('Repository' )
72
68
b_key = [{'Repository' : x , 'Tag' : [d ['Tag' ] for d in y ]}
73
- for x , y in itertools .groupby (sorted (self .img_list , key = key ), key = key )]
69
+ for x , y in itertools .groupby (sorted (self .img_list , key = key ), key = key )] # noqa
74
70
75
71
self .img_list .clear ()
76
72
self .img_list = b_key .copy ()
77
73
78
-
79
74
def sort_tag (self ):
80
75
"""
81
76
Sorting Tags according to the Version Numbers
@@ -94,8 +89,8 @@ def sort_tag(self):
94
89
for new , i in enumerate (img ['Tag' ]):
95
90
final_list .append (template_string .format (i ))
96
91
97
- for i in range (0 , len (img ['Tag' ])):
98
- hash_map = { 'TagsManipulated' : final_list [i ] , 'TagsOriginal' : temp [i ] }
92
+ for i in range (0 , len (img ['Tag' ])):
93
+ hash_map = {'TagsManipulated' : final_list [i ], 'TagsOriginal' : temp [i ]} # noqa
99
94
self .hash_list .append (hash_map )
100
95
101
96
final_list .sort ()
@@ -104,10 +99,9 @@ def sort_tag(self):
104
99
img ['Tag' ].extend (final_list [:- 1 ])
105
100
106
101
print (self .hash_list )
107
- print (self .img_list )
102
+ print (self .img_list )
108
103
109
-
110
- def hash_function (self , tag ):
104
+ def hash_function (self , tag ):
111
105
"""
112
106
Hash Function for Error Detection
113
107
"""
@@ -119,7 +113,6 @@ def hash_function(self , tag):
119
113
temp = 'Error in Manipulation'
120
114
return temp
121
115
122
-
123
116
def remove_image (self ):
124
117
"""
125
118
Running the Docker RMI Command to Delete the Older Versions
@@ -129,9 +122,8 @@ def remove_image(self):
129
122
for tag in img ['Tag' ]:
130
123
val = self .hash_function (tag )
131
124
image_url = img ['Repository' ] + ":" + val
132
- print ("Deleting Image : " + image_url )
133
- (sp .run ("sudo docker rmi " + image_url ,
134
- shell = True ,capture_output = True , check = True ))
125
+ print ("Deleting Image : " + image_url )
126
+ sp .run ("sudo docker rmi " + image_url ,shell = True ,capture_output = True , check = True ) # noqa
135
127
136
128
137
129
# Main Function
0 commit comments