From patchwork Mon Jul 25 10:15:24 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 72704 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1064251qga; Mon, 25 Jul 2016 03:14:41 -0700 (PDT) X-Received: by 10.28.35.193 with SMTP id j184mr38037531wmj.33.1469441681438; Mon, 25 Jul 2016 03:14:41 -0700 (PDT) Return-Path: Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id qw7si14169748wjb.6.2016.07.25.03.14.41; Mon, 25 Jul 2016 03:14:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) client-ip=85.214.87.163; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 73443A77A3; Mon, 25 Jul 2016 12:14:32 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id gJbDxhZF5wv7; Mon, 25 Jul 2016 12:14:32 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id B876AA7769; Mon, 25 Jul 2016 12:14:23 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 47E82A778C for ; Mon, 25 Jul 2016 12:14:21 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id NqKz2jDUHREw for ; Mon, 25 Jul 2016 12:14:21 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from conuserg-11.nifty.com (conuserg-11.nifty.com [210.131.2.78]) by theia.denx.de (Postfix) with ESMTPS id C076DA7778 for ; Mon, 25 Jul 2016 12:14:17 +0200 (CEST) Received: from beagle.diag.org (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-11.nifty.com with ESMTP id u6PADhxF021296; Mon, 25 Jul 2016 19:13:45 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-11.nifty.com u6PADhxF021296 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1469441626; bh=ezsqydhFIy+iIW9yGtdZnvdpGCYclYEjSBBaCrqtbOA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xLZ64ezYd4+v1g77cgxmnvr3z9EQrxZUSuxqMMQL4L+xnsb4bxI+miWCuudiEkiQ3 rWZF0e82CwVsHbB6xxuzT9azLY7/W9us2eC79cJA9MtqywDGqYB63KBjl4n6Y0sQOC 8wjxlXMC6mdVazd9vMMEEVBHKYqgDliTz2vVYDeOQd9caI6Sp0DW3WunOMsUV4V/Ao 0OQYrWDfdZdp6XHvpEPJBbkDDKdfWqYWRm+54beT7TQAiF+h7nJ4yRoO7fYIWI2sz0 oxa3vE1Mqd+eCvK/JukmB4dV+Rr04Sjy2K2uEGTbhrriJvIUjiotsGS1XYPFnN5BTE cnnu7PQoaTg6w== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: u-boot@lists.denx.de Date: Mon, 25 Jul 2016 19:15:24 +0900 Message-Id: <1469441729-5817-4-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1469441729-5817-1-git-send-email-yamada.masahiro@socionext.com> References: <1469441729-5817-1-git-send-email-yamada.masahiro@socionext.com> Cc: Tom Rini , Joe Hershberger Subject: [U-Boot] [PATCH v2 3/8] tools: moveconfig: trim garbage lines after header cleanups X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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 #define CONFIG_FOO --- Changes in v2: - cleanup between #elif ... #endif, #elif ... #elif tools/moveconfig.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 4 deletions(-) -- 1.9.1 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot diff --git a/tools/moveconfig.py b/tools/moveconfig.py index 05a581f..03e4953 100755 --- a/tools/moveconfig.py +++ b/tools/moveconfig.py @@ -160,6 +160,7 @@ To see the complete list of supported options, run """ +import copy import filecmp import fnmatch import multiprocessing @@ -319,6 +320,57 @@ def update_cross_compile(color_enabled): CROSS_COMPILE[arch] = cross_compile +def extend_matched_lines(lines, matched, pre_patterns, post_patterns, 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_patterns: A list of regular expression that should be matched as + preamble. + post_patterns: A list of 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 + + for p in pre_patterns: + if p.search(lines[i - 1]): + break + else: + # not matched + continue + + for p in post_patterns: + if p.search(lines[j]): + break + else: + # not matched + continue + + 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 +386,38 @@ 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_if = re.compile(r'#\s*if(def|ndef)?\W') # #if, #ifdef, #ifndef + pattern_elif = re.compile(r'#\s*el(if|se)\W') # #elif, #else + pattern_endif = re.compile(r'#\s*endif\W') # #endif + pattern_blank = re.compile(r'^\s*$') # empty line + + while True: + old_matched = copy.copy(matched) + extend_matched_lines(lines, matched, [pattern_if], + [pattern_endif], True, True) + extend_matched_lines(lines, matched, [pattern_elif], + [pattern_elif, pattern_endif], True, False) + extend_matched_lines(lines, matched, [pattern_if, pattern_elif], + [pattern_blank], False, True) + extend_matched_lines(lines, matched, [pattern_blank], + [pattern_elif, 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: