Mike Dougherty | 69f88351 | 2023-07-27 21:06:14 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2023 The Chromium Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | '''Removes the file and directory names of Downloaded items from within a |
| 7 | Sandbox File Statistics json output file. |
| 8 | ''' |
| 9 | |
| 10 | import argparse |
| 11 | import json |
| 12 | import os |
| 13 | import sys |
| 14 | |
| 15 | def clean(stats_json): |
| 16 | for item in stats_json: |
| 17 | for attribute, value in item.items(): |
| 18 | if attribute == 'name': |
| 19 | item['name'] = '##DOWNLOADED_ITEM##' |
| 20 | elif attribute == 'contents': |
| 21 | clean(item['contents']) |
| 22 | |
| 23 | def main(): |
| 24 | description = 'Removes file names in downloads directory stats.' |
| 25 | parser = argparse.ArgumentParser(description=description) |
| 26 | |
| 27 | parser.add_argument('stats_json_path', nargs=1, |
| 28 | help='path to file statistics json file') |
| 29 | |
| 30 | options, extra_options = parser.parse_known_args() |
| 31 | if len(extra_options): |
| 32 | print >> sys.stderr, 'Unknown options: ', extra_options |
| 33 | return 1 |
| 34 | |
| 35 | stats_json_path = options.stats_json_path[0] |
| 36 | |
| 37 | if not os.path.isfile(stats_json_path): |
| 38 | print('The input file does not exist: ' + stats_json_path, file=sys.stderr) |
| 39 | return 1 |
| 40 | |
| 41 | stats_json_splitext = os.path.splitext(stats_json_path) |
| 42 | out_json_path = stats_json_splitext[0] + '_clean' + stats_json_splitext[1] |
| 43 | |
| 44 | if os.path.exists(out_json_path): |
| 45 | print('The output file already exists: ' + out_json_path, file=sys.stderr) |
| 46 | return 1 |
| 47 | |
| 48 | stats_json = None |
| 49 | with open(stats_json_path, 'r') as json_it: |
| 50 | stats_json = json.load(json_it) |
| 51 | for item in stats_json['contents']: |
| 52 | if item['name'] == 'Documents': |
| 53 | clean(item['contents']) |
| 54 | |
| 55 | with open(out_json_path, 'x') as out_file: |
| 56 | json.dump(stats_json, out_file) |
| 57 | |
| 58 | return 0 |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | sys.exit(main()) |