fix_style: Add Python 3 support (see issue #2856)

This commit is contained in:
Marshall Greenblatt 2020-01-13 21:43:18 +01:00
parent f0347f0589
commit 53b98a5022
2 changed files with 17 additions and 13 deletions

View File

@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import absolute_import
from __future__ import print_function
import os, re, sys import os, re, sys
from clang_util import clang_format from clang_util import clang_format
from file_util import eval_file, get_files, read_file, write_file from file_util import eval_file, get_files, read_file, write_file
@ -34,7 +36,7 @@ def msg(filename, status):
filename = filename[pos:] filename = filename[pos:]
filename = "..." + filename filename = "..." + filename
print "%-60s %s" % (filename, status) print("%-60s %s" % (filename, status))
updatect = 0 updatect = 0
@ -124,15 +126,15 @@ def fix_style(filenames, white_list=None, black_list=None):
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) == 1: if len(sys.argv) == 1:
print "Usage: %s [file-path|git-hash|unstaged|staged] ..." % sys.argv[0] print("Usage: %s [file-path|git-hash|unstaged|staged] ...\n" % sys.argv[0])
print "\n Format C, C++ and ObjC files using Chromium's clang-format style." print(" Format C, C++ and ObjC files using Chromium's clang-format style.")
print "\nOptions:" print("\nOptions:")
print " file-path\tProcess the specified file or directory." print(" file-path\tProcess the specified file or directory.")
print " \t\tDirectories will be processed recursively." print(" \t\tDirectories will be processed recursively.")
print " \t\tThe \"*\" wildcard character is supported." print(" \t\tThe \"*\" wildcard character is supported.")
print " git-hash\tProcess all files changed in the specified Git commit." print(" git-hash\tProcess all files changed in the specified Git commit.")
print " unstaged\tProcess all unstaged files in the Git repo." print(" unstaged\tProcess all unstaged files in the Git repo.")
print " staged\t\tProcess all staged files in the Git repo." print(" staged\t\tProcess all staged files in the Git repo.")
sys.exit(1) sys.exit(1)
# Read the configuration file. # Read the configuration file.
@ -140,4 +142,4 @@ if __name__ == "__main__":
# Process anything passed on the command-line. # Process anything passed on the command-line.
fix_style(sys.argv[1:]) fix_style(sys.argv[1:])
print 'Done - Wrote %d files.' % updatect print('Done - Wrote %d files.' % updatect)

View File

@ -2,6 +2,8 @@
# reserved. Use of this source code is governed by a BSD-style license that # reserved. Use of this source code is governed by a BSD-style license that
# can be found in the LICENSE file # can be found in the LICENSE file
from __future__ import absolute_import
from __future__ import print_function
from exec_util import exec_cmd from exec_util import exec_cmd
import os import os
import sys import sys
@ -14,9 +16,9 @@ root_dir = os.path.join(script_dir, os.pardir)
def yapf_format(file_name, file_contents): def yapf_format(file_name, file_contents):
# Reads .style.yapf in the root_dir when specifying contents via stdin. # Reads .style.yapf in the root_dir when specifying contents via stdin.
result = exec_cmd("%s %s/yapf" % (sys.executable, script_dir), root_dir, result = exec_cmd("%s %s/yapf" % (sys.executable, script_dir), root_dir,
file_contents) file_contents.encode('utf-8'))
if result['err'] != '': if result['err'] != '':
print "yapf error: %s" % result['err'] print("yapf error: %s" % result['err'])
if result['out'] != '': if result['out'] != '':
output = result['out'] output = result['out']
if sys.platform == 'win32': if sys.platform == 'win32':