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