Skip to content

Commit fce99c3

Browse files
committed
Move build utils to appropriate file
Signed-off-by: Joffrey F <[email protected]>
1 parent 77c3e57 commit fce99c3

File tree

3 files changed

+93
-93
lines changed

3 files changed

+93
-93
lines changed

docker/utils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# flake8: noqa
2-
from .build import tar, exclude_paths
2+
from .build import create_archive, exclude_paths, mkbuildcontext, tar
33
from .decorators import check_resource, minimum_version, update_headers
44
from .utils import (
55
compare_version, convert_port_bindings, convert_volume_binds,
6-
mkbuildcontext, parse_repository_tag, parse_host,
6+
parse_repository_tag, parse_host,
77
kwargs_from_env, convert_filters, datetime_to_timestamp,
88
create_host_config, parse_bytes, parse_env_file, version_lt,
99
version_gte, decode_json_header, split_command, create_ipam_config,
1010
create_ipam_pool, parse_devices, normalize_links, convert_service_networks,
11-
format_environment, create_archive, format_extra_hosts
11+
format_environment, format_extra_hosts
1212
)
1313

docker/utils/build.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import io
12
import os
23
import re
4+
import six
5+
import tarfile
6+
import tempfile
37

48
from ..constants import IS_WINDOWS_PLATFORM
5-
from .utils import create_archive
69
from fnmatch import fnmatch
710
from itertools import chain
811

@@ -127,3 +130,89 @@ def match(p):
127130
yield f
128131
elif matched:
129132
yield f
133+
134+
135+
def build_file_list(root):
136+
files = []
137+
for dirname, dirnames, fnames in os.walk(root):
138+
for filename in fnames + dirnames:
139+
longpath = os.path.join(dirname, filename)
140+
files.append(
141+
longpath.replace(root, '', 1).lstrip('/')
142+
)
143+
144+
return files
145+
146+
147+
def create_archive(root, files=None, fileobj=None, gzip=False,
148+
extra_files=None):
149+
extra_files = extra_files or []
150+
if not fileobj:
151+
fileobj = tempfile.NamedTemporaryFile()
152+
t = tarfile.open(mode='w:gz' if gzip else 'w', fileobj=fileobj)
153+
if files is None:
154+
files = build_file_list(root)
155+
for path in files:
156+
if path in [e[0] for e in extra_files]:
157+
# Extra files override context files with the same name
158+
continue
159+
full_path = os.path.join(root, path)
160+
161+
i = t.gettarinfo(full_path, arcname=path)
162+
if i is None:
163+
# This happens when we encounter a socket file. We can safely
164+
# ignore it and proceed.
165+
continue
166+
167+
# Workaround https://bugs.python.org/issue32713
168+
if i.mtime < 0 or i.mtime > 8**11 - 1:
169+
i.mtime = int(i.mtime)
170+
171+
if IS_WINDOWS_PLATFORM:
172+
# Windows doesn't keep track of the execute bit, so we make files
173+
# and directories executable by default.
174+
i.mode = i.mode & 0o755 | 0o111
175+
176+
if i.isfile():
177+
try:
178+
with open(full_path, 'rb') as f:
179+
t.addfile(i, f)
180+
except IOError:
181+
raise IOError(
182+
'Can not read file in context: {}'.format(full_path)
183+
)
184+
else:
185+
# Directories, FIFOs, symlinks... don't need to be read.
186+
t.addfile(i, None)
187+
188+
for name, contents in extra_files:
189+
info = tarfile.TarInfo(name)
190+
info.size = len(contents)
191+
t.addfile(info, io.BytesIO(contents.encode('utf-8')))
192+
193+
t.close()
194+
fileobj.seek(0)
195+
return fileobj
196+
197+
198+
def mkbuildcontext(dockerfile):
199+
f = tempfile.NamedTemporaryFile()
200+
t = tarfile.open(mode='w', fileobj=f)
201+
if isinstance(dockerfile, io.StringIO):
202+
dfinfo = tarfile.TarInfo('Dockerfile')
203+
if six.PY3:
204+
raise TypeError('Please use io.BytesIO to create in-memory '
205+
'Dockerfiles with Python 3')
206+
else:
207+
dfinfo.size = len(dockerfile.getvalue())
208+
dockerfile.seek(0)
209+
elif isinstance(dockerfile, io.BytesIO):
210+
dfinfo = tarfile.TarInfo('Dockerfile')
211+
dfinfo.size = len(dockerfile.getvalue())
212+
dockerfile.seek(0)
213+
else:
214+
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')
215+
t.addfile(dfinfo, dockerfile)
216+
t.close()
217+
f.seek(0)
218+
return f

