diff options
| author | Bruno ReniƩ <brutasse@gmail.com> | 2014-07-09 10:17:31 +0200 |
|---|---|---|
| committer | Bruno ReniƩ <brutasse@gmail.com> | 2014-07-23 15:05:53 +0200 |
| commit | 87b4d327d13dae20dcaaff9047585186360cff0e (patch) | |
| tree | 0dff25b4c3cb625c13761ddee41ae5f8fadb687b /docker/utils | |
| parent | 9170219188103022c06afd9c0caebf6aeea7eb5c (diff) | |
| download | docker-py-87b4d327d13dae20dcaaff9047585186360cff0e.tar.gz | |
Add support for .dockerignore
Fixes #265.
Implementation is a bit more elaborate than docker's implementation and
matches with the one proposed in dotcloud/docker#6869 to handle permission
issues more nicely.
Diffstat (limited to 'docker/utils')
| -rw-r--r-- | docker/utils/utils.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py index 036a504..be99f14 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -13,9 +13,11 @@ # limitations under the License. import io +import os import tarfile import tempfile from distutils.version import StrictVersion +from fnmatch import fnmatch import requests import six @@ -42,10 +44,29 @@ def mkbuildcontext(dockerfile): return f -def tar(path): +def fnmatch_any(relpath, patterns): + return any([fnmatch(relpath, pattern) for pattern in patterns]) + + +def tar(path, exclude=None): f = tempfile.NamedTemporaryFile() t = tarfile.open(mode='w', fileobj=f) - t.add(path, arcname='.') + for dirpath, dirnames, filenames in os.walk(path): + relpath = os.path.relpath(dirpath, path) + if relpath == '.': + relpath = '' + if exclude is None: + fnames = filenames + else: + dirnames[:] = [d for d in dirnames + if not fnmatch_any(os.path.join(relpath, d), + exclude)] + fnames = [name for name in filenames + if not fnmatch_any(os.path.join(relpath, name), + exclude)] + for name in fnames: + arcname = os.path.join(relpath, name) + t.add(os.path.join(path, arcname), arcname=arcname) t.close() f.seek(0) return f |
