• 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.match(r"(\w+)_\d+_\d+", top_ta)
45        # Typeattributes in V.v.cil have _V_v suffix, but not in V.v.ignore.cil
46        bottom_type = m.group(1) if m else top_ta
47
48        # If type doesn't exist in bottom map, no need to maintain mappings to
49        # that type.
50        if bottom_type not in bottom.rTypeattributesets.keys():
51            continue
52
53        for bottom_ta in bottom.rTypeattributesets[bottom_type]:
54            bottom.typeattributesets[bottom_ta].update(top_type_set)
55
56    return bottom
57
58if __name__ == "__main__":
59    parser = argparse.ArgumentParser()
60    parser.add_argument("-t", "--top-map", dest="top_map",
61                        required=True, help="top map file")
62    parser.add_argument("-b", "--bottom-map", dest="bottom_map",
63                        required=True, help="bottom map file")
64    parser.add_argument("-o", "--output-file", dest="output_file",
65                        required=True, help="output map file")
66    args = parser.parse_args()
67
68    top_map_cil = MiniCilParser(args.top_map)
69    bottom_map_cil = MiniCilParser(args.bottom_map)
70    result = Combine(top_map_cil, bottom_map_cil)
71
72    with open(args.output_file, "w") as output:
73        output.write(result.unparse())
74