File: manip_gadget.py

package info (click to toggle)
snapd 2.49-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,432 kB
  • sloc: ansic: 12,125; sh: 8,453; python: 2,163; makefile: 1,284; exp: 173; xml: 22
file content (50 lines) | stat: -rwxr-xr-x 1,334 bytes parent folder | download | duplicates (4)
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
49
50
#!/usr/bin/python3

import sys
import argparse

import yaml


def parse_arguments():
    parser = argparse.ArgumentParser(description="pc gadget yaml generator for test")
    parser.add_argument(
        "gadgetyaml", type=argparse.FileType("r"), help="path to gadget.yaml input file"
    )
    return parser.parse_args()


def must_find_struct(structs, name):
    found = [s for s in structs if s["name"] == name]
    if len(found) != 1:
        raise RuntimeError("no structure with name {} among: {}".format(name, structs))
    return found[0]


def bump_update_edition(update):
    if update is None:
        return {"edition": 1}
    if "edition" not in update:
        update["edition"] = 1
    else:
        update["edition"] += 1
    return update


def main(opts):
    gadget_yaml = yaml.safe_load(opts.gadgetyaml)

    structs = gadget_yaml["volumes"]["pc"]["structure"]
    ubuntu_seed = must_find_struct(structs, "ubuntu-seed")
    ubuntu_seed["update"] = bump_update_edition(ubuntu_seed.get("update"))

    # trigger an update, so that device lookup is performed
    mbr = must_find_struct(structs, "mbr")
    mbr["update"] = bump_update_edition(mbr.get("update"))

    yaml.dump(gadget_yaml, stream=sys.stdout, indent=2, default_flow_style=False)


if __name__ == "__main__":
    opts = parse_arguments()
    main(opts)