File: feattranslator.py

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (84 lines) | stat: -rwxr-xr-x 2,715 bytes parent folder | download | duplicates (2)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3

from features import Change, Cmd, Endpoint, Interface, Status, Task
import argparse
import json
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))


def translate_all_features(all_features_json: str, output_json: str):
    with open(all_features_json, "r", encoding="utf-8") as f:
        all_features = json.load(f)

    if any(
        key not in all_features
        for key in [
            "endpoints",
            "tasks",
            "commands",
            "ensures",
            "interfaces",
            "changes",
        ]
    ):
        raise RuntimeError(f"missing key in all features file {all_features_json}")

    all_endpoints = []
    for endpoint in all_features["endpoints"]:
        if "actions" in endpoint and endpoint["actions"]:
            for action in endpoint["actions"]:
                all_endpoints.append(
                    Endpoint(
                        method=endpoint["method"], path=endpoint["path"], action=action
                    )
                )
        else:
            all_endpoints.append(
                Endpoint(method=endpoint["method"], path=endpoint["path"])
            )
    all_cmds = [Cmd(cmd=cmd) for cmd in all_features["commands"]]
    all_tasks = []
    for task in all_features["tasks"]:
        status_list = (
            [Status.done, Status.undone, Status.error]
            if "has-undo" in task and task["has-undo"]
            else [Status.done, Status.error]
        )
        for status in status_list:
            all_tasks.append(Task(kind=task["kind"], last_status=status))
    all_changes = [Change(kind=change) for change in all_features["changes"]]
    all_interfaces = [Interface(name=iface) for iface in all_features["interfaces"]]

    with open(output_json, "w", encoding="utf-8") as f:
        json.dump(
            {
                "cmds": all_cmds,
                "ensures": all_features["ensures"],
                "tasks": all_tasks,
                "changes": all_changes,
                "endpoints": all_endpoints,
                "interfaces": all_interfaces,
            },
            f,
        )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Translates feature tagging output from 'snap debug features' to the same format as feature tagging reports"
    )
    parser.add_argument(
        "-f", "--file", help="snap debug features output file", required=True, type=str
    )
    parser.add_argument(
        "-o",
        "--output",
        help="where to write translated output",
        required=True,
        type=str,
    )
    args = parser.parse_args()
    translate_all_features(args.file, args.output)