1#!/usr/bin/env python 2# 3# Copyright 2013 The Flutter 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 7import sys 8import subprocess 9import os 10import argparse 11import errno 12import shutil 13 14def GetGNFiles(directory): 15 directory = os.path.abspath(directory) 16 gn_files = [] 17 assert os.path.exists(directory), "Directory must exist %s" % directory 18 for root, dirs, files in os.walk(directory): 19 for file in files: 20 if file.endswith(".gn") or file.endswith(".gni"): 21 gn_files.append(os.path.join(root, file)) 22 return gn_files 23 24def main(): 25 parser = argparse.ArgumentParser(); 26 27 parser.add_argument('--gn-binary', dest='gn_binary', required=True, type=str) 28 parser.add_argument('--dry-run', dest='dry_run', required=True, type=bool) 29 parser.add_argument('--root-directory', dest='root_directory', required=True, type=str) 30 31 args = parser.parse_args() 32 33 gn_binary = os.path.abspath(args.gn_binary) 34 assert os.path.exists(gn_binary), "GN Binary must exist %s" % gn_binary 35 36 gn_command = [ gn_binary, 'format'] 37 38 if args.dry_run: 39 gn_command.append('--dry-run') 40 41 42 for gn_file in GetGNFiles(args.root_directory): 43 if subprocess.call(gn_command + [ gn_file ]) != 0: 44 print "ERROR: '%s' is incorrectly formatted." % os.path.relpath(gn_file, args.root_directory) 45 print "Format the same with 'gn format' using the 'gn' binary in //buildtools." 46 print "Or, run ./ci/check_gn_format.py with '--dry-run false'" 47 return -1 48 49 return 0 50 51if __name__ == '__main__': 52 sys.exit(main()) 53