summaryrefslogtreecommitdiff
path: root/docker/utils/build.py
blob: b644c9fca174f6267ee303e9d7f89cdf87f64153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import io
import os
import re
import six
import tarfile
import tempfile

from ..constants import IS_WINDOWS_PLATFORM
from fnmatch import fnmatch
from itertools import chain


_SEP = re.compile('/|\\\\') if IS_WINDOWS_PLATFORM else re.compile('/')


def tar(path, exclude=None, dockerfile=None, fileobj=None, gzip=False):
    root = os.path.abspath(path)
    exclude = exclude or []
    dockerfile = dockerfile or (None, None)
    extra_files = []
    if dockerfile[1] is not None:
        dockerignore_contents = '\n'.join(
            (exclude or ['.dockerignore']) + [dockerfile[0]]
        )
        extra_files = [
            ('.dockerignore', dockerignore_contents),
            dockerfile,
        ]
    return create_archive(
        files=sorted(exclude_paths(root, exclude, dockerfile=dockerfile[0])),
        root=root, fileobj=fileobj, gzip=gzip, extra_files=extra_files
    )


def exclude_paths(root, patterns, dockerfile=None):
    """
    Given a root directory path and a list of .dockerignore patterns, return
    an iterator of all paths (both regular files and directories) in the root
    directory that do *not* match any of the patterns.

    All paths returned are relative to the root.
    """

    if dockerfile is None:
        dockerfile = 'Dockerfile'

    def split_path(p):
        return [pt for pt in re.split(_SEP, p) if pt and pt != '.']

    def normalize(p):
        # Leading and trailing slashes are not relevant. Yes,
        # "foo.py/" must exclude the "foo.py" regular file. "."
        # components are not relevant either, even if the whole
        # pattern is only ".", as the Docker reference states: "For
        # historical reasons, the pattern . is ignored."
        # ".." component must be cleared with the potential previous
        # component, regardless of whether it exists: "A preprocessing
        # step [...]  eliminates . and .. elements using Go's
        # filepath.".
        i = 0
        split = split_path(p)
        while i < len(split):
            if split[i] == '..':
                del split[i]
                if i > 0:
                    del split[i - 1]
                    i -= 1
            else:
                i += 1
        return split

    patterns = (
        (True, normalize(p[1:]))
        if p.startswith('!') else
        (False, normalize(p))
        for p in patterns)
    patterns = list(reversed(list(chain(
        # Exclude empty patterns such as "." or the empty string.
        filter(lambda p: p[1], patterns),
        # Always include the Dockerfile and .dockerignore
        [(True, split_path(dockerfile)), (True, ['.dockerignore'])]))))
    return set(walk(root, patterns))


def walk(root, patterns, default=True):
    """
    A collection of file lying below root that should be included according to
    patterns.
    """

    def match(p):
        if p[1][0] == '**':
            rec = (p[0], p[1][1:])
            return [p] + (match(rec) if rec[1] else [rec])
        elif fnmatch(f, p[1][0]):
            return [(p[0], p[1][1:])]
        else:
            return []

    for f in os.listdir(root):
        cur = os.path.join(root, f)
        # The patterns if recursing in that directory.
        sub = list(chain(*(match(p) for p in patterns)))
        # Whether this file is explicitely included / excluded.
        hit = next((p[0] for p in sub if not p[1]), None)
        # Whether this file is implicitely included / excluded.
        matched = default if hit is None else hit
        sub = list(filter(lambda p: p[1], sub))
        if os.path.isdir(cur) and not os.path.islink(cur):
            # Entirely skip directories if there are no chance any subfile will
            # be included.
            if all(not p[0] for p in sub) and not matched:
                continue
            # I think this would greatly speed up dockerignore handling by not
            # recursing into directories we are sure would be entirely
            # included, and only yielding the directory itself, which will be
            # recursively archived anyway. However the current unit test expect
            # the full list of subfiles and I'm not 100% sure it would make no
            # difference yet.
            # if all(p[0] for p in sub) and matched:
            #     yield f
            #     continue
            children = False
            for r in (os.path.join(f, p) for p in walk(cur, sub, matched)):
                yield r
                children = True
            # The current unit tests expect directories only under those
            # conditions. It might be simplifiable though.
            if (not sub or not children) and hit or hit is None and default:
                yield f
        elif matched:
            yield f


def build_file_list(root):
    files = []
    for dirname, dirnames, fnames in os.walk(root):
        for filename in fnames + dirnames:
            longpath = os.path.join(dirname, filename)
            files.append(
                longpath.replace(root, '', 1).lstrip('/')
            )

    return files


def create_archive(root, files=None, fileobj=None, gzip=False,
                   extra_files=None):
    extra_files = extra_files or []
    if not fileobj:
        fileobj = tempfile.NamedTemporaryFile()
    t = tarfile.open(mode='w:gz' if gzip else 'w', fileobj=fileobj)
    if files is None:
        files = build_file_list(root)
    extra_names = set(e[0] for e in extra_files)
    for path in files:
        if path in extra_names:
            # Extra files override context files with the same name
            continue
        full_path = os.path.join(root, path)

        i = t.gettarinfo(full_path, arcname=path)
        if i is None:
            # This happens when we encounter a socket file. We can safely
            # ignore it and proceed.
            continue

        # Workaround https://bugs.python.org/issue32713
        if i.mtime < 0 or i.mtime > 8**11 - 1:
            i.mtime = int(i.mtime)

        if IS_WINDOWS_PLATFORM:
            # Windows doesn't keep track of the execute bit, so we make files
            # and directories executable by default.
            i.mode = i.mode & 0o755 | 0o111

        if i.isfile():
            try:
                with open(full_path, 'rb') as f:
                    t.addfile(i, f)
            except IOError:
                raise IOError(
                    'Can not read file in context: {}'.format(full_path)
                )
        else:
            # Directories, FIFOs, symlinks... don't need to be read.
            t.addfile(i, None)

    for name, contents in extra_files:
        info = tarfile.TarInfo(name)
        info.size = len(contents)
        t.addfile(info, io.BytesIO(contents.encode('utf-8')))

    t.close()
    fileobj.seek(0)
    return fileobj


def mkbuildcontext(dockerfile):
    f = tempfile.NamedTemporaryFile()
    t = tarfile.open(mode='w', fileobj=f)
    if isinstance(dockerfile, io.StringIO):
        dfinfo = tarfile.TarInfo('Dockerfile')
        if six.PY3:
            raise TypeError('Please use io.BytesIO to create in-memory '
                            'Dockerfiles with Python 3')
        else:
            dfinfo.size = len(dockerfile.getvalue())
            dockerfile.seek(0)
    elif isinstance(dockerfile, io.BytesIO):
        dfinfo = tarfile.TarInfo('Dockerfile')
        dfinfo.size = len(dockerfile.getvalue())
        dockerfile.seek(0)
    else:
        dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')
    t.addfile(dfinfo, dockerfile)
    t.close()
    f.seek(0)
    return f