Skip to content
Open
Show file tree
Hide file tree
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
30 changes: 27 additions & 3 deletions dockerize/dockerize.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def add_group(self, group):
grent = grp.getgrnam(group)
self.groups.append(':'.join(str(x) for x in grent))

def add_file(self, src, dst=None):
def add_file(self, src, dst=None, upx=False):
'''Add a file to the list of files that will be installed into the
image.'''

Expand All @@ -97,7 +97,7 @@ def add_file(self, src, dst=None):
raise ValueError('%s: container paths must be fully '
'qualified' % dst)

self.paths.add((src, dst))
self.paths.add((src, dst, upx))

def build(self):
'''Call this method to produce a Docker image. It will either
Expand All @@ -120,6 +120,7 @@ def build(self):

self.copy_files()
self.resolve_deps()
self.compress_files()
self.populate()
self.generate_dockerfile()
if self._build_image:
Expand Down Expand Up @@ -187,6 +188,20 @@ def copy_file(self, src, dst=None, symlinks=None):
LOG.info('running: %s', cmd)
subprocess.check_call(cmd)

def compress_file(self, src, dst=None):
'''Compress a file in the image using "upx".'''

if dst is None:
dst = src

LOG.info('compressing file %s', dst)
target = os.path.join(self.targetdir, dst[1:])

cmd = ['upx', target]

LOG.info('running: %s', cmd)
subprocess.check_call(cmd)

def resolve_deps(self):
'''Uses the dockerize.depsolver.DepSolver class to find all the shared
library dependencies of files installed into the Docker image.'''
Expand Down Expand Up @@ -217,10 +232,19 @@ def copy_files(self):
'''Process the list of paths generated via add_file and copy items
into the image.'''

for src, dst in self.paths:
for src, dst, _ in self.paths:
for srcitem in glob.iglob(src):
self.copy_file(srcitem, dst)

def compress_files(self):
'''Process the list of paths generated via add_file and compress items
in the image.'''

for src, dst, upx in self.paths:
if upx:
for srcitem in glob.iglob(src):
self.compress_file(srcitem, dst)

def build_image(self):
LOG.info('building Docker image')
cmd = ['docker', 'build']
Expand Down
7 changes: 6 additions & 1 deletion dockerize/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def parse_args():
action='store_true',
help='Add common file manipulation tools')

parser.add_argument('--upx',
action='store_true',
default=False,
help='Compress paths with upx')

group = parser.add_argument_group('Logging options')
group.add_argument('--verbose',
action='store_const',
Expand Down Expand Up @@ -107,7 +112,7 @@ def main():
symlinks=args.symlinks)

for path in args.paths:
app.add_file(path)
app.add_file(path, upx=args.upx)

for src, dst in args.add_file:
app.add_file(src, dst)
Expand Down