tools: moveconfig: improve header cleanup feature
cover tools: moveconfig: improve header cleanup feature
Masahiro Yamada (4):
tools: moveconfig: trim garbage lines after header cleanups
tools: moveconfig: show result of header cleaning in unified diff
tools: moveconfig: show result of header cleaning in color mode
tools: moveconfig: fix cleanup of defines across multiple lines
tools/moveconfig.py | 93 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 11 deletions(-)
Signature · 1 line
1.9.1
PATCH 1/4 tools: moveconfig: trim garbage lines after header cleanups
The tools/moveconfig.py has a feature to cleanup #define/#undef's of moved config options, but I want this tool to do a better job.
For example, when we are moving CONFIG_FOO and its define is surrounded by #ifdef ... #endif, like follows:
#ifdef CONFIG_BAR # define CONFIG_FOO #endif
The header cleanup will leave empty #ifdef ... #endif:
#ifdef CONFIG_BAR #endif
Likewise, if a define line between two blank lines
<blank line> #define CONFIG_FOO <blank lines.
... is deleted, the result of the clean-up will be successive empty lines, which is a coding-style violation.
It is tedious to remove left-over garbage lines manually, so I want the tool to take care of this. The tool's job is still not perfect, so we should check the output of the tool, but I hope our life will be much easier with this patch.
---
tools/moveconfig.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-)
tools/moveconfig.py +62 -4
@@ -160,6 +160,7 @@ To see the complete list of supported options, run """ +import copy import filecmp import fnmatch import multiprocessing
@@ -319,6 +320,41 @@ def update_cross_compile(color_enabled): CROSS_COMPILE[arch] = cross_compile +def extend_matched_lines(lines, matched, pre_pattern, post_pattern, extend_pre, + extend_post): + """Extend matched lines if desired patterns are found before/after already + matched lines. + + Arguments: + lines: A list of lines handled. + matched: A list of line numbers that have been already matched. + (will be updated by this function) + pre_pattern: Regular expression that should be matched as preamble. + post_pattern: Regular expression that should be matched as postamble. + extend_pre: Add the line number of matched preamble to the matched list. + extend_post: Add the line number of matched postamble to the matched list. + """ + extended_matched = [] + + j = matched[0] + + for i in matched: + if i == 0 or i < j: + continue + j = i + while j in matched: + j += 1 + if j >= len(lines): + break + if pre_pattern.search(lines[i - 1]) and post_pattern.search(lines[j]): + if extend_pre: + extended_matched.append(i - 1) + if extend_post: + extended_matched.append(j) + + matched += extended_matched + matched.sort() + def cleanup_one_header(header_path, patterns, dry_run): """Clean regex-matched lines away from a file.
@@ -334,13 +370,35 @@ def cleanup_one_header(header_path, patterns, dry_run): matched = [] for i, line in enumerate(lines): for pattern in patterns: - m = pattern.search(line) - if m: - print '%s: %s: %s' % (header_path, i + 1, line), + if pattern.search(line): matched.append(i) break - if dry_run or not matched: + if not matched: + return + + # remove empty #ifdef ... #endif, successive blank lines + pattern_ifdef = re.compile(r'#\s*if(def|ndef)?\s') + pattern_endif = re.compile(r'#\s*endif\s') + pattern_blank = re.compile(r'^\s*$') + + while True: + old_matched = copy.copy(matched) + extend_matched_lines(lines, matched, pattern_ifdef, pattern_endif, + True, True) + extend_matched_lines(lines, matched, pattern_ifdef, pattern_blank, + False, True) + extend_matched_lines(lines, matched, pattern_blank, pattern_endif, + True, False) + extend_matched_lines(lines, matched, pattern_blank, pattern_blank, + True, False) + if matched == old_matched: + break + + for i in matched: + print '%s: %s: %s' % (header_path, i + 1, lines[i]), + + if dry_run: return with open(header_path, 'w') as f:
Signature · 1 line
1.9.1
PATCH 1/4 tools: moveconfig: trim garbage lines after header cleanups
Quoted 15 lines · Masahiro Yamada
The
tools/moveconfig.pyhas a feature to cleanup #define/#undef's of moved config options, but I want this tool to do a better job.For example, when we are moving
CONFIG_FOOand its define is surrounded by #ifdef ... #endif, like follows:#ifdef CONFIG_BAR # define CONFIG_FOO #endifThe header cleanup will leave empty #ifdef ... #endif:
#ifdef CONFIG_BAR #endif
Quoted 8 lines
Likewise, if a define line between two blank lines
<blank line> #define CONFIG_FOO <blank lines.... is deleted, the result of the clean-up will be successive empty lines, which is a coding-style violation.
Thanks! This is mildly annoying to find/fixup with regex, which is how I usually do it. A few comments / questions:
[snip]
tools/moveconfig.py def cleanup_one_header(header_path, patterns, dry_run): · line 381
Quoted 2 lines
tools/moveconfig.py +2
-0+ # remove empty #ifdef ... #endif, successive blank lines + pattern_ifdef = re.compile(r'#\s*if(def|ndef)?\s')
It starts to get complex to catch if defined/elif defined cases, so we should just leave that to manual review, yes?
tools/moveconfig.py def cleanup_one_header(header_path, patterns, dry_run): · line 383
Quoted 2 lines
tools/moveconfig.py +2
-0+ pattern_endif = re.compile(r'#\s*endif\s') + pattern_blank = re.compile(r'^\s*$')
The last pattern will catch multiple blank lines, right?
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: Digital signature URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160724/5ce3b785/attachment.sig>
PATCH 1/4 tools: moveconfig: trim garbage lines after header cleanups
Hi Tom,
2016-07-25 0:19 GMT+09:00 Tom Rini <[email protected]>:
Quoted 29 lines · Masahiro Yamada
Quoted 16 lines
The
tools/moveconfig.pyhas a feature to cleanup #define/#undef's of moved config options, but I want this tool to do a better job.For example, when we are moving
CONFIG_FOOand its define is surrounded by #ifdef ... #endif, like follows:#ifdef CONFIG_BAR # define CONFIG_FOO #endifThe header cleanup will leave empty #ifdef ... #endif:
#ifdef CONFIG_BAR #endifQuoted 8 lines
Likewise, if a define line between two blank lines
<blank line> #define CONFIG_FOO <blank lines.... is deleted, the result of the clean-up will be successive empty lines, which is a coding-style violation.
Thanks! This is mildly annoying to find/fixup with regex, which is how I usually do it. A few comments / questions:
Quoted 6 lines
[snip]
tools/moveconfig.py def cleanup_one_header(header_path, patterns, dry_run): · line 381
Quoted 2 lines
tools/moveconfig.py +2
-0+ # remove empty #ifdef ... #endif, successive blank lines + pattern_ifdef = re.compile(r'#\s*if(def|ndef)?\s')It starts to get complex to catch if defined/elif defined cases, so we should just leave that to manual review, yes?
It was missing from my thought.
It is difficult to invert the logic, like changing ifdef to ifndef, but just removing can be done easily.
In v2,
"tools/moveconfig -H -c FOO" can clean more!
ex1)
#if CONFIG_BAR
/* ... */
-#elif CONFIG_BAZ
-# define CONFIG_FOO
#endifex2)
#if CONFIG_BAR
/* ... */
-#elif CONFIG_BAZ
-# define CONFIG_FOO
#elif CONFIG_BAZ2
/* ... */#endif
Quoted 4 lines
tools/moveconfig.py def cleanup_one_header(header_path, patterns, dry_run): · line 383
Quoted 2 lines
tools/moveconfig.py +2
-0+ pattern_endif = re.compile(r'#\s*endif\s') + pattern_blank = re.compile(r'^\s*$')The last pattern will catch multiple blank lines, right?
Yes.
Signature · 2 lines
Best Regards Masahiro Yamada
PATCH 2/4 tools: moveconfig: show result of header cleaning in unified diff
In reply to Masahiro YamadaThe header cleanup feature of this tool now removes empty ifdef's, successive blank lines as well as moved option defines. So, we want to see a little more context to check which lines were deleted.
It is true that we can see it by "git diff", but it would not work in the --dry-run mode. So, here, this commit.
---
tools/moveconfig.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
tools/moveconfig.py +14 -5
@@ -161,6 +161,7 @@ To see the complete list of supported options, run """ import copy +import difflib import filecmp import fnmatch import multiprocessing
@@ -395,16 +396,24 @@ def cleanup_one_header(header_path, patterns, dry_run): if matched == old_matched: break - for i in matched: - print '%s: %s: %s' % (header_path, i + 1, lines[i]), + tolines = copy.copy(lines) + + for i in reversed(matched): + tolines.pop(i) + + diff = difflib.unified_diff(lines, tolines, + fromfile=os.path.join('a', header_path), + tofile=os.path.join('b', header_path)) + + for line in diff: + print line, if dry_run: return with open(header_path, 'w') as f: - for i, line in enumerate(lines): - if not i in matched: - f.write(line) + for line in tolines: + f.write(line) def cleanup_headers(configs, dry_run): """Delete config defines from board headers.
Signature · 1 line
1.9.1
PATCH 3/4 tools: moveconfig: show result of header cleaning in color mode
In reply to Masahiro YamadaThis commit makes the tool show the cleaned lines in red if the --color option is given.
---
tools/moveconfig.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
tools/moveconfig.py +7 -6
@@ -356,7 +356,7 @@ def extend_matched_lines(lines, matched, pre_pattern, post_pattern, extend_pre, matched += extended_matched matched.sort() -def cleanup_one_header(header_path, patterns, dry_run): +def cleanup_one_header(header_path, patterns, options): """Clean regex-matched lines away from a file. Arguments:
@@ -406,16 +406,17 @@ def cleanup_one_header(header_path, patterns, dry_run): tofile=os.path.join('b', header_path)) for line in diff: - print line, + print color_text(options.color and line[0] == '-' and line[1] != '-', + COLOR_LIGHT_RED, line), - if dry_run: + if options.dry_run: return with open(header_path, 'w') as f: for line in tolines: f.write(line) -def cleanup_headers(configs, dry_run): +def cleanup_headers(configs, options): """Delete config defines from board headers. Arguments:
@@ -441,7 +442,7 @@ def cleanup_headers(configs, dry_run): for filename in filenames: if not fnmatch.fnmatch(filename, '*~'): cleanup_one_header(os.path.join(dirpath, filename), - patterns, dry_run) + patterns, options) ### classes ### class Progress:
@@ -1115,7 +1116,7 @@ def main(): move_config(configs, options) if configs: - cleanup_headers(configs, options.dry_run) + cleanup_headers(configs, options) if __name__ == '__main__': main()
Signature · 1 line
1.9.1
PATCH 4/4 tools: moveconfig: fix cleanup of defines across multiple lines
In reply to Masahiro YamadaCorrect the clean-up of such defines that continue across multiple lines, like follows:
#define CONFIG_FOO "this continues to the next line " \
"this line should be removed too" \
"this line should be removed as well"---
tools/moveconfig.py | 3 +++ 1 file changed, 3 insertions(+)
tools/moveconfig.py +3 -0
@@ -370,6 +370,9 @@ def cleanup_one_header(header_path, patterns, options): matched = [] for i, line in enumerate(lines): + if i - 1 in matched and lines[i - 1][-2:] == '\\\n': + matched.append(i) + continue for pattern in patterns: if pattern.search(line): matched.append(i)
Signature · 1 line
1.9.1