Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021-04-14 - Timestamp suggestion and correction for buildkit.dockerfile #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions dedockify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from sys import argv
import docker
from datetime import datetime


class ImageNotFound(Exception):
pass
Expand All @@ -12,7 +14,11 @@ def __init__(self):
super(MainObj, self).__init__()
self.commands = []
self.cli = docker.APIClient(base_url='unix://var/run/docker.sock')
self._get_image(argv[-1])
self._get_image(argv[1])
if len(argv) >= 3 and argv[2] == '--date': # check --date argument
self.created_date = True
else:
self.created_date = False
self.hist = self.cli.history(self.img['RepoTags'][0])
self._parse_history()
self.commands.reverse()
Expand All @@ -30,13 +36,20 @@ def _get_image(self, img_hash):
return
raise ImageNotFound("Image {} not found\n".format(img_hash))

def _insert_step(self, step):
def _insert_step(self, step, created, comment):
if "#(nop)" in step:
to_add = step.split("#(nop) ")[1]
elif "buildkit.dockerfile" in comment: # Images generated with buildkit.dockerfile do not have "# (nop)"
to_add = step
else:
to_add = ("RUN {}".format(step))
to_add = to_add.replace("&&", "\\\n &&")
self.commands.append(to_add.strip(' '))
if self.created_date: # Print a comment with Created_timestamp before command
Created_timestamp = created
Created_date_time = '\n# Created At: ' + str(datetime.fromtimestamp(Created_timestamp)) + '\n'
self.commands.append(Created_date_time + to_add.strip(' '))
else:
self.commands.append(to_add.strip(' '))

def _parse_history(self, rec=False):
first_tag = False
Expand All @@ -47,7 +60,7 @@ def _parse_history(self, rec=False):
if first_tag and not rec:
break
first_tag = True
self._insert_step(i['CreatedBy'])
self._insert_step(i['CreatedBy'], i['Created'], i['Comment'])
if not rec:
self.commands.append("FROM {}".format(actual_tag))

Expand Down