Port tools/clang-format/git-clang-format to work Python beyond 2.7
Since Python3 may deliberately not be available on Linux Systems (to prevent people from writing non-futureproof code), git-clang-format should not only work with Python2.7. This patch adds support for Python3. From 20175b753d8ef9f00f33fe04484dce1c0148ff7c Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier Date: Wed, 17 Aug 2016 10:55:32 +0200 Subject: Use 2to3 on git-clang-format. diff --git a/tools/clang-format/git-clang-format b/tools/clang-format/git-clang-format index 0c45762..a2d7e7a 100755 --- a/tools/clang-format/git-clang-format +++ b/tools/clang-format/git-clang-format @@ -20,9 +20,11 @@ clang-format on the changes in current files or a specific commit. For further details, run: git clang-format -h -Requires Python 2.7 +Requires a minimum of Python 2.7 """ +from __future__ import print_function + import argparse import collections import contextlib @@ -128,15 +130,15 @@ def main(): if opts.verbose >= 1: ignored_files.difference_update(changed_lines) if ignored_files: - print 'Ignoring changes in the following files (wrong extension):' + print('Ignoring changes in the following files (wrong extension):') for filename in ignored_files: -print ' ', filename +print(' ', filename) if changed_lines: - print 'Running clang-format on the following files:' + print('Running clang-format on the following files:') for filename in changed_lines: -print ' ', filename +print(' ', filename) if not changed_lines: -print 'no modified files to format' +print('no modified files to format') return # The computed diff outputs absolute paths, so we must cd before accessing # those files. @@ -146,20 +148,20 @@ def main(): binary=opts.binary, style=opts.style) if opts.verbose >= 1: -print 'old tree:', old_tree -print 'new tree:', new_tree +print('old tree:', old_tree) +print('new tree:', new_tree) if old_tree == new_tree: if opts.verbose >= 0: - print 'clang-format did not modify any files' + print('clang-format did not modify any files') elif opts.diff: print_diff(old_tree, new_tree) else: changed_files = apply_changes(old_tree, new_tree, force=opts.force, patch_mode=opts.patch) if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: - print 'changed files:' + print('changed files:') for filename in changed_files: -print ' ', filename +print(' ', filename) def load_git_config(non_string_options=None): @@ -298,7 +300,7 @@ def filter_by_extension(dictionary, allowed_extensions): `allowed_extensions` must be a collection of lowercase file extensions, excluding the period.""" allowed_extensions = frozenset(allowed_extensions) - for filename in dictionary.keys(): + for filename in list(dictionary.keys()): base_ext = filename.rsplit('.', 1) if len(base_ext) == 1 or base_ext[1].lower() not in allowed_extensions: del dictionary[filename] @@ -323,7 +325,7 @@ def run_clang_format_and_save_to_tree(changed_lines, binary='clang-format', Returns the object ID (SHA-1) of the created tree.""" def index_info_generator(): -for filename, line_ranges in changed_lines.iteritems(): +for filename, line_ranges in changed_lines.items(): mode = oct(os.stat(filename).st_mode) blob_id = clang_format_to_blob(filename, line_ranges, binary=binary, style=style) @@ -431,10 +433,10 @@ def apply_changes(old_tree, new_tree, force=False, patch_mode=False): if not force: unstaged_files = run('git', 'diff-files', '--name-status', *changed_files) if unstaged_files: - print >>sys.stderr, ('The following files would be modified but ' - 'have unstaged changes:') - print >>sys.stderr, unstaged_files - print >>sys.stderr, 'Please commit, stage, or stash them first.' + print(('The following files would be modified but ' + 'have unstaged changes:'), file=sys.stderr) + print(unstaged_files, file=sys.stderr) + print('Please commit, stage, or stash them first.', file=sys.stderr) sys.exit(2) if patch_mode: # In patch mode, we could just as well create an index from the new tree @@ -464,20 +466,20 @@ def run(*args, **kwargs): if p.returncode == 0: if stderr: if verbose: -print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args) - print >>sys.stderr, stderr.rstrip() +print('`%s` printed to stderr:' % ' '.join(args), file=sys.stderr) + print(stderr.rstrip(), file=sys.stderr) if strip: stdout = stdout.rstrip('\r\n') return stdout if verbose: -print >>sys.stderr, '`%s` returned %s' % (' '.join(args), p.returncode) +print('`%s` returned %s' %
[PATCH] D23602: Port tools/clang-format/git-clang-format to work Python beyond 2.7
abergmeier-dsfishlabs created this revision. abergmeier-dsfishlabs added a reviewer: djasper. abergmeier-dsfishlabs added a subscriber: cfe-commits. abergmeier-dsfishlabs set the repository for this revision to rL LLVM. abergmeier-dsfishlabs changed the edit policy of this Differential Revision from "All Users" to "abergmeier-dsfishlabs (Andreas Bergmeier)". Since Python3 may deliberately not be available on Linux Systems (to prevent people from writing non-futureproof code), git-clang-format should not only work with Python2.7. This patch adds support for Python3. Repository: rL LLVM https://reviews.llvm.org/D23602 Files: tools/clang-format/git-clang-format Index: tools/clang-format/git-clang-format === --- tools/clang-format/git-clang-format +++ tools/clang-format/git-clang-format @@ -20,9 +20,11 @@ For further details, run: git clang-format -h -Requires Python 2.7 +Requires a minimum of Python 2.7 """ +from __future__ import print_function + import argparse import collections import contextlib @@ -128,15 +130,15 @@ if opts.verbose >= 1: ignored_files.difference_update(changed_lines) if ignored_files: - print 'Ignoring changes in the following files (wrong extension):' + print('Ignoring changes in the following files (wrong extension):') for filename in ignored_files: -print ' ', filename +print(' ', filename) if changed_lines: - print 'Running clang-format on the following files:' + print('Running clang-format on the following files:') for filename in changed_lines: -print ' ', filename +print(' ', filename) if not changed_lines: -print 'no modified files to format' +print('no modified files to format') return # The computed diff outputs absolute paths, so we must cd before accessing # those files. @@ -146,20 +148,20 @@ binary=opts.binary, style=opts.style) if opts.verbose >= 1: -print 'old tree:', old_tree -print 'new tree:', new_tree +print('old tree:', old_tree) +print('new tree:', new_tree) if old_tree == new_tree: if opts.verbose >= 0: - print 'clang-format did not modify any files' + print('clang-format did not modify any files') elif opts.diff: print_diff(old_tree, new_tree) else: changed_files = apply_changes(old_tree, new_tree, force=opts.force, patch_mode=opts.patch) if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: - print 'changed files:' + print('changed files:') for filename in changed_files: -print ' ', filename +print(' ', filename) def load_git_config(non_string_options=None): @@ -298,7 +300,7 @@ `allowed_extensions` must be a collection of lowercase file extensions, excluding the period.""" allowed_extensions = frozenset(allowed_extensions) - for filename in dictionary.keys(): + for filename in list(dictionary.keys()): base_ext = filename.rsplit('.', 1) if len(base_ext) == 1 or base_ext[1].lower() not in allowed_extensions: del dictionary[filename] @@ -323,7 +325,7 @@ Returns the object ID (SHA-1) of the created tree.""" def index_info_generator(): -for filename, line_ranges in changed_lines.iteritems(): +for filename, line_ranges in changed_lines.items(): mode = oct(os.stat(filename).st_mode) blob_id = clang_format_to_blob(filename, line_ranges, binary=binary, style=style) @@ -431,10 +433,10 @@ if not force: unstaged_files = run('git', 'diff-files', '--name-status', *changed_files) if unstaged_files: - print >>sys.stderr, ('The following files would be modified but ' - 'have unstaged changes:') - print >>sys.stderr, unstaged_files - print >>sys.stderr, 'Please commit, stage, or stash them first.' + print(('The following files would be modified but ' + 'have unstaged changes:'), file=sys.stderr) + print(unstaged_files, file=sys.stderr) + print('Please commit, stage, or stash them first.', file=sys.stderr) sys.exit(2) if patch_mode: # In patch mode, we could just as well create an index from the new tree @@ -464,20 +466,20 @@ if p.returncode == 0: if stderr: if verbose: -print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args) - print >>sys.stderr, stderr.rstrip() +print('`%s` printed to
Re: [PATCH] D23602: Port tools/clang-format/git-clang-format to work Python beyond 2.7
abergmeier-dsfishlabs removed rL LLVM as the repository for this revision. abergmeier-dsfishlabs updated this revision to Diff 68487. abergmeier-dsfishlabs added a comment. Removed unnecessary list conversion. https://reviews.llvm.org/D23602 Files: tools/clang-format/git-clang-format Index: tools/clang-format/git-clang-format === --- tools/clang-format/git-clang-format +++ tools/clang-format/git-clang-format @@ -20,9 +20,11 @@ For further details, run: git clang-format -h -Requires Python 2.7 +Requires a minimum of Python 2.7 """ +from __future__ import print_function + import argparse import collections import contextlib @@ -128,15 +130,15 @@ if opts.verbose >= 1: ignored_files.difference_update(changed_lines) if ignored_files: - print 'Ignoring changes in the following files (wrong extension):' + print('Ignoring changes in the following files (wrong extension):') for filename in ignored_files: -print ' ', filename +print(' ', filename) if changed_lines: - print 'Running clang-format on the following files:' + print('Running clang-format on the following files:') for filename in changed_lines: -print ' ', filename +print(' ', filename) if not changed_lines: -print 'no modified files to format' +print('no modified files to format') return # The computed diff outputs absolute paths, so we must cd before accessing # those files. @@ -146,20 +148,20 @@ binary=opts.binary, style=opts.style) if opts.verbose >= 1: -print 'old tree:', old_tree -print 'new tree:', new_tree +print('old tree:', old_tree) +print('new tree:', new_tree) if old_tree == new_tree: if opts.verbose >= 0: - print 'clang-format did not modify any files' + print('clang-format did not modify any files') elif opts.diff: print_diff(old_tree, new_tree) else: changed_files = apply_changes(old_tree, new_tree, force=opts.force, patch_mode=opts.patch) if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: - print 'changed files:' + print('changed files:') for filename in changed_files: -print ' ', filename +print(' ', filename) def load_git_config(non_string_options=None): @@ -323,7 +325,7 @@ Returns the object ID (SHA-1) of the created tree.""" def index_info_generator(): -for filename, line_ranges in changed_lines.iteritems(): +for filename, line_ranges in changed_lines.items(): mode = oct(os.stat(filename).st_mode) blob_id = clang_format_to_blob(filename, line_ranges, binary=binary, style=style) @@ -431,10 +433,10 @@ if not force: unstaged_files = run('git', 'diff-files', '--name-status', *changed_files) if unstaged_files: - print >>sys.stderr, ('The following files would be modified but ' - 'have unstaged changes:') - print >>sys.stderr, unstaged_files - print >>sys.stderr, 'Please commit, stage, or stash them first.' + print(('The following files would be modified but ' + 'have unstaged changes:'), file=sys.stderr) + print(unstaged_files, file=sys.stderr) + print('Please commit, stage, or stash them first.', file=sys.stderr) sys.exit(2) if patch_mode: # In patch mode, we could just as well create an index from the new tree @@ -464,20 +466,20 @@ if p.returncode == 0: if stderr: if verbose: -print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args) - print >>sys.stderr, stderr.rstrip() +print('`%s` printed to stderr:' % ' '.join(args), file=sys.stderr) + print(stderr.rstrip(), file=sys.stderr) if strip: stdout = stdout.rstrip('\r\n') return stdout if verbose: -print >>sys.stderr, '`%s` returned %s' % (' '.join(args), p.returncode) +print('`%s` returned %s' % (' '.join(args), p.returncode), file=sys.stderr) if stderr: -print >>sys.stderr, stderr.rstrip() +print(stderr.rstrip(), file=sys.stderr) sys.exit(2) def die(message): - print >>sys.stderr, 'error:', message + print('error:', message, file=sys.stderr) sys.exit(2) ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D23602: Port tools/clang-format/git-clang-format to work Python beyond 2.7
abergmeier-dsfishlabs set the repository for this revision to rL LLVM. abergmeier-dsfishlabs updated this revision to Diff 68488. abergmeier-dsfishlabs added a comment. Proper patch of last version (hopefully) Repository: rL LLVM https://reviews.llvm.org/D23602 Files: tools/clang-format/git-clang-format Index: tools/clang-format/git-clang-format === --- tools/clang-format/git-clang-format +++ tools/clang-format/git-clang-format @@ -20,9 +20,11 @@ For further details, run: git clang-format -h -Requires Python 2.7 +Requires a minimum of Python 2.7 """ +from __future__ import print_function + import argparse import collections import contextlib @@ -128,15 +130,15 @@ if opts.verbose >= 1: ignored_files.difference_update(changed_lines) if ignored_files: - print 'Ignoring changes in the following files (wrong extension):' + print('Ignoring changes in the following files (wrong extension):') for filename in ignored_files: -print ' ', filename +print(' ', filename) if changed_lines: - print 'Running clang-format on the following files:' + print('Running clang-format on the following files:') for filename in changed_lines: -print ' ', filename +print(' ', filename) if not changed_lines: -print 'no modified files to format' +print('no modified files to format') return # The computed diff outputs absolute paths, so we must cd before accessing # those files. @@ -146,20 +148,20 @@ binary=opts.binary, style=opts.style) if opts.verbose >= 1: -print 'old tree:', old_tree -print 'new tree:', new_tree +print('old tree:', old_tree) +print('new tree:', new_tree) if old_tree == new_tree: if opts.verbose >= 0: - print 'clang-format did not modify any files' + print('clang-format did not modify any files') elif opts.diff: print_diff(old_tree, new_tree) else: changed_files = apply_changes(old_tree, new_tree, force=opts.force, patch_mode=opts.patch) if (opts.verbose >= 0 and not opts.patch) or opts.verbose >= 1: - print 'changed files:' + print('changed files:') for filename in changed_files: -print ' ', filename +print(' ', filename) def load_git_config(non_string_options=None): @@ -323,7 +325,7 @@ Returns the object ID (SHA-1) of the created tree.""" def index_info_generator(): -for filename, line_ranges in changed_lines.iteritems(): +for filename, line_ranges in changed_lines.items(): mode = oct(os.stat(filename).st_mode) blob_id = clang_format_to_blob(filename, line_ranges, binary=binary, style=style) @@ -431,10 +433,10 @@ if not force: unstaged_files = run('git', 'diff-files', '--name-status', *changed_files) if unstaged_files: - print >>sys.stderr, ('The following files would be modified but ' - 'have unstaged changes:') - print >>sys.stderr, unstaged_files - print >>sys.stderr, 'Please commit, stage, or stash them first.' + print(('The following files would be modified but ' + 'have unstaged changes:'), file=sys.stderr) + print(unstaged_files, file=sys.stderr) + print('Please commit, stage, or stash them first.', file=sys.stderr) sys.exit(2) if patch_mode: # In patch mode, we could just as well create an index from the new tree @@ -464,20 +466,20 @@ if p.returncode == 0: if stderr: if verbose: -print >>sys.stderr, '`%s` printed to stderr:' % ' '.join(args) - print >>sys.stderr, stderr.rstrip() +print('`%s` printed to stderr:' % ' '.join(args), file=sys.stderr) + print(stderr.rstrip(), file=sys.stderr) if strip: stdout = stdout.rstrip('\r\n') return stdout if verbose: -print >>sys.stderr, '`%s` returned %s' % (' '.join(args), p.returncode) +print('`%s` returned %s' % (' '.join(args), p.returncode), file=sys.stderr) if stderr: -print >>sys.stderr, stderr.rstrip() +print(stderr.rstrip(), file=sys.stderr) sys.exit(2) def die(message): - print >>sys.stderr, 'error:', message + print('error:', message, file=sys.stderr) sys.exit(2) ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits