Skip to content

Commit 6609ecb

Browse files
committed
otamaker: Check payload types and files
Verify that the payload type is in the known list. Warn if a file with a compressed-looking name is provided for anything other than a tmpfile, as it is almost certainly a mistake. Signed-off-by: Phil Elwell <phil@raspberrypi.com>
1 parent cc3810e commit 6609ecb

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

otamaker/otamaker

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ BLOCK_PAYLOADS = 2
4646

4747
CONTENTS_FILE = '_contents_.yaml'
4848

49+
# Must be kept up to date with new payload types
50+
KNOWN_PAYLOAD_TYPES = ('image-sparse', 'script', 'tmpfile', 'eeprom-ab')
51+
52+
# The final artefact is already zstd-compressed as a whole, so an
53+
# individually-compressed payload (e.g. 'boot.sparse.zst' instead of
54+
# 'boot.sparse') is almost always a mistake.
55+
COMPRESSED_SUFFIXES = ('.zst', '.gz', '.bz2', '.xz', '.lz4', '.tgz', '.tzst')
56+
4957
def main():
5058
parser = argparse.ArgumentParser()
5159
parser.add_argument('contents',
@@ -72,6 +80,7 @@ def main():
7280
payloads = [CONTENTS_FILE]
7381
outfile = args.outfile
7482
errors = 0
83+
current_payload = {}
7584

7685
with tempfile.TemporaryDirectory() as tmpdir:
7786
for raw_line in lines:
@@ -100,9 +109,11 @@ def main():
100109
# Normalise list item: '- name: x' -> ' name: x'
101110
payload_line = re.sub(r'^\s*-\s*', ' ', line)
102111
name_m = re.match(r'^\s+name:\s*(.+)$', payload_line)
112+
type_m = re.match(r'^\s+type:\s*(.+)$', payload_line)
103113
if name_m:
104114
name = name_m.group(1).strip()
105115
target = (comment if comment is not None else name).strip()
116+
current_payload = {'name': name, 'target': target}
106117
src = os.path.join(cwd, target)
107118
dst = os.path.join(tmpdir, name)
108119
if not os.path.isfile(src) and not os.path.islink(src) and not os.path.isdir(src):
@@ -120,6 +131,18 @@ def main():
120131
else:
121132
print(f'* error: cannot symlink {target}')
122133
errors += 1
134+
elif type_m:
135+
ptype = type_m.group(1).strip()
136+
pname = current_payload.get('name', '?')
137+
target = current_payload.get('target', pname)
138+
if ptype not in KNOWN_PAYLOAD_TYPES:
139+
print(f"* error: payload '{pname}' has unknown type '{ptype}' "
140+
f"(expected one of: {', '.join(KNOWN_PAYLOAD_TYPES)})")
141+
errors += 1
142+
if ptype != "tmpfile" and target.endswith(COMPRESSED_SUFFIXES):
143+
print(f"* warning: payload '{pname}' file '{target}' looks pre-compressed; "
144+
f"the artefact is already compressed as a whole, "
145+
f"did you mean '{re.sub(r'(?:'+'|'.join(re.escape(s) for s in COMPRESSED_SUFFIXES)+r')$', '', target)}'?")
123146

124147
if errors:
125148
sys.exit(1)

0 commit comments

Comments
 (0)