docker/utils/utils.py

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import base64
2-
import io
32
import os
43
import os.path
54
import json
65
import shlex
7-
import tarfile
8-
import tempfile
96
from distutils.version import StrictVersion
107
from datetime import datetime
118

129
import six
1310

14-
from .. import constants
1511
from .. import errors
1612
from .. import tls
1713

@@ -46,98 +42,13 @@ def create_ipam_config(*args, **kwargs):
4642
)
4743

4844

49-
def mkbuildcontext(dockerfile):
50-
f = tempfile.NamedTemporaryFile()
51-
t = tarfile.open(mode='w', fileobj=f)
52-
if isinstance(dockerfile, io.StringIO):
53-
dfinfo = tarfile.TarInfo('Dockerfile')
54-
if six.PY3:
55-
raise TypeError('Please use io.BytesIO to create in-memory '
56-
'Dockerfiles with Python 3')
57-
else:
58-
dfinfo.size = len(dockerfile.getvalue())
59-
dockerfile.seek(0)
60-
elif isinstance(dockerfile, io.BytesIO):
61-
dfinfo = tarfile.TarInfo('Dockerfile')
62-
dfinfo.size = len(dockerfile.getvalue())
63-
dockerfile.seek(0)
64-
else:
65-
dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile')
66-
t.addfile(dfinfo, dockerfile)
67-
t.close()
68-
f.seek(0)
69-
return f
70-
71-
7245
def decode_json_header(header):
7346
data = base64.b64decode(header)
7447
if six.PY3:
7548
data = data.decode('utf-8')
7649
return json.loads(data)
7750

7851

79-
def build_file_list(root):
80-
files = []
81-
for dirname, dirnames, fnames in os.walk(root):
82-
for filename in fnames + dirnames:
83-
longpath = os.path.join(dirname, filename)
84-
files.append(
85-
longpath.replace(root, '', 1).lstrip('/')
86-
)
87-
88-
return files
89-
90-
91-
def create_archive(root, files=None, fileobj=None, gzip=False,
92-
extra_files=None):
93-
if not fileobj:
94-
fileobj = tempfile.NamedTemporaryFile()
95-
t = tarfile.open(mode='w:gz' if gzip else 'w', fileobj=fileobj)
96-
if files is None:
97-
files = build_file_list(root)
98-
for path in files:
99-
if path in [e[0] for e in extra_files]:
100-
# Extra files override context files with the same name
101-
continue
102-
full_path = os.path.join(root, path)
103-
104-
i = t.gettarinfo(full_path, arcname=path)
105-
if i is None:
106-
# This happens when we encounter a socket file. We can safely
107-
# ignore it and proceed.
108-
continue
109-
110-
# Workaround https://bugs.python.org/issue32713
111-
if i.mtime < 0 or i.mtime > 8**11 - 1:
112-
i.mtime = int(i.mtime)
113-
114-
if constants.IS_WINDOWS_PLATFORM:
115-
# Windows doesn't keep track of the execute bit, so we make files
116-
# and directories executable by default.
117-
i.mode = i.mode & 0o755 | 0o111
118-
119-
if i.isfile():
120-
try:
121-
with open(full_path, 'rb') as f:
122-
t.addfile(i, f)
123-
except IOError:
124-
raise IOError(
125-
'Can not read file in context: {}'.format(full_path)
126-
)
127-
else:
128-
# Directories, FIFOs, symlinks... don't need to be read.
129-
t.addfile(i, None)
130-
131-
for name, contents in extra_files:
132-
info = tarfile.TarInfo(name)
133-
info.size = len(contents)
134-
t.addfile(info, io.BytesIO(contents.encode('utf-8')))
135-
136-
t.close()
137-
fileobj.seek(0)
138-
return fileobj
139-
140-
14152
def compare_version(v1, v2):
14253
"""Compare docker versions
14354

0 commit comments

Comments
 (0)