|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Helper script for debugging Zulip Terminal. |
| 4 | +
|
| 5 | +This script provides utilities for common debugging tasks: |
| 6 | +1. Analyzing debug logs |
| 7 | +2. Testing connectivity to Zulip server |
| 8 | +3. Checking terminal capabilities |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import os |
| 13 | +import re |
| 14 | +import sys |
| 15 | +import subprocess |
| 16 | +import json |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | + |
| 20 | +def analyze_debug_log(log_file="debug.log"): |
| 21 | + """ |
| 22 | + Analyze a debug log file for common issues. |
| 23 | + """ |
| 24 | + if not os.path.exists(log_file): |
| 25 | + print(f"Error: Log file '{log_file}' not found") |
| 26 | + return |
| 27 | + |
| 28 | + print(f"Analyzing {log_file}...") |
| 29 | + with open(log_file, 'r') as f: |
| 30 | + content = f.read() |
| 31 | + |
| 32 | + # Look for error patterns |
| 33 | + error_patterns = [ |
| 34 | + r'ERROR', |
| 35 | + r'Exception', |
| 36 | + r'Traceback', |
| 37 | + r'Failed to' |
| 38 | + ] |
| 39 | + |
| 40 | + errors_found = False |
| 41 | + for pattern in error_patterns: |
| 42 | + matches = re.finditer(pattern, content, re.IGNORECASE) |
| 43 | + for match in matches: |
| 44 | + line_start = content.rfind('\n', 0, match.start()) + 1 |
| 45 | + line_end = content.find('\n', match.end()) |
| 46 | + if line_end == -1: |
| 47 | + line_end = len(content) |
| 48 | + |
| 49 | + line = content[line_start:line_end].strip() |
| 50 | + print(f"Potential issue found: {line}") |
| 51 | + errors_found = True |
| 52 | + |
| 53 | + if not errors_found: |
| 54 | + print("No obvious errors found in the log file.") |
| 55 | + |
| 56 | + |
| 57 | +def test_connectivity(server_url=None): |
| 58 | + """ |
| 59 | + Test connectivity to a Zulip server. |
| 60 | + """ |
| 61 | + if not server_url: |
| 62 | + # Try to get server URL from zuliprc |
| 63 | + zuliprc_path = os.path.expanduser("~/.zuliprc") |
| 64 | + if os.path.exists(zuliprc_path): |
| 65 | + with open(zuliprc_path, 'r') as f: |
| 66 | + for line in f: |
| 67 | + if line.startswith('site='): |
| 68 | + server_url = line.split('=')[1].strip() |
| 69 | + break |
| 70 | + |
| 71 | + if not server_url: |
| 72 | + print("Error: No server URL provided and couldn't find one in ~/.zuliprc") |
| 73 | + return |
| 74 | + |
| 75 | + print(f"Testing connectivity to {server_url}...") |
| 76 | + try: |
| 77 | + import requests |
| 78 | + response = requests.get(f"{server_url}/api/v1/server_settings") |
| 79 | + if response.status_code == 200: |
| 80 | + print(f"Successfully connected to {server_url}") |
| 81 | + try: |
| 82 | + settings = response.json() |
| 83 | + print(f"Server version: {settings.get('zulip_version', 'unknown')}") |
| 84 | + except json.JSONDecodeError: |
| 85 | + print("Received response, but couldn't parse as JSON") |
| 86 | + else: |
| 87 | + print(f"Failed to connect: HTTP status {response.status_code}") |
| 88 | + except Exception as e: |
| 89 | + print(f"Connection error: {e}") |
| 90 | + |
| 91 | + |
| 92 | +def check_terminal_capabilities(): |
| 93 | + """ |
| 94 | + Check for terminal capabilities that might affect Zulip Terminal. |
| 95 | + """ |
| 96 | + print("Checking terminal capabilities...") |
| 97 | + |
| 98 | + # Check for color support |
| 99 | + colors = os.environ.get('TERM', 'unknown') |
| 100 | + print(f"TERM environment: {colors}") |
| 101 | + |
| 102 | + if 'COLORTERM' in os.environ: |
| 103 | + print(f"COLORTERM: {os.environ['COLORTERM']}") |
| 104 | + |
| 105 | + # Check for Unicode support |
| 106 | + print("\nTesting Unicode rendering capabilities:") |
| 107 | + test_chars = [ |
| 108 | + ("Basic symbols", "▶ ◀ ✓ ✗"), |
| 109 | + ("Emoji (simple)", "😀 🙂 👍"), |
| 110 | + ("Box drawing", "│ ┌ ┐ └ ┘ ├ ┤ ┬ ┴ ┼"), |
| 111 | + ("Math symbols", "∞ ∑ √ ∫ π") |
| 112 | + ] |
| 113 | + |
| 114 | + for name, chars in test_chars: |
| 115 | + print(f" {name}: {chars}") |
| 116 | + |
| 117 | + # Check for urwid compatibility |
| 118 | + try: |
| 119 | + import urwid |
| 120 | + print("\nUrwid detected. Running basic urwid test...") |
| 121 | + # This doesn't actually run a visual test - just checks if urwid can be imported |
| 122 | + print("Urwid import successful") |
| 123 | + except ImportError: |
| 124 | + print("Urwid not found. This may indicate installation issues.") |
| 125 | + |
| 126 | + |
| 127 | +def main(): |
| 128 | + parser = argparse.ArgumentParser(description="Zulip Terminal Debugging Helper") |
| 129 | + subparsers = parser.add_subparsers(dest="command", help="Command to run") |
| 130 | + |
| 131 | + # Log analyzer |
| 132 | + log_parser = subparsers.add_parser("log", help="Analyze debug logs") |
| 133 | + log_parser.add_argument("--file", default="debug.log", help="Log file to analyze") |
| 134 | + |
| 135 | + # Connectivity test |
| 136 | + conn_parser = subparsers.add_parser("connect", help="Test connectivity") |
| 137 | + conn_parser.add_argument("--server", help="Server URL (e.g., https://chat.zulip.org)") |
| 138 | + |
| 139 | + # Terminal test |
| 140 | + term_parser = subparsers.add_parser("terminal", help="Check terminal capabilities") |
| 141 | + |
| 142 | + # Run zulip-term with debug |
| 143 | + run_parser = subparsers.add_parser("run", help="Run zulip-term with debugging") |
| 144 | + run_parser.add_argument("--profile", action="store_true", help="Enable profiling") |
| 145 | + |
| 146 | + args = parser.parse_args() |
| 147 | + |
| 148 | + if args.command == "log": |
| 149 | + analyze_debug_log(args.file) |
| 150 | + elif args.command == "connect": |
| 151 | + test_connectivity(args.server) |
| 152 | + elif args.command == "terminal": |
| 153 | + check_terminal_capabilities() |
| 154 | + elif args.command == "run": |
| 155 | + cmd = ["zulip-term", "-d"] |
| 156 | + if args.profile: |
| 157 | + cmd.append("--profile") |
| 158 | + print(f"Running: {' '.join(cmd)}") |
| 159 | + subprocess.run(cmd) |
| 160 | + else: |
| 161 | + parser.print_help() |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments