1#!/usr/bin/env python3
2#  Copyright (C) 2021 The Android Open Source Project
3#
4#  Licensed under the Apache License, Version 2.0 (the "License");
5#  you may not use this file except in compliance with the License.
6#  You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#  Unless required by applicable law or agreed to in writing, software
11#  distributed under the License is distributed on an "AS IS" BASIS,
12#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#  See the License for the specific language governing permissions and
14#  limitations under the License.
15
16import argparse
17import sys
18from resource_utils import get_all_resources, get_resources_from_single_file, Resource
19
20if sys.version_info[0] != 3:
21    print("Must use python 3")
22    sys.exit(1)
23
24"""
25Script used to verify the 'overlayable.xml' file.
26"""
27def main():
28    parser = argparse.ArgumentParser(description='Verify overlayable.xml.')
29    optional_args = parser.add_argument_group('optional arguments')
30    optional_args.add_argument('-e', '--excludeFiles', nargs='*', help='File paths (absolute or relative to cwd) that should be excluded when generating overlayable.xml')
31    optional_args.add_argument('-m', '--errorMessage', nargs='*', help='Custom error message if resources are added or removed.')
32    required_args = parser.add_argument_group('required arguments')
33    required_args.add_argument('-r', '--resourcePath', help='Path to resource directory (absolute or relative to cwd)', required=True, action='append')
34    required_args.add_argument('-o', '--overlayableFilePath', help='Filepath to overlayable.xml (absolute or relative to cwd).', required=True)
35    args = parser.parse_args()
36
37    resources = set()
38    for path in args.resourcePath:
39        resources |= get_all_resources(path, args.excludeFiles)
40    old_mapping = get_resources_from_single_file(args.overlayableFilePath)
41    compare_resources(old_mapping, resources, args.overlayableFilePath, args.errorMessage)
42
43def compare_resources(old_mapping, new_mapping, res_public_file, errorMsg):
44    removed = old_mapping.difference(new_mapping)
45    added = new_mapping.difference(old_mapping)
46    if len(removed) > 0:
47        print('Resources removed:\n' + '\n'.join(map(lambda x: str(x), removed)))
48    if len(added) > 0:
49        print('Resources added:\n' + '\n'.join(map(lambda x: str(x), added)))
50    if len(added) + len(removed) > 0:
51        if errorMsg is not None:
52            print(errorMsg)
53        else:
54            print("Some resource have been modified. If this is intentional please " +
55                  "run 'python3 generate-overlayable.py' again and submit the new %s" % res_public_file)
56            print("Some projects may include $PROJECT_TOP/tools/generate-overlayable.sh which calls " +
57                  "the above command with the appropriate command line arguments")
58        sys.exit(1)
59
60if __name__ == '__main__':
61    main()
62