From 361ab97cc4c99db37f977725dd4036278fc8c4fa Mon Sep 17 00:00:00 2001
From: Philip Kamenarsky
Date: Wed, 23 Mar 2016 16:55:39 +0200
Subject: [PATCH] Add optional compression with upx
---
dockerize/dockerize.py | 30 +++++++++++++++++++++++++++---
dockerize/main.py | 7 ++++++-
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/dockerize/dockerize.py b/dockerize/dockerize.py
index 17fb0ad..afff915 100644
--- a/dockerize/dockerize.py
+++ b/dockerize/dockerize.py
@@ -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.'''
@@ -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
@@ -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:
@@ -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.'''
@@ -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']
diff --git a/dockerize/main.py b/dockerize/main.py
index 330f56d..3014db2 100644
--- a/dockerize/main.py
+++ b/dockerize/main.py
@@ -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',
@@ -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)