Selaa lähdekoodia

Make Pylint mostly happy

master
Ricardo Branco 5 vuotta sitten
vanhempi
commit
6524291e88
1 muutettua tiedostoa jossa 49 lisäystä ja 14 poistoa
  1. 49
    14
      clean_registry.py

+ 49
- 14
clean_registry.py Näytä tiedosto

36
 
36
 
37
 VERSION = "1.3.1"
37
 VERSION = "1.3.1"
38
 REGISTRY_DIR = "REGISTRY_STORAGE_FILESYSTEM_ROOTREGISTRY_DIR"
38
 REGISTRY_DIR = "REGISTRY_STORAGE_FILESYSTEM_ROOTREGISTRY_DIR"
39
+args = None
39
 
40
 
40
 
41
 
41
 def dockerized():
42
 def dockerized():
59
 
60
 
60
 def clean_revisions(repo):
61
 def clean_revisions(repo):
61
     '''Remove the revision manifests that are not present in the tags directory'''
62
     '''Remove the revision manifests that are not present in the tags directory'''
62
-    revisions = set(os.listdir(repo + "/_manifests/revisions/sha256/"))
63
-    manifests = set(map(os.path.basename, iglob(repo + "/_manifests/tags/*/*/sha256/*")))
63
+    revisions = set(
64
+        os.listdir(repo + "/_manifests/revisions/sha256/")
65
+    )
66
+    manifests = set(
67
+        map(os.path.basename, iglob(repo + "/_manifests/tags/*/*/sha256/*"))
68
+    )
64
     revisions.difference_update(manifests)
69
     revisions.difference_update(manifests)
65
     for revision in revisions:
70
     for revision in revisions:
66
         remove(repo + "/_manifests/revisions/sha256/" + revision)
71
         remove(repo + "/_manifests/revisions/sha256/" + revision)
133
     # Note: Internally, distribution permits multiple dashes and up to 2 underscores as separators.
138
     # Note: Internally, distribution permits multiple dashes and up to 2 underscores as separators.
134
     # See https://github.com/docker/distribution/blob/master/reference/regexp.go
139
     # See https://github.com/docker/distribution/blob/master/reference/regexp.go
135
 
140
 
136
-    return len(image) < 256 and len(tag) < 129 and re.match('[a-zA-Z0-9_][a-zA-Z0-9_.-]*$', tag) and \
137
-           all(re.match('[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*$', path) for path in repo.split("/"))
141
+    return len(image) < 256 and len(tag) < 129 and \
142
+        re.match('[a-zA-Z0-9_][a-zA-Z0-9_.-]*$', tag) and \
143
+        all(
144
+            re.match('[a-z0-9]+(?:(?:[._]|__|[-]*)[a-z0-9]+)*$', path)
145
+            for path in repo.split("/")
146
+        )
138
 
147
 
139
 
148
 
140
 class RegistryCleaner():
149
 class RegistryCleaner():
185
         if self.container is not None:
194
         if self.container is not None:
186
             self.docker.api.stop(self.container)
195
             self.docker.api.stop(self.container)
187
 
196
 
188
-        images = args.images or map(os.path.dirname, iglob("**/_manifests", recursive=True))
197
+        images = args.images or \
198
+            map(os.path.dirname, iglob("**/_manifests", recursive=True))
189
 
199
 
190
         exit_status = 0
200
         exit_status = 0
191
         for image in images:
201
         for image in images:
204
     def get_file(self, path):
214
     def get_file(self, path):
205
         '''Returns the contents of the specified file from the container'''
215
         '''Returns the contents of the specified file from the container'''
206
         try:
216
         try:
207
-            with BytesIO(
208
-                b"".join(_ for _ in self.docker.api.get_archive(self.container, path)[0])
209
-            ) as buf, tarfile.open(fileobj=buf) as tar, tar.extractfile(os.path.basename(path)) as infile:
217
+            with BytesIO(b"".join(
218
+                    _ for _ in self.docker.api.get_archive(self.container, path)[0]
219
+            )) as buf, tarfile.open(fileobj=buf) \
220
+                    as tar, tar.extractfile(os.path.basename(path)) \
221
+                    as infile:
210
                 data = infile.read()
222
                 data = infile.read()
211
         except NotFound as err:
223
         except NotFound as err:
212
             error(err)
224
             error(err)
230
             try:
242
             try:
231
                 registry_dir = data['storage']['filesystem']['rootdirectory']
243
                 registry_dir = data['storage']['filesystem']['rootdirectory']
232
             except KeyError:
244
             except KeyError:
233
-                driver = [k for k in 'azure gcs inmemory oss s3 swift'.split() if k in data['storage']][0]
245
+                driver = [
246
+                    _ for _ in 'azure gcs inmemory oss s3 swift'.split()
247
+                    if _ in data['storage']
248
+                ][0]
234
                 error("Unsupported storage driver: " + driver)
249
                 error("Unsupported storage driver: " + driver)
235
 
250
 
236
         if dockerized():
251
         if dockerized():
239
         for item in self.info['Mounts']:
254
         for item in self.info['Mounts']:
240
             if item['Destination'] == registry_dir:
255
             if item['Destination'] == registry_dir:
241
                 return item['Source']
256
                 return item['Source']
257
+        return None
242
 
258
 
243
     def get_image_version(self):
259
     def get_image_version(self):
244
         '''Gets the Docker distribution version running on the container'''
260
         '''Gets the Docker distribution version running on the container'''
245
         if self.info['State']['Running']:
261
         if self.info['State']['Running']:
246
-            data = self.docker.containers.get(self.container).exec_run("/bin/registry --version").output
262
+            data = self.docker.containers.get(
263
+                self.container
264
+            ).exec_run("/bin/registry --version").output
247
         else:
265
         else:
248
-            data = self.docker.containers.run(self.info["Image"], command="--version", remove=True)
266
+            data = self.docker.containers.run(
267
+                self.info["Image"], command="--version", remove=True
268
+            )
249
         return data.decode('utf-8').split()[2]
269
         return data.decode('utf-8').split()[2]
250
 
270
 
251
     def garbage_collect(self):
271
     def garbage_collect(self):
253
         command = "garbage-collect " + "/etc/docker/registry/config.yml"
273
         command = "garbage-collect " + "/etc/docker/registry/config.yml"
254
         if dockerized():
274
         if dockerized():
255
             command = "/bin/registry " + command
275
             command = "/bin/registry " + command
256
-            with subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
276
+            with subprocess.Popen(
277
+                    command.split(),
278
+                    stdout=subprocess.PIPE,
279
+                    stderr=subprocess.STDOUT
280
+            ) as proc:
257
                 if not args.quiet:
281
                 if not args.quiet:
258
                     print(proc.stdout.read().decode('utf-8'))
282
                     print(proc.stdout.read().decode('utf-8'))
259
             status = proc.wait()
283
             status = proc.wait()
260
         else:
284
         else:
261
-            cli = self.docker.containers.run("registry:2", command=command, detach=True, stderr=True,
262
-                                             volumes={self.registry_dir: {'bind': "/var/lib/registry", 'mode': "rw"}})
285
+            cli = self.docker.containers.run(
286
+                "registry:2",
287
+                command=command,
288
+                detach=True,
289
+                stderr=True,
290
+                volumes={
291
+                    self.registry_dir: {
292
+                        'bind': "/var/lib/registry",
293
+                        'mode': "rw"
294
+                    }
295
+                }
296
+            )
263
             if not args.quiet:
297
             if not args.quiet:
264
                 for line in cli.logs(stream=True):
298
                 for line in cli.logs(stream=True):
265
                     print(line.decode('utf-8'), end="")
299
                     print(line.decode('utf-8'), end="")
313
 
347
 
314
     sys.exit(cleaner())
348
     sys.exit(cleaner())
315
 
349
 
350
+
316
 if __name__ == "__main__":
351
 if __name__ == "__main__":
317
     try:
352
     try:
318
         main()
353
         main()

Loading…
Peruuta
Tallenna