Skip to content

Commit 5f741a1

Browse files
authored
Fixed More Linting Errors
1 parent 6a239ad commit 5f741a1

File tree

1 file changed

+17
-25
lines changed
  • old_docker_images_auto_delete

1 file changed

+17
-25
lines changed

old_docker_images_auto_delete/main.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"""
33
Rules:
44
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.
79
"""
810

911
import subprocess as sp
@@ -20,36 +22,31 @@ def __init__(self):
2022
self.img_list = []
2123
self.hash_list = []
2224

23-
2425
def get_all(self):
2526
"""
2627
Storing All the Docker Image Details Found on the System to a File
2728
"""
28-
file = open("temp.txt","r+" , encoding='utf-8')
29+
file = open("temp.txt", "r+", encoding='utf-8')
2930
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
3232
file.close()
3333

34-
35-
36-
def is_excluded(self , tag):
34+
def is_excluded(self, tag):
3735
"""
3836
Add other exceptions here
3937
"""
40-
#Excluding all the images with Alpine, Buster, Slim & Latest Tags
38+
# Excluding all the images with Alpine, Buster, Slim & Latest Tags
4139
reg = r"alpine|buster|slim|latest"
4240
flag = re.search(reg, tag)
4341
if flag is not None:
4442
return 0
4543
return 1
4644

47-
4845
def load_all(self):
4946
"""
5047
Loading data from the File to the Python program
5148
"""
52-
file = open("temp.txt", "r" , encoding='utf-8')
49+
file = open("temp.txt", "r", encoding='utf-8')
5350

5451
for line in file:
5552
line = line.rstrip("\n")
@@ -59,23 +56,21 @@ def load_all(self):
5956
regex = r"^(((\d+\.)?(\d+\.)?(\*|\d+)))(\-(dev|stage|prod))*$"
6057
match = re.search(regex, image[1])
6158

62-
img_dict = { 'Repository':image[0] , 'Tag': match.group(2) }
59+
img_dict = {'Repository': image[0], 'Tag': match.group(2)}
6360
self.img_list.append(img_dict)
6461
file.close()
6562

66-
6763
def man_data(self):
6864
"""
6965
Manipulating Data to perform the reqd operation
7066
"""
7167
key = operator.itemgetter('Repository')
7268
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
7470

7571
self.img_list.clear()
7672
self.img_list = b_key.copy()
7773

78-
7974
def sort_tag(self):
8075
"""
8176
Sorting Tags according to the Version Numbers
@@ -94,8 +89,8 @@ def sort_tag(self):
9489
for new, i in enumerate(img['Tag']):
9590
final_list.append(template_string.format(i))
9691

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
9994
self.hash_list.append(hash_map)
10095

10196
final_list.sort()
@@ -104,10 +99,9 @@ def sort_tag(self):
10499
img['Tag'].extend(final_list[:-1])
105100

106101
print(self.hash_list)
107-
print (self.img_list)
102+
print(self.img_list)
108103

109-
110-
def hash_function(self , tag):
104+
def hash_function(self, tag):
111105
"""
112106
Hash Function for Error Detection
113107
"""
@@ -119,7 +113,6 @@ def hash_function(self , tag):
119113
temp = 'Error in Manipulation'
120114
return temp
121115

122-
123116
def remove_image(self):
124117
"""
125118
Running the Docker RMI Command to Delete the Older Versions
@@ -129,9 +122,8 @@ def remove_image(self):
129122
for tag in img['Tag']:
130123
val = self.hash_function(tag)
131124
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
135127

136128

137129
# Main Function

0 commit comments

Comments
 (0)