Last active
August 29, 2015 14:11
-
-
Save legendmohe/8e3a0905c4dc1455f63a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
# Copyright 2014 Xinyu, He <legendmohe@foxmail.com> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
import os | |
import re | |
import optparse | |
g_file_ext = ".java" | |
g_reg_log_begin = re.compile(r'(public|protected|private|static) +[\w\<\>\[\]]+\s+(\w+) *\((.*[^;]$)') | |
g_reg_log_search = re.compile(r'(public|protected|private|static) +[\w\<\>\[\]]+\s+(\w+) *\(([^\)]*)\) *(throws\s+\w+)?\s*(\{?|[^;])') | |
def find_log_line(reg, line): | |
mat = reg.search(line) | |
if mat is not None: | |
groups = mat.groups() | |
return groups | |
def format_log_line(log_line): | |
ret = "//add log\n" | |
ret += "Log.d(TAG, \"" | |
ret += "[m] %s %s():\\n" % (log_line[0], log_line[1]) | |
for param in re.split("\s*,\s*", log_line[2]): | |
s_p = re.split("\s+", param) | |
if len(s_p) > 1: | |
# final int foo <- s_p[-1] | |
ret += " [p] %s:\" + %s + \"\\n" % (s_p[-1], s_p[-1]) | |
ret += "\");" | |
return ret + '\n\n' | |
def add_log_to_file(file_path): | |
print "add log to file:", file_path | |
log_num = 0 | |
content = "" | |
with open(file_path, "r") as f: | |
lines = f.read().splitlines() | |
i = 0 | |
while i < len(lines): | |
log_line = find_log_line(g_reg_log_begin, lines[i]) | |
# ignore the constructor and invaild line | |
if log_line is None or len(log_line) == 0: | |
content += lines[i] + "\n" | |
i += 1 | |
continue | |
log_num += 1 | |
target = "" | |
if not "{" in lines[i]: | |
# find { | |
while i < len(lines) and not "{" in lines[i]: | |
content += lines[i] + "\n" | |
target += lines[i].strip() | |
i += 1 | |
target += lines[i].strip() | |
print "multiline: ", target | |
else: | |
target = lines[i] | |
print "singleline: ", lines[i] | |
content += lines[i] + "\n" | |
content += format_log_line(find_log_line(g_reg_log_search, target)) + "\n" | |
i += 1 | |
res_file = file_path + '.add_log' | |
if log_num != 0: | |
print "write res to:", res_file | |
with open(res_file, "w") as f: | |
f.write(content) | |
print "done. rows changed:", log_num | |
if __name__ == "__main__": | |
parser = optparse.OptionParser() | |
parser.add_option('-p', '--path', | |
action="store", dest="rootdir", | |
help="src path", default=".") | |
options, args = parser.parse_args() | |
rootdir = options.rootdir | |
print 'src path:', rootdir | |
for subdir, dirs, files in os.walk(rootdir): | |
for file in files: | |
if(file.endswith(g_file_ext)): | |
file_path = os.path.join(subdir, file) | |
add_log_to_file(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment