aboutsummaryrefslogtreecommitdiff
path: root/tools/rro/verify-overlayable.py
blob: b469e3ccd8c3f7b9a2df7d4ce2e27a08e902aa72 (plain)
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
#!/usr/bin/env python3
#  Copyright (C) 2021 The Android Open Source Project
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import argparse
import sys
from resource_utils import get_all_resources, get_resources_from_single_file, Resource

if sys.version_info[0] != 3:
    print("Must use python 3")
    sys.exit(1)

"""
Script used to verify the 'overlayable.xml' file.
"""
def main():
    parser = argparse.ArgumentParser(description='Verify overlayable.xml.')
    optional_args = parser.add_argument_group('optional arguments')
    optional_args.add_argument('-e', '--excludeFiles', nargs='*', help='File paths (absolute or relative to cwd) that should be excluded when generating overlayable.xml')
    required_args = parser.add_argument_group('required arguments')
    required_args.add_argument('-r', '--resourcePath', help='Path to resource directory (absolute or relative to cwd)', required=True)
    required_args.add_argument('-o', '--overlayableFilePath', help='Filepath to overlayable.xml (absolute or relative to cwd).', required=True)
    args = parser.parse_args()

    resources = get_all_resources(args.resourcePath, args.excludeFiles)
    old_mapping = get_resources_from_single_file(args.overlayableFilePath)
    compare_resources(old_mapping, resources, args.overlayableFilePath)

def compare_resources(old_mapping, new_mapping, res_public_file):
    removed = old_mapping.difference(new_mapping)
    added = new_mapping.difference(old_mapping)
    if len(removed) > 0:
        print('Resources removed:\n' + '\n'.join(map(lambda x: str(x), removed)))
    if len(added) > 0:
        print('Resources added:\n' + '\n'.join(map(lambda x: str(x), added)))
    if len(added) + len(removed) > 0:
        print("Some resource have been modified. If this is intentional please " +
              "run 'python generate-overlayable.py' again and submit the new %s" % res_public_file)
        sys.exit(1)

if __name__ == '__main__':
    main()