Corentin Wallez | 59382b7 | 2020-04-17 20:43:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 2 | # Copyright 2019 The Dawn Authors |
| 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 | |
| 16 | import argparse, glob, os, sys |
| 17 | |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 18 | |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 19 | def check_in_subdirectory(path, directory): |
| 20 | return path.startswith(directory) and not '/' in path[len(directory):] |
| 21 | |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 22 | |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 23 | def check_is_allowed(path, allowed_dirs): |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 24 | return any( |
| 25 | check_in_subdirectory(path, directory) for directory in allowed_dirs) |
| 26 | |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 27 | |
| 28 | def get_all_files_in_dir(find_directory): |
| 29 | result = [] |
| 30 | for (directory, _, files) in os.walk(find_directory): |
| 31 | result += [os.path.join(directory, filename) for filename in files] |
| 32 | return result |
| 33 | |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 34 | |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 35 | def run(): |
| 36 | # Parse command line arguments |
| 37 | parser = argparse.ArgumentParser( |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 38 | description="Removes stale autogenerated files from gen/ directories.") |
| 39 | parser.add_argument( |
| 40 | '--root-dir', |
| 41 | type=str, |
| 42 | help='The root directory, all other paths in files are relative to it.' |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 43 | ) |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 44 | parser.add_argument( |
| 45 | '--allowed-output-dirs-file', |
| 46 | type=str, |
| 47 | help='The file containing a list of allowed directories') |
| 48 | parser.add_argument( |
| 49 | '--stale-dirs-file', |
| 50 | type=str, |
| 51 | help= |
| 52 | 'The file containing a list of directories to check for stale files') |
| 53 | parser.add_argument('--stamp', |
| 54 | type=str, |
| 55 | help='A stamp written once this script completes') |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 56 | args = parser.parse_args() |
| 57 | |
| 58 | root_dir = args.root_dir |
| 59 | stamp_file = args.stamp |
| 60 | |
| 61 | # Load the list of allowed and stale directories |
| 62 | with open(args.allowed_output_dirs_file) as f: |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 63 | allowed_dirs = set( |
| 64 | [os.path.join(root_dir, line.strip()) for line in f.readlines()]) |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 65 | |
| 66 | for directory in allowed_dirs: |
| 67 | if not directory.endswith('/'): |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 68 | print('Allowed directory entry "{}" doesn\'t end with /'.format( |
| 69 | directory)) |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 70 | return 1 |
| 71 | |
| 72 | with open(args.stale_dirs_file) as f: |
| 73 | stale_dirs = set([line.strip() for line in f.readlines()]) |
| 74 | |
| 75 | # Remove all files in stale dirs that aren't in the allowed dirs. |
| 76 | for stale_dir in stale_dirs: |
| 77 | stale_dir = os.path.join(root_dir, stale_dir) |
| 78 | |
| 79 | for candidate in get_all_files_in_dir(stale_dir): |
| 80 | if not check_is_allowed(candidate, allowed_dirs): |
| 81 | os.remove(candidate) |
| 82 | |
| 83 | # Finished! Write the stamp file so ninja knows to not run this again. |
| 84 | with open(stamp_file, "w") as f: |
| 85 | f.write("") |
| 86 | |
| 87 | return 0 |
| 88 | |
Kai Ninomiya | 01aeca2 | 2020-07-15 19:51:17 +0000 | [diff] [blame] | 89 | |
Corentin Wallez | a9a84df | 2019-09-19 23:30:42 +0000 | [diff] [blame] | 90 | if __name__ == "__main__": |
| 91 | sys.exit(run()) |