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