import os import shutil import stat import struct import subprocess import sys import container # 1 def main(output_path, entry_path, input_paths, squashfs_path): output_tmp_path = output_path + '.tmp' shutil.copy2(entry_path, output_tmp_path) st = os.stat(output_tmp_path) os.chmod(output_tmp_path, st.st_mode | stat.S_IWUSR) layer_paths = [] other_paths = [] for input_path in input_paths: (layer_paths if input_path.endswith('.container_layer') else other_paths).append(input_path) if len(other_paths) == 0: raise Exception('No program in container dependencies') if len(other_paths) > 1: raise Exception('Multiple non-layer inputs') program_path = other_paths[0] program_container_path = os.path.basename(program_path) os.symlink(program_container_path, 'entry') add_cmd = [os.path.join(squashfs_path, 'mksquashfs')] add_cmd.extend([program_path, 'entry', 'program_layer']) subprocess.run(add_cmd) layer_paths.append('program_layer') container.join_layers(layer_paths, 'container_data', squashfs_path) size = 0 block_size = 1024 * 1024 with open(output_tmp_path, 'ab') as output: with open('container_data', 'rb') as input_: while True: data = input_.read(block_size) output.write(data) size += len(data) if len(data) < block_size: break with open(os.path.join(squashfs_path, 'unsquashfs'), 'rb') as input_: while True: data = input_.read(block_size) output.write(data) size += len(data) if len(data) == 0: break output.write(struct.pack('