1#!/usr/bin/env python3 2# Copyright (C) 2025 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 subprocess 18import sys 19from pathlib import Path 20 21ROOT_DIR = Path(__file__).parent.parent 22FORMAT_SQL = ROOT_DIR / 'tools' / 'format_sql.py' 23STDLIB_PATH = ROOT_DIR / 'src' / 'trace_processor' / 'perfetto_sql' / 'stdlib' 24 25def main(): 26 parser = argparse.ArgumentParser(description='Format SQL files in the stdlib directory') 27 parser.add_argument('paths', 28 nargs='*', 29 help='Optional paths to format (must be within stdlib)') 30 parser.add_argument('--check-only', 31 action='store_true', 32 help='Check if files are properly formatted without making changes') 33 parser.add_argument('--verbose', 34 action='store_true', 35 help='Print status messages during formatting') 36 args = parser.parse_args() 37 38 if not STDLIB_PATH.exists(): 39 print(f'Error: stdlib directory not found at {STDLIB_PATH}', 40 file=sys.stderr) 41 return 1 42 43 paths_to_format = [str(STDLIB_PATH)] if not args.paths else args.paths 44 cmd = [str(FORMAT_SQL)] + paths_to_format 45 if args.check_only: 46 cmd.append('--check-only') 47 else: 48 cmd.append('--in-place') 49 if args.verbose: 50 cmd.append('--verbose') 51 52 try: 53 return subprocess.run(cmd, check=False).returncode 54 except subprocess.CalledProcessError as e: 55 return e.returncode 56 57if __name__ == '__main__': 58 sys.exit(main())