|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Dumps data bundle metadata from the database in CSV format.""" |
| 15 | + |
| 16 | +import csv |
| 17 | + |
| 18 | +from clusterfuzz._internal.datastore import data_types |
| 19 | + |
| 20 | + |
| 21 | +def _dump(f) -> None: |
| 22 | + """Dumps data bundle metadata from the database to the given file.""" |
| 23 | + writer = csv.DictWriter( |
| 24 | + f, |
| 25 | + fieldnames=[ |
| 26 | + 'name', |
| 27 | + 'bucket_name', |
| 28 | + 'source', |
| 29 | + 'timestamp', |
| 30 | + 'sync_to_worker', |
| 31 | + ]) |
| 32 | + writer.writeheader() |
| 33 | + |
| 34 | + for bundle in data_types.DataBundle.query(): |
| 35 | + writer.writerow({ |
| 36 | + 'name': bundle.name, |
| 37 | + 'bucket_name': bundle.bucket_name, |
| 38 | + 'source': bundle.source, |
| 39 | + 'timestamp': str(bundle.timestamp), |
| 40 | + 'sync_to_worker': bundle.sync_to_worker, |
| 41 | + }) |
| 42 | + |
| 43 | + |
| 44 | +def execute(args): |
| 45 | + """Dumps data bundle metadata from the database in CSV format.""" |
| 46 | + if not args.script_args or len(args.script_args) != 1: |
| 47 | + print('Usage: dump_data_bundles --script_arg OUTPUT_FILE') |
| 48 | + return |
| 49 | + |
| 50 | + with open(args.script_args[0], 'w') as f: |
| 51 | + _dump(f) |
0 commit comments