Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2015 gRPC authors. |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 15 | |
| 16 | """Change comments style of source files from // to /** */""" |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 17 | |
| 18 | import re |
| 19 | import sys |
| 20 | |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 21 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 22 | if len(sys.argv) < 2: |
| 23 | print("Please provide at least one source file name as argument.") |
Jorge Canizales | 55e9742 | 2015-10-26 15:14:47 -0700 | [diff] [blame] | 24 | sys.exit() |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 25 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 26 | for file_name in sys.argv[1:]: |
| 27 | |
| 28 | print("Modifying format of {file} comments in place...".format( |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 29 | file=file_name, |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 30 | )) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 31 | |
| 32 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 33 | # Input |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 34 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 35 | with open(file_name, "r") as input_file: |
| 36 | lines = input_file.readlines() |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 37 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 38 | def peek(): |
| 39 | return lines[0] |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 40 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 41 | def read_line(): |
| 42 | return lines.pop(0) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 43 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 44 | def more_input_available(): |
| 45 | return lines |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 46 | |
| 47 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 48 | # Output |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 49 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 50 | output_lines = [] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 51 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 52 | def write(line): |
| 53 | output_lines.append(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 54 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 55 | def flush_output(): |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 56 | with open(file_name, "w") as output_file: |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 57 | for line in output_lines: |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 58 | output_file.write(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 59 | |
| 60 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 61 | # Pattern matching |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 62 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 63 | comment_regex = r'^(\s*)//\s(.*)$' |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 64 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 65 | def is_comment(line): |
| 66 | return re.search(comment_regex, line) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 67 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 68 | def isnt_comment(line): |
| 69 | return not is_comment(line) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 70 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 71 | def next_line(predicate): |
Jorge Canizales | 1bef95d | 2015-10-26 13:29:09 -0700 | [diff] [blame] | 72 | return more_input_available() and predicate(peek()) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 73 | |
| 74 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 75 | # Transformation |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 76 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 77 | def indentation_of(line): |
| 78 | match = re.search(comment_regex, line) |
| 79 | return match.group(1) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 80 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 81 | def content(line): |
| 82 | match = re.search(comment_regex, line) |
| 83 | return match.group(2) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 84 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 85 | def format_as_block(comment_block): |
| 86 | if len(comment_block) == 0: |
| 87 | return [] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 88 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 89 | indent = indentation_of(comment_block[0]) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 90 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 91 | if len(comment_block) == 1: |
| 92 | return [indent + "/** " + content(comment_block[0]) + " */\n"] |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 93 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 94 | block = ["/**"] + [" * " + content(line) for line in comment_block] + [" */"] |
| 95 | return [indent + line.rstrip() + "\n" for line in block] |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 96 | |
| 97 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 98 | # Main algorithm |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 99 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 100 | while more_input_available(): |
| 101 | while next_line(isnt_comment): |
| 102 | write(read_line()) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 103 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 104 | comment_block = [] |
| 105 | # Get all lines in the same comment block. We could restrict the indentation |
| 106 | # to be the same as the first line of the block, but it's probably ok. |
| 107 | while (next_line(is_comment)): |
| 108 | comment_block.append(read_line()) |
Jorge Canizales | fef5bee | 2015-10-19 12:24:28 -0700 | [diff] [blame] | 109 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 110 | for line in format_as_block(comment_block): |
| 111 | write(line) |
Jorge Canizales | 88c3284 | 2015-10-26 10:18:59 -0700 | [diff] [blame] | 112 | |
Jorge Canizales | bbb7774 | 2015-10-26 10:44:38 -0700 | [diff] [blame] | 113 | flush_output() |