Rework fix-apk to cover all apk files

This commit is contained in:
Disassembler 2019-10-03 12:12:45 +02:00
parent 99d31eb0b5
commit 77196fdcca
Signed by: Disassembler
GPG Key ID: 524BD33A0EE29499

View File

@ -1,12 +1,42 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse
import os import os
import shutil
import sys import sys
import tarfile
import tempfile
def fix_installed(layers): parser = argparse.ArgumentParser(description='APK database merge script')
installed = [] parser.add_argument('layers', help='Path to LXC layers to be merged', nargs=argparse.REMAINDER)
for layer in layers[:-1]:
if len(sys.argv) < 2:
parser.print_usage()
sys.exit(1)
args = parser.parse_args()
layers = args.layers[::-1]
# /etc/apk/world
world = []
for layer in layers:
try:
with open(os.path.join(layer, 'etc/apk/world'), 'r') as f:
for line in f:
if line not in world:
world.append(line)
except:
continue
os.makedirs(os.path.join(layers[-1], 'etc/apk'), 0o755, True)
with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as f:
f.writelines(world)
os.chown(os.path.join(layers[-1], 'etc'), 100000, 100000)
os.chown(os.path.join(layers[-1], 'etc/apk'), 100000, 100000)
os.chown(os.path.join(layers[-1], 'etc/apk/world'), 100000, 100000)
# /lib/apk/db/installed
installed = []
for layer in layers:
try: try:
with open(os.path.join(layer, 'lib/apk/db/installed'), 'r') as f: with open(os.path.join(layer, 'lib/apk/db/installed'), 'r') as f:
buffer = [] buffer = []
@ -22,24 +52,43 @@ def fix_installed(layers):
installed.append(buffer) installed.append(buffer)
except: except:
continue continue
os.makedirs(os.path.join(layers[-1], 'lib/apk/db'), 0o755, True) os.makedirs(os.path.join(layers[-1], 'lib/apk/db'), 0o755, True)
with open(os.path.join(layers[-1], 'lib/apk/db/installed'), 'w') as f: with open(os.path.join(layers[-1], 'lib/apk/db/installed'), 'w') as f:
f.writelines(installed) f.writelines(installed)
os.chown(os.path.join(layers[-1], 'lib'), 100000, 100000)
os.chown(os.path.join(layers[-1], 'lib/apk'), 100000, 100000)
os.chown(os.path.join(layers[-1], 'lib/apk/db'), 100000, 100000)
os.chown(os.path.join(layers[-1], 'lib/apk/db/installed'), 100000, 100000)
def fix_world(layers): # /lib/apk/db/scripts.tar
world = [] tmp_tar_path = tempfile.mkstemp()[1]
for layer in layers[:-1]: files_in_tar = []
with tarfile.open(tmp_tar_path, 'w:') as tmp_tar:
for layer in layers:
tar_path = os.path.join(layer, 'lib/apk/db/scripts.tar')
if os.path.exists(tar_path):
with tarfile.open(tar_path, 'r:') as tar:
for member in tar.getmembers():
if member.name not in files_in_tar:
buffer = tar.extractfile(member)
tmp_tar.addfile(member, buffer)
files_in_tar.append(member.name)
if files_in_tar:
shutil.move(tmp_tar_path, os.path.join(layers[-1], 'lib/apk/db/scripts.tar'))
os.chown(os.path.join(layers[-1], 'lib/apk/db/scripts.tar'), 100000, 100000)
else:
os.unlink(tmp_tar_path)
# /lib/apk/db/triggers
triggers = []
for layer in layers:
try: try:
with open(os.path.join(layer, 'etc/apk/world'), 'r') as f: with open(os.path.join(layer, 'lib/apk/db/triggers'), 'r') as f:
for line in f: for line in f:
if line not in world: if line not in triggers:
world.append(line) triggers.append(line)
except: except:
continue continue
os.makedirs(os.path.join(layers[-1], 'etc/apk'), 0o755, True) with open(os.path.join(layers[-1], 'lib/apk/db/triggers'), 'w') as f:
with open(os.path.join(layers[-1], 'etc/apk/world'), 'w') as f: f.writelines(triggers)
f.writelines(world) os.chown(os.path.join(layers[-1], 'lib/apk/db/triggers'), 100000, 100000)
if __name__ == '__main__':
fix_installed(sys.argv[1:])
fix_world(sys.argv[1:])