• 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. Say, a new type "bar" in top is mapped like this "foo_V_v<-bar", then we map
22"bar" to whatever "foo" is mapped to in the bottom map. We do this for all new
23types in the top map.
24
25More generally, we can correctly construct x->z from x->y' and y"->z as long as
26y">y'.
27
28This file contains the implementation of combining two mapping files.
29"""
30import argparse
31import re
32from mini_parser import MiniCilParser
33
34def Combine(top, bottom):
35    bottom.types.update(top.types)
36
37    for top_ta in top.typeattributesets:
38        top_type_set = top.typeattributesets[top_ta]
39        if len(top_type_set) == 1:
40            continue
41
42        m = re.match(r"(\w+)_\d+_\d+", top_ta)
43        # Typeattributes in V.v.cil have _V_v suffix, but not in V.v.ignore.cil
44        bottom_type = m.group(1) if m else top_ta
45
46        for bottom_ta in bottom.rTypeattributesets[bottom_type]:
47            bottom.typeattributesets[bottom_ta].update(top_type_set)
48
49    return bottom
50
51if __name__ == "__main__":
52    parser = argparse.ArgumentParser()
53    parser.add_argument("-t", "--top-map", dest="top_map",
54                        required=True, help="top map file")
55    parser.add_argument("-b", "--bottom-map", dest="bottom_map",
56                        required=True, help="bottom map file")
57    parser.add_argument("-o", "--output-file", dest="output_file",
58                        required=True, help="output map file")
59    args = parser.parse_args()
60
61    top_map_cil = MiniCilParser(args.top_map)
62    bottom_map_cil = MiniCilParser(args.bottom_map)
63    result = Combine(top_map_cil, bottom_map_cil)
64
65    with open(args.output_file, "w") as output:
66        output.write(result.unparse())
67