Skip to content

Commit

Permalink
Merge pull request #27 from avirshup/output
Browse files Browse the repository at this point in the history
Colored output
  • Loading branch information
avirshup authored Sep 23, 2017
2 parents ff60dfa + 06d398e commit 9a0b73c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
47 changes: 32 additions & 15 deletions dockermake/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import os
from builtins import object
from termcolor import cprint

from dockermake.step import FileCopyStep
from . import utils
Expand Down Expand Up @@ -51,15 +52,11 @@ def write_dockerfile(self, output_dir):
lines.extend(step.dockerfile_lines)
else:
lines.extend(step.dockerfile_lines[1:])

path = os.path.join(output_dir, 'Dockerfile.%s' % self.imagename)

with open(path, 'w') as outfile:
outfile.write('\n'.join(lines))

print('Wrote %s' % path)


def build(self, client,
nobuild=False,
keepbuildtags=False,
Expand All @@ -80,14 +77,18 @@ def build(self, client,
usecache=usecache,
pull=pull)

print('\n' + '-'*utils.get_console_width())
print(' STARTING BUILD for "%s" (image definition "%s" from %s)\n' % (
self.targetname, self.imagename, self.steps[-1].sourcefile))
width = utils.get_console_width()
cprint('\n' + '='*width,
color='white', attrs=['bold'])

line = 'STARTING BUILD for "%s" (image definition "%s" from %s)\n' % (
self.targetname, self.imagename, self.steps[-1].sourcefile)

cprint(_centered(line, width), color='blue', attrs=['bold'])

for istep, step in enumerate(self.steps):
print(' * Building %s, Step %d/%d:' % (self.imagename,
istep+1,
len(self.steps)))
cprint('* Building image "%s", Step %d/%d:' % (self.imagename, istep+1, len(self.steps)),
color='blue')

if not nobuild:
if step.bust_cache:
Expand All @@ -96,7 +97,8 @@ def build(self, client,
step.bust_cache = False

step.build(client, usecache=usecache)
print(" - Created intermediate image %s\n" % step.buildname)
cprint("* Created intermediate image \"%s\"\n" % step.buildname,
'green')

if step.bust_cache:
_rebuilt.add(stackkey)
Expand All @@ -105,7 +107,11 @@ def build(self, client,

if not nobuild:
self.finalizenames(client, finalimage, keepbuildtags)
print(' *** Successfully built image %s\n' % self.targetname)
line = 'FINISHED BUILDING "%s" (image definition "%s" from %s)'%(
self.targetname, self.imagename, self.steps[-1].sourcefile)
cprint(_centered(line, width),
color='green', attrs=['bold'])
cprint('=' * width, color='white', attrs=['bold'], end='\n\n')

def _get_stack_key(self, istep):
names = [self.from_image]
Expand All @@ -120,20 +126,31 @@ def update_source_images(self, client, usecache, pull):
for build in self.sourcebuilds:
if build.targetname in _updated_staging_images:
continue
print('\nUpdating source image %s' % build.targetname)
cprint('\nUpdating source image %s' % build.targetname,
'blue')
build.build(client,
usecache=usecache,
pull=pull)
print(' *** Done with source image %s\n' % build.targetname)
cprint('Finished with build image "%s"\n' % build.targetname,
color='green')

def finalizenames(self, client, finalimage, keepbuildtags):
""" Tag the built image with its final name and untag intermediate containers
"""
client.tag(finalimage, *self.targetname.split(':'))
print('Tagged final image as %s' % self.targetname)
cprint('Tagged final image as "%s"' % self.targetname,
'green')
if not keepbuildtags:
print('Untagging intermediate containers:', end='')
for step in self.steps:
client.remove_image(step.buildname, force=True)
print(step.buildname, end=',')
print()


def _centered(s, w):
leftover = w - len(s)
if leftover < 0:
return s
else:
return ' '*(leftover//2) + s
12 changes: 7 additions & 5 deletions dockermake/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import docker.errors
from builtins import object
from termcolor import cprint

import os
import tempfile
Expand All @@ -33,10 +34,10 @@ def clear_copy_cache():
for path in (BUILD_CACHEDIR, BUILD_TEMPDIR):
if os.path.exists(path):
assert os.path.isdir(path), "'%s' is not a directory!"
print('Removing docker-make cache %s' % path)
cprint('Removing docker-make cache %s' % path, 'yellow')
shutil.rmtree(path)
else:
print('Cache directory %s does not exist.' % path)
cprint('Cache directory %s does not exist.' % path, 'red')


class StagedFile(object):
Expand Down Expand Up @@ -64,8 +65,9 @@ def stage(self, startimage, newimage):
from .step import BuildError

client = utils.get_client()
print(' * Copying FROM "%s:/%s" TO "%s://%s/"'%(self.sourceimage, self.sourcepath,
startimage, self.destpath))
cprint(' Copying file from "%s:/%s" \n to "%s://%s/"'
% (self.sourceimage, self.sourcepath, startimage, self.destpath),
'blue')

# copy build artifacts from the container if necessary
cachedir = self._setcache(client)
Expand Down Expand Up @@ -93,7 +95,7 @@ def stage(self, startimage, newimage):
os.mkdir(cachedir)
os.rename(tempdir, cachedir)
else:
print(' * Using cached files from %s' % cacherelpath)
print(' Using cached files from %s' % cacherelpath)

# write Dockerfile for the new image and then build it
dockerfile = 'FROM %s\nADD content.tar %s' % (startimage, self.destpath)
Expand Down
3 changes: 2 additions & 1 deletion dockermake/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def write_dockerfile(self, dockerfile):
def build_external_dockerfile(client, image):
import docker.errors
cprint(" Building base image from %s" % image, 'blue')
assert not image.built

stream = client.build(path=os.path.dirname(image.path),
dockerfile=os.path.basename(image.path),
Expand All @@ -136,7 +137,7 @@ def build_external_dockerfile(client, image):
raise errors.ExternalBuildError(
'Error building Dockerfile at %s. ' % image.path +
'Please check it for errors\n. Docker API error message:' + str(e))

image.built = True
cprint(" Finished building Dockerfile at %s" % image.path, 'green')


Expand Down

0 comments on commit 9a0b73c

Please sign in to comment.