aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-firmware-package.py
blob: b36b55e5096a6e3eb6ba1164c83a3f7295166bc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

import subprocess
import tempfile
import argparse
import hashlib
import tarfile
import json
import sys
import os

def run(*args, **kwargs):
    kwargs.update(stdout = subprocess.PIPE, universal_newlines = True, check = True)
    return subprocess.run(args, **kwargs)

parser = argparse.ArgumentParser()
parser.add_argument("tarfile", type = argparse.FileType("wb"),  help = "tarball to create")
parser.add_argument("firmware", nargs = "+",                    help = "firmware files to stuff into tarball")
args = parser.parse_args()

tar     = tarfile.TarFile.open(fileobj = args.tarfile, mode = "w|gz")
head    = run("git", "rev-parse", "HEAD").stdout.strip()
time    = run("git", "show", "-s", "--format=%ct", "HEAD").stdout.strip()
commits = { path: hash
            for hash, path, branch in (
                    line.split()
                    for line in run("git", "submodule", "status").stdout.splitlines() ) }
sha256  = {}

for fn in args.firmware:
    with open(fn, "rb") as f:
        sha256[os.path.basename(fn)] = hashlib.sha256(f.read()).hexdigest()
    tar.add(fn, os.path.basename(fn))

manifest = json.dumps(dict(head = head, time = time, commits = commits, sha256  = sha256), indent = 2, sort_keys = True)

if os.path.isdir(os.getenv("GNUPGHOME", "")):
    manifest = run("gpg", "--clearsign", "--personal-digest-preferences", "SHA256", "--no-permission-warning",
                   input = manifest).stdout

with tempfile.NamedTemporaryFile("w+") as f:
    os.fchmod(f.fileno(), 0o644)
    f.write(manifest)
    f.seek(0)
    tar.add(f.name, "MANIFEST")

tar.close()
args.tarfile.close()