|
1 | 1 | import base64
|
2 |
| -import io |
3 | 2 | import os
|
4 | 3 | import os.path
|
5 | 4 | import json
|
6 | 5 | import shlex
|
7 |
| -import tarfile |
8 |
| -import tempfile |
9 | 6 | from distutils.version import StrictVersion
|
10 | 7 | from datetime import datetime
|
11 | 8 |
|
12 | 9 | import six
|
13 | 10 |
|
14 |
| -from .. import constants |
15 | 11 | from .. import errors
|
16 | 12 | from .. import tls
|
17 | 13 |
|
@@ -46,98 +42,13 @@ def create_ipam_config(*args, **kwargs):
|
46 | 42 | )
|
47 | 43 |
|
48 | 44 |
|
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 |
| - |
72 | 45 | def decode_json_header(header):
|
73 | 46 | data = base64.b64decode(header)
|
74 | 47 | if six.PY3:
|
75 | 48 | data = data.decode('utf-8')
|
76 | 49 | return json.loads(data)
|
77 | 50 |
|
78 | 51 |
|
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 |
| - |
141 | 52 | def compare_version(v1, v2):
|
142 | 53 | """Compare docker versions
|
143 | 54 |
|
|
0 commit comments