Spotter-VM/build/usr/lib/python3.6/lxcbuild/packer.py

41 lines
1.2 KiB
Python
Raw Normal View History

2019-09-20 10:13:41 +02:00
# -*- coding: utf-8 -*-
import json
import os
import subprocess
from . import crypto
from .paths import PRIVATE_KEY, REPO_META_FILE, REPO_SIG_FILE
2019-09-20 10:13:41 +02:00
class PackageExistsError(Exception):
pass
class Packer:
def __init__(self):
self.tar_path = None
2019-09-24 10:04:13 +02:00
self.tar_size = 0
2019-09-20 10:13:41 +02:00
self.xz_path = None
2019-09-24 10:04:13 +02:00
self.xz_size = 0
2019-09-20 15:43:01 +02:00
if os.path.exists(REPO_META_FILE):
with open(REPO_META_FILE, 'r') as f:
self.packages = json.load(f)
2019-09-20 10:13:41 +02:00
else:
2019-09-20 15:43:01 +02:00
self.packages = {'apps': {}, 'images': {}}
2019-09-20 10:13:41 +02:00
2019-09-20 15:43:01 +02:00
def save_repo_meta(self):
with open(REPO_META_FILE, 'w') as f:
json.dump(self.packages, f, sort_keys=True, indent=4)
2019-09-20 10:13:41 +02:00
def compress_archive(self):
# Compress the tarball with xz (LZMA2)
2019-09-24 10:04:13 +02:00
self.tar_size = os.path.getsize(self.tar_path)
print('Compressing', self.tar_path, '({:.2f} MB)'.format(self.tar_size/1048576))
2019-09-20 10:13:41 +02:00
subprocess.run(['xz', '-9', self.tar_path])
2019-09-24 10:04:13 +02:00
self.xz_size = os.path.getsize(self.xz_path)
print('Compressed ', self.xz_path, '({:.2f} MB)'.format(self.xz_size/1048576))
2019-09-20 10:13:41 +02:00
def sign_packages(self):
2019-09-20 15:43:01 +02:00
signature = crypto.sign_file(PRIVATE_KEY, REPO_META_FILE)
with open(REPO_SIG_FILE, 'wb') as f:
2019-09-20 10:13:41 +02:00
f.write(signature)