From patchwork Wed Oct 20 08:41:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Martin_Li=C5=A1ka?= X-Patchwork-Id: 46426 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC9513857C6E for ; Wed, 20 Oct 2021 08:41:24 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 96EC33858402 for ; Wed, 20 Oct 2021 08:41:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 96EC33858402 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7912721A74 for ; Wed, 20 Oct 2021 08:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1634719267; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=G10VLj7n+pIoPF1vRRLHvQzffNmCC7Koaq4IvuQodso=; b=KejVdeSkJSwg2gKbcXZgEQivp4sFTIIsInV4vDZw4Y6lJbWf2gOfXxJOdEcpnLmd7hsWWt sw9O2KcF3bWFxbxXYSWPfBCg+HOhu1U6Q77XkgxgNZuKWaDyOL4FmCwsZ9oBVs1lHv0f0Q w5i9cVyzCZIj+Rbt83exuAOIkCGTmwc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1634719267; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=G10VLj7n+pIoPF1vRRLHvQzffNmCC7Koaq4IvuQodso=; b=cOL7qu3SdmHKkurZiNu4d0DrBdkoA3bPlTZGmVSx9ucbEo1lHgwU1r+gORmtWgEQh49xzR 3VrWMLAf6j7spEDA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 67D8A13F7C for ; Wed, 20 Oct 2021 08:41:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id uaVeFyPWb2FUZwAAMHmgww (envelope-from ) for ; Wed, 20 Oct 2021 08:41:07 +0000 Message-ID: <01a68ba6-fffc-4ca3-135d-791441e666fd@suse.cz> Date: Wed, 20 Oct 2021 10:41:07 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 From: =?utf-8?q?Martin_Li=C5=A1ka?= Subject: [PATCH] Add new replace script. To: gcc-patches@gcc.gnu.org Content-Language: en-US X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hello. Sometimes, one needs a more complex replacement in source files and an editor can be a weak tool. That has happened to me in the recent time and so I made the replacement in Python. The script provides simple API: - handle_file_p - return True if you want to touch the file - modify_line - return modified line (if you want) Are others also interested or should I put it to my internal tools? Cheers, Martin contrib/ChangeLog: * replace.py: New file. --- contrib/replace.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 contrib/replace.py diff --git a/contrib/replace.py b/contrib/replace.py new file mode 100755 index 00000000000..a510c6cd32c --- /dev/null +++ b/contrib/replace.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021 Free Software Foundation, Inc. +# +# This file is part of GCC. +# +# GCC is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GCC is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GCC; see the file COPYING. If not, write to +# the Free Software Foundation, 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. +# +# The script can be used for more complex replacements in the source code. +# + +import argparse +import os +import re +import sys + +EXTENSIONS = ('.h', '.c', '.cc', '.C') + + +def handle_file_p(filename): + if 'testsuite' in filename: + return False + if all(not filename.endswith(ext) for ext in EXTENSIONS): + return False + + return True + + +def modify_line(line): + # Example replacement: + # m = re.match(r'.*time_function\(&([^,]*),', line) + # if m: + # e = m.end(1) + # name = m.group(1) + # line = line[:e + 1] + f' "{name}",' + line[e + 1:] + + return line + + +parser = argparse.ArgumentParser(description='Make a custom replacements ' + 'for source files') +parser.add_argument('directory', help='Root directory') +parser.add_argument('-v', '--verbose', action='store_true', + help='Verbose output') +args = parser.parse_args() + +visited_files = 0 +modified_files = 0 + +for root, _, files in os.walk(sys.argv[1]): + for file in files: + full = os.path.join(root, file) + if not handle_file_p(full): + continue + + visited_files += 1 + + modified = False + try: + modified_lines = [] + with open(full) as f: + lines = f.readlines() + for line in lines: + modified_line = modify_line(line) + if line != modified_line: + modified = True + modified_lines.append(line) + if modified: + with open(full, 'w') as w: + w.write('\n'.join(modified_lines)) + modified_files += 1 + if args.verbose: + print(f'File modified: {full}') + except UnicodeDecodeError as e: + print(f'Skipping file: {full} ({e})') + + +print(f'Visited files: {visited_files}') +print(f'Modified files: {modified_files}')