container.py 825 B

1234567891011121314151617181920212223242526272829
  1. import subprocess
  2. import os
  3. import shutil
  4. class ContainerError(Exception):
  5. pass
  6. def join_layers(input_paths, output_path, squashfs_path):
  7. if len(input_paths) == 1:
  8. shutil.copy2(input_paths[0], output_path)
  9. else:
  10. # We cannot use appending here as it doesn't allow replacing files
  11. for input_path in input_paths:
  12. unpack_cmd = [os.path.join(squashfs_path, 'unsquashfs')]
  13. unpack_cmd.extend(['-f', input_path])
  14. subprocess.run(unpack_cmd)
  15. pack_cmd = [os.path.join(squashfs_path, 'mksquashfs')]
  16. pack_cmd.append(os.path.join(os.curdir, 'squashfs-root'))
  17. pack_cmd.append(output_path)
  18. pack_cmd.append('-all-root')
  19. subprocess.run(pack_cmd)
  20. shutil.rmtree(os.path.join(os.curdir, 'squashfs-root'))
  21. return 0