Procházet zdrojové kódy

Add diagnose command

Fixes #1835
Jelmer Vernooij před 2 měsíci
rodič
revize
a5a4691f52
3 změnil soubory, kde provedl 94 přidání a 0 odebrání
  1. 5 0
      NEWS
  2. 67 0
      dulwich/cli.py
  3. 22 0
      tests/test_cli.py

+ 5 - 0
NEWS

@@ -10,6 +10,11 @@
 
  * Add support for column formatting. (Jelmer Vernooij, #1837)
 
+ * Add ``dulwich diagnose`` command to display diagnostic information about
+   the Python environment including Python version, PYTHONPATH, sys.path,
+   Dulwich version, and installed dependencies with their versions.
+   (Jelmer Vernooij, #1835)
+
 0.24.8	2025-10-29
 
  * Add Rust implementation of pack delta creation (create_delta). The

+ 67 - 0
dulwich/cli.py

@@ -4082,6 +4082,72 @@ class cmd_describe(Command):
         logger.info(porcelain.describe("."))
 
 
+class cmd_diagnose(Command):
+    """Display diagnostic information about the Python environment."""
+
+    def run(self, args: Sequence[str]) -> None:
+        """Execute the diagnose command.
+
+        Args:
+            args: Command line arguments
+        """
+        # TODO: Support creating zip files with diagnostic information
+        parser = argparse.ArgumentParser()
+        parser.parse_args(args)
+
+        # Python version and executable
+        logger.info("Python version: %s", sys.version)
+        logger.info("Python executable: %s", sys.executable)
+
+        # PYTHONPATH
+        pythonpath = os.environ.get("PYTHONPATH", "")
+        if pythonpath:
+            logger.info("PYTHONPATH: %s", pythonpath)
+        else:
+            logger.info("PYTHONPATH: (not set)")
+
+        # sys.path
+        logger.info("sys.path:")
+        for path_entry in sys.path:
+            logger.info("  %s", path_entry)
+
+        # Dulwich version
+        try:
+            import dulwich
+
+            logger.info("Dulwich version: %s", dulwich.__version__)
+        except AttributeError:
+            logger.info("Dulwich version: (unknown)")
+
+        # List installed dependencies and their versions
+        logger.info("Installed dependencies:")
+
+        # Core dependencies
+        dependencies = [
+            ("urllib3", "core"),
+            ("typing_extensions", "core (Python < 3.12)"),
+        ]
+
+        # Optional dependencies
+        optional_dependencies = [
+            ("fastimport", "fastimport"),
+            ("gpg", "pgp"),
+            ("paramiko", "paramiko"),
+            ("rich", "colordiff"),
+            ("merge3", "merge"),
+            ("patiencediff", "patiencediff"),
+            ("atheris", "fuzzing"),
+        ]
+
+        for dep, dep_type in dependencies + optional_dependencies:
+            try:
+                module = __import__(dep)
+                version = getattr(module, "__version__", "(unknown)")
+                logger.info("  %s: %s [%s]", dep, version, dep_type)
+            except ImportError:
+                logger.info("  %s: (not installed) [%s]", dep, dep_type)
+
+
 class cmd_merge(Command):
     """Join two or more development histories together."""
 
@@ -6361,6 +6427,7 @@ commands = {
     "config": cmd_config,
     "count-objects": cmd_count_objects,
     "describe": cmd_describe,
+    "diagnose": cmd_diagnose,
     "daemon": cmd_daemon,
     "diff": cmd_diff,
     "diff-tree": cmd_diff_tree,

+ 22 - 0
tests/test_cli.py

@@ -4316,5 +4316,27 @@ Body
             self.assertIn("Body", msg_content)
 
 
+class DiagnoseCommandTest(DulwichCliTestCase):
+    """Tests for diagnose command."""
+
+    def test_diagnose(self):
+        """Test the diagnose command."""
+        with self.assertLogs("dulwich.cli", level="INFO") as cm:
+            result, _stdout, _stderr = self._run_cli("diagnose")
+            self.assertIsNone(result)
+
+            # Check that key information is present in log output
+            log_output = "\n".join(cm.output)
+            self.assertIn("Python version:", log_output)
+            self.assertIn("Python executable:", log_output)
+            self.assertIn("PYTHONPATH:", log_output)
+            self.assertIn("sys.path:", log_output)
+            self.assertIn("Dulwich version:", log_output)
+            self.assertIn("Installed dependencies:", log_output)
+
+            # Check that at least core dependencies are listed
+            self.assertIn("urllib3:", log_output)
+
+
 if __name__ == "__main__":
     unittest.main()