We will need this to automagically find MAINTAINERS. Use the same
logic as the perl script by looking for expected files and
directories.

Signed-off-by: Alex Bennée <[email protected]>
---
 scripts/get_maintainer.py | 77 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/scripts/get_maintainer.py b/scripts/get_maintainer.py
index 2b8fc7ae9ee..c713f290cc7 100755
--- a/scripts/get_maintainer.py
+++ b/scripts/get_maintainer.py
@@ -15,6 +15,59 @@
 from pathlib import Path
 
 
+#
+# Helper functions for dealing with the source path
+#
+
+def is_qemu_src_root(directory):
+    """
+    Checks if the given path appears to be the root of a QEMU source tree,
+    based on the presence of key files and directories.
+    """
+    if not path.isdir(directory):
+        return False
+
+    required_files = ['COPYING', 'MAINTAINERS', 'Makefile', 'VERSION']
+    required_dirs = ['docs', 'linux-user', 'system']
+
+    for f in required_files:
+        if not path.isfile(path.join(directory, f)):
+            return False
+    for d in required_dirs:
+        if not path.isdir(path.join(directory, d)):
+            return False
+    return True
+
+
+def find_src_root():
+    """
+    Walks up the directory tree from the script's location
+    to find the QEMU source root.
+    Returns the absolute path of the root directory if found, or None.
+    """
+    script_dir = path.dirname(path.abspath(__file__))
+    current_dir = script_dir
+
+    while True:
+        if is_qemu_src_root(current_dir):
+            return current_dir
+
+        # Move up to the parent directory
+        parent_dir = path.dirname(current_dir)
+
+        # If we reached the filesystem root and haven't found it, stop
+        if parent_dir == current_dir:
+            break
+
+        current_dir = parent_dir
+
+    return None
+
+#
+# Argument validation
+#
+
+
 def valid_file_path(arg):
     """
     Checks if arg exists and is a regular file or raises ArgumentTypeError.
@@ -26,7 +79,21 @@ def valid_file_path(arg):
     return Path(path.abspath(arg))
 
 
+def valid_src_root(arg):
+    """
+    Checks if arg is a valid QEMU source root or raise ArgumentTypeError.
+    """
+    abs_path = path.abspath(arg)
+    if not is_qemu_src_root(abs_path):
+        raise ArgumentTypeError(f"Path '{arg}' is not a valid QEMU source 
tree")
+    return abs_path
+
+
 def main():
+    """
+    Main entry point for the script.
+    """
+
     parser = ArgumentParser(description='Extract maintainer information. ')
 
     # We can either specify patches or an individual file
@@ -36,6 +103,16 @@ def main():
     group.add_argument('-f', '--file', type=valid_file_path,
                        help='path to source file')
 
+    # We need to know or be told where the root of the source tree is
+    src = find_src_root()
+
+    if src is None:
+        parser.add_argument('--src', type=valid_src_root, required=True,
+                            help='Root of QEMU source tree')
+    else:
+        parser.add_argument('--src', type=valid_src_root, default=src,
+                            help=f'Root of QEMU source tree (default: {src})')
+
     args = parser.parse_args()
 
 
-- 
2.47.3


Reply via email to