-
Notifications
You must be signed in to change notification settings - Fork 12
/
flatten.py
executable file
·55 lines (46 loc) · 1.63 KB
/
flatten.py
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
#!/usr/bin/env python3
import json
import os
import sys
import tarfile
WHITEOUT = '.wh.'
WHITEOUT_OPAQUE = '.wh..wh..opq'
def flatten(image, output):
manifest = json.load(image.extractfile('manifest.json'))
assert len(manifest) == 1
manifest = manifest[0]
entries = {}
layers = [tarfile.open(fileobj=image.extractfile(layer)) for layer in manifest['Layers']]
for layer in layers:
real_members = []
# process whiteout files
for info in layer.getmembers():
info.name = './' + info.name
dirname, sep, basename = info.name.rpartition('/')
if basename == WHITEOUT_OPAQUE:
for key in entries.keys():
if key.startswith(dirname+sep):
del entries[key]
elif basename.startswith(WHITEOUT):
del entries[dirname+sep+basename[len(WHITEOUT):]]
else:
real_members.append(info)
continue
# real files
for info in real_members:
entries[info.name] = layer, info
# need a root entry
if './' not in entries:
info = tarfile.TarInfo('./')
info.type = tarfile.DIRTYPE
info.mode = 0o755
entries[info.name] = None, info
for layer, info in entries.values():
fileobj = layer.extractfile(info) if info.isfile() else None
info.mtime = 0 # make reproducible
output.addfile(info, fileobj)
for layer in layers:
layer.close()
with tarfile.open(sys.argv[1]) as image:
with tarfile.open(fileobj=sys.stdout.buffer, mode='w|') as output:
flatten(image, output)