[RFC,1/3,gdb/contrib] Add refactor.py

Message ID 20230125200626.29340-2-tdevries@suse.de
State Committed
Headers
Series Introduce is_x86_64_m64_target |

Commit Message

Tom de Vries Jan. 25, 2023, 8:06 p.m. UTC
  Add a refactoring script gdb/contrib/refactor.py that takes a transformation
script as argument, for instance a script gdb/contrib/transform.py, like so:
...
$ ./gdb/contrib/refactor.py transform
...
---
 gdb/contrib/refactor.py | 73 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100755 gdb/contrib/refactor.py
  

Patch

diff --git a/gdb/contrib/refactor.py b/gdb/contrib/refactor.py
new file mode 100755
index 00000000000..fe2fa019051
--- /dev/null
+++ b/gdb/contrib/refactor.py
@@ -0,0 +1,73 @@ 
+#! /usr/bin/env python3
+
+# Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This file is part of GDB.
+#
+# This program 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 of the License, or
+# (at your option) any later version.
+#
+# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import sys
+import re
+
+transform_file = sys.argv[1]
+transform_file = re.sub(r"\.py$", r"", transform_file)
+
+transformation = __import__(transform_file)
+
+# Define scope of refactoring.
+
+# Sources.
+#dirs = ["gdb", "gdbserver", "gdbsupport"]
+#avoid_dir = "/testsuite/"
+#exts = [".c", ".cc", ".h"]
+
+# Testsuite.
+#dirs = ["gdb/testsuite"]
+#avoid_dir = None
+#exts = [".exp"]
+
+# In transformation file.
+dirs=transformation.dirs
+avoid_dir=transformation.avoid_dir
+exts=transformation.exts
+
+def handle_file(filename):
+    file = open(filename, 'r+')
+    data = file.read()
+    file.close()
+
+    transformation.have_match = False
+    data = transformation.transform(data)
+    if transformation.have_match:
+        file = open(filename, 'w')
+        file.write(data)
+        file.close()
+
+def main():
+    for dir in dirs:
+        for walk_root, walk_dirs, walk_files in os.walk(dir):
+            for file in walk_files:
+                full = os.path.join(walk_root, file)
+                if avoid_dir != None and avoid_dir in full:
+                    continue
+                found = False
+                for ext in exts:
+                    if file.endswith(ext):
+                        found = True
+                        break
+                if found:
+                    handle_file(full)
+
+main()