File size: 3,872 Bytes
bd61f34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
"""
DroneKit patcher to fix compatibility with Python 3.10+

This script needs to be run before importing DroneKit and it will patch
the DroneKit package directly to use collections.abc instead of collections
for MutableMapping.
"""

import os
import sys
import re
import site
from pathlib import Path

def patch_dronekit_files():
    """
    Patch DroneKit Python files to use collections.abc instead of collections
    for MutableMapping.
    """
    # Find DroneKit install path
    try:
        import dronekit
        print("Error: DroneKit already imported. This script must be run before importing DroneKit.")
        return False
    except AttributeError:
        # This is the error we're trying to fix
        pass
    
    # Try to find dronekit in site-packages
    dronekit_paths = []
    
    # Check standard site-packages locations
    for site_dir in site.getsitepackages() + [site.getusersitepackages()]:
        potential_path = os.path.join(site_dir, "dronekit")
        if os.path.exists(potential_path):
            dronekit_paths.append(potential_path)
    
    # Also check current directory and its parent
    for relative_path in ["dronekit", "../dronekit", "lib/dronekit"]:
        if os.path.exists(relative_path):
            dronekit_paths.append(os.path.abspath(relative_path))
    
    if not dronekit_paths:
        print("Could not find DroneKit installation. Please install it with pip install dronekit")
        return False
    
    patched_files = 0
    
    for dronekit_path in dronekit_paths:
        print(f"Found DroneKit at: {dronekit_path}")
        
        # Find all Python files
        for root, _, files in os.walk(dronekit_path):
            for file in files:
                if file.endswith(".py"):
                    file_path = os.path.join(root, file)
                    
                    # Read the file content
                    with open(file_path, 'r') as f:
                        content = f.read()
                    
                    # Check if the file uses collections.MutableMapping
                    if "collections.MutableMapping" in content or "collections import MutableMapping" in content:
                        # Replace direct imports
                        modified_content = re.sub(
                            r'from collections import (\w+, )*MutableMapping(, \w+)*', 
                            r'from collections.abc import MutableMapping', 
                            content
                        )
                        
                        # Replace usage in code
                        modified_content = re.sub(
                            r'collections\.MutableMapping', 
                            r'collections.abc.MutableMapping', 
                            modified_content
                        )
                        
                        # Write the modified content back to the file
                        with open(file_path, 'w') as f:
                            f.write(modified_content)
                        
                        print(f"Patched: {file_path}")
                        patched_files += 1
    
    if patched_files > 0:
        print(f"Successfully patched {patched_files} DroneKit files to use collections.abc.MutableMapping")
        return True
    else:
        print("No DroneKit files needed patching or files could not be found.")
        return False

if __name__ == "__main__":
    # Only apply patch for Python 3.10+
    if sys.version_info >= (3, 10):
        success = patch_dronekit_files()
        if success:
            print("DroneKit patched successfully for Python 3.10+ compatibility")
        else:
            print("Failed to patch DroneKit")
    else:
        print(f"Python version {sys.version_info.major}.{sys.version_info.minor} does not require patching")