File: merge_rules.py

package info (click to toggle)
0ad 0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 171,928 kB
  • sloc: cpp: 194,011; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,695; perl: 1,575; java: 533; xml: 415; php: 192; makefile: 99
file content (112 lines) | stat: -rw-r--r-- 4,115 bytes parent folder | download | duplicates (3)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
#
# Copyright (C) 2024 Wildfire Games.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import argparse
import json
import os


def make_plane_combinations(combinations):
    plane_combinations = []
    for combination in combinations:
        plane_combination = [(define["name"], define["value"]) for define in combination]
        plane_combination.sort()
        if plane_combination not in plane_combinations:
            plane_combinations.append(plane_combination)
    plane_combinations.sort()
    return plane_combinations


def merge_plane_combinations(lhs, rhs):
    # We assume that combinations are sorted.
    combined_plane_combinations = []
    lhs_index, rhs_index = 0, 0
    while lhs_index < len(lhs) and rhs_index < len(rhs):
        if lhs[lhs_index] == rhs[rhs_index]:
            combined_plane_combinations.append(lhs[lhs_index])
            lhs_index += 1
            rhs_index += 1
        elif lhs[lhs_index] < rhs[rhs_index]:
            combined_plane_combinations.append(lhs[lhs_index])
            lhs_index += 1
        else:
            combined_plane_combinations.append(rhs[rhs_index])
            rhs_index += 1
    while lhs_index < len(lhs):
        combined_plane_combinations.append(lhs[lhs_index])
        lhs_index += 1
    while rhs_index < len(rhs):
        combined_plane_combinations.append(rhs[rhs_index])
        rhs_index += 1

    combinations = []
    for plane_combination in combined_plane_combinations:
        combinations.append(
            [{"name": define[0], "value": define[1]} for define in plane_combination]
        )
    return combinations


def merge_rules(lhs, rhs):
    rules = {}
    for program_name in set(lhs.keys()) | set(rhs.keys()):
        rules[program_name] = {
            "name": program_name,
            "combinations": merge_plane_combinations(
                make_plane_combinations(
                    lhs[program_name]["combinations"]
                    if program_name in lhs and "combinations" in lhs[program_name]
                    else []
                ),
                make_plane_combinations(
                    rhs[program_name]["combinations"]
                    if program_name in rhs and "combinations" in rhs[program_name]
                    else []
                ),
            ),
        }
    return rules


def run():
    parser = argparse.ArgumentParser()
    parser.add_argument("output_rules_path", help="a path to output merged rules")
    parser.add_argument("input_paths", help="a paths to input rules", nargs=argparse.REMAINDER)
    args = parser.parse_args()

    if not args.input_paths or any(
        not os.path.isfile(input_path) for input_path in args.input_paths
    ):
        raise ValueError("Invalid input files.")

    rules = {}
    for input_path in args.input_paths:
        with open(input_path) as handle:
            input_rules = json.load(handle)
        rules = merge_rules(rules, input_rules)

    with open(args.output_rules_path, "w") as handle:
        json.dump(rules, handle, sort_keys=True)


if __name__ == "__main__":
    run()