Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env python3
"""Example: Show all containers created since midnight."""
from datetime import datetime, time, timezone
import podman
print('{}\n'.format(__doc__))
midnight = datetime.combine(datetime.today(), time.min, tzinfo=timezone.utc)
with podman.Client() as client:
for c in client.containers.list():
created_at = podman.datetime_parse(c.createdat)
if created_at > midnight:
print('{}: image: {} createdAt: {}'.format(
c.id[:12], c.image[:32], podman.datetime_format(created_at)))
with open(path, "w") as stream:
stream.write("FROM alpine")
builder = self.pclient.images.build(
containerfiles=[path], tags=["alpine-unittest"]
)
self.assertIsNotNone(builder)
for line, img in builder():
# drain the generator...
continue
self.assertIsNotNone(img)
self.assertIn("localhost/alpine-unittest:latest", img.repoTags)
self.assertLess(
podman.datetime_parse(img.created), datetime.now(timezone.utc)
)
def list(self):
"""List images."""
images = sorted(
self.client.images.list(),
key=operator.attrgetter(self._args.sort))
if not images:
return
rows = list()
for image in images:
fields = dict(image)
fields.update({
'created':
humanize.naturaldate(podman.datetime_parse(image.created)),
'size':
humanize.naturalsize(int(image.size)),
'repoDigests':
' '.join(image.repoDigests),
})
for r in image.repoTags:
name, tag = r.split(':', 1)
fields.update({
'name': name,
'tag': tag,
})
rows.append(fields)
if not self._args.digests:
del self.columns['repoDigests']
def list(self):
"""List images."""
images = sorted(
self.client.images.list(),
key=operator.attrgetter(self._args.sort))
if not images:
return
rows = list()
for image in images:
fields = dict(image)
fields.update({
'created':
humanize.naturaldate(podman.datetime_parse(image.created)),
'size':
humanize.naturalsize(int(image.size)),
'repoDigests':
' '.join(image.repoDigests),
})
for r in image.repoTags:
name, tag = r.rsplit(':', 1)
fields.update({
'name': name,
'tag': tag,
})
rows.append(fields)
if not self._args.digests:
del self.columns['repoDigests']
lambda c: podman.FoldedString(c['status']) == 'running',
self.client.containers.list())
# TODO: Verify sorting on dates and size
ctnrs = sorted(ictnrs, key=operator.attrgetter(self._args.sort))
if not ctnrs:
return
rows = list()
for ctnr in ctnrs:
fields = dict(ctnr)
fields.update({
'command':
' '.join(ctnr.command),
'createdat':
humanize.naturaldate(podman.datetime_parse(ctnr.createdat)),
})
rows.append(fields)
with Report(self.columns, heading=self._args.heading) as report:
report.layout(
rows, self.columns.keys(), truncate=self._args.truncate)
for row in rows:
report.row(**row)
def history(self):
"""Report image history."""
rows = list()
for ident in self._args.image:
for details in self.client.images.get(ident).history():
fields = dict(details._asdict())
if self._args.human:
fields.update({
'size':
humanize.naturalsize(details.size),
'created':
humanize.naturaldate(
podman.datetime_parse(details.created)),
})
del fields['tags']
rows.append(fields)
if self._args.quiet:
for row in rows:
ident = row['id'][:12] if self._args.truncate else row['id']
print(ident)
elif self._args.format == 'json':
print(json.dumps(rows, indent=2), flush=True)
else:
with Report(self.columns, heading=self._args.heading) as report:
report.layout(
rows, self.columns.keys(), truncate=self._args.truncate)
for row in rows: