• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2018 - The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Tool to combine SEPolicy mapping file.
16
17Say, x, y, z are platform SEPolicy versions such that x > y > z. Then given two
18mapping files from x to y (top) and y to z (bottom), it's possible to construct
19a mapping file from x to z. We do the following to combine two maps.
201. Add all new types declarations from top to bottom.
212. Add all new typeattribute declarations from top to bottom.
223. Say, a new type "bar" in top is mapped like this "foo_V_v<-bar", then we map
23"bar" to whatever "foo" is mapped to in the bottom map. We do this for all new
24types in the top map.
25
26More generally, we can correctly construct x->z from x->y' and y"->z as long as
27y">y'.
28
29This file contains the implementation of combining two mapping files.
30"""
31import argparse
32import re
33from mini_parser import MiniCilParser
34
35def Combine(top, bottom):
36    bottom.types.update(top.types)
37    bottom.typeattributes.update(top.typeattributes)
38
39    for top_ta in top.typeattributesets:
40        top_type_set = top.typeattributesets[top_ta]
41        if len(top_type_set) == 1:
42            continue
43
44        m = re.fullmatch(r"(\w+?)_\d+(_0)?", top_ta)
45        # Typeattributes in V(.0).cil have _V(_0) suffix, but not in
46        # V(.0).ignore.cil
47        bottom_type = m.group(1) if m else top_ta
48
49        # If type doesn't exist in bottom map, no need to maintain mappings to
50        # that type.
51        if bottom_type not in bottom.rTypeattributesets.keys():
52            continue
53
54        for bottom_ta in bottom.rTypeattributesets[bottom_type]:
55            bottom.typeattributesets[bottom_ta].update(top_type_set)
56
57    return bottom
58
59if __name__ == "__main__":
60    parser = argparse.ArgumentParser()
61    parser.add_argument("-t", "--top-map", dest="top_map",
62                        required=True, help="top map file")
63    parser.add_argument("-b", "--bottom-map", dest="bottom_map",
64                        required=True, help="bottom map file")
65    parser.add_argument("-o", "--output-file", dest="output_file",
66                        required=True, help="output map file")
67    args = parser.parse_args()
68
69    top_map_cil = MiniCilParser(args.top_map)
70    bottom_map_cil = MiniCilParser(args.bottom_map)
71    result = Combine(top_map_cil, bottom_map_cil)
72
73    with open(args.output_file, "w") as output:
74        output.write(result.unparse())
75