Abhishek Pandit-Subedi | b75bd56 | 2021-02-25 15:32:22 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | """ This script cleans up the vendor directory. |
| 7 | """ |
| 8 | import json |
| 9 | import os |
| 10 | import pathlib |
| 11 | |
| 12 | |
| 13 | def _remove_OWNERS_checksum(root): |
| 14 | """ Delete all OWNERS files from the checksum file. |
| 15 | |
| 16 | Args: |
| 17 | root: Root directory for the vendored crate. |
| 18 | |
| 19 | Returns: |
| 20 | True if OWNERS was found and cleaned up. Otherwise False. |
| 21 | """ |
| 22 | checksum_path = os.path.join(root, '.cargo-checksum.json') |
| 23 | if not pathlib.Path(checksum_path).is_file(): |
| 24 | return False |
| 25 | |
| 26 | with open(checksum_path, 'r') as fread: |
| 27 | contents = json.load(fread) |
| 28 | |
| 29 | del_keys = [] |
| 30 | for cfile in contents['files']: |
| 31 | if 'OWNERS' in cfile: |
| 32 | del_keys.append(cfile) |
| 33 | |
| 34 | for key in del_keys: |
| 35 | del contents['files'][key] |
| 36 | |
| 37 | if del_keys: |
| 38 | print('{} deleted: {}'.format(root, del_keys)) |
| 39 | with open(checksum_path, 'w') as fwrite: |
| 40 | json.dump(contents, fwrite) |
| 41 | |
| 42 | return bool(del_keys) |
| 43 | |
| 44 | |
| 45 | def cleanup_owners(vendor_path): |
| 46 | """ Remove owners checksums from the vendor directory. |
| 47 | |
| 48 | We currently do not check in the OWNERS files from vendored crates because |
| 49 | they interfere with the find-owners functionality in gerrit. This cleanup |
| 50 | simply finds all instances of "OWNERS" in the checksum files within and |
| 51 | removes them. |
| 52 | |
| 53 | Args: |
| 54 | vendor_path: Absolute path to vendor directory. |
| 55 | """ |
| 56 | deps_cleaned = [] |
| 57 | for root, dirs, _ in os.walk(vendor_path): |
| 58 | for d in dirs: |
| 59 | removed = _remove_OWNERS_checksum(os.path.join(root, d)) |
| 60 | if removed: |
| 61 | deps_cleaned.append(d) |
| 62 | |
| 63 | if deps_cleaned: |
| 64 | print('Cleanup owners:\n {}'.format("\n".join(deps_cleaned))) |
| 65 | |
| 66 | |
| 67 | def main(): |
| 68 | current_path = pathlib.Path(__file__).parent.absolute() |
| 69 | |
| 70 | # All cleanups |
| 71 | cleanup_owners(os.path.join(current_path, 'vendor')) |
| 72 | |
| 73 | |
| 74 | if __name__ == '__main__': |
| 75 | main() |