-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmodify_imports.py
More file actions
186 lines (158 loc) · 6.26 KB
/
modify_imports.py
File metadata and controls
186 lines (158 loc) · 6.26 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Import Fix Script - Modify import statements in all files to work correctly in pip install mode
"""
import os
import re
import sys
import glob
from typing import List, Dict
# List of files and directory patterns to process
FILE_PATTERNS = [
# Core module files
'models.py',
'app.py',
'app_factory.py',
'init_knowledge_base.py',
# API modules
'api/*.py',
'api/services/*.py',
'api/utils/*.py',
# Configuration modules
'config/*.py',
# LLM model modules
'llm_model/*.py',
# Vector storage modules
'vector_store/*.py',
# Utility modules
'utils/*.py',
# Other modules
'retriever/*.py',
'task/*.py',
'translator/*.py',
'preprocessor/*.py',
'preprocessor/*/*.py',
'preprocessor/*/*/*.py',
'doc_process/*.py',
'translate.py',
'cracksql.py',
]
# List of modules that should not be replaced with cracksql.xxx
# Includes Python standard libraries and common third-party libraries
DO_NOT_REPLACE_MODULES = [
# Python standard libraries
'os', 'sys', 're', 'json', 'time', 'datetime', 'logging', 'logging.config',
'math', 'random', 'collections', 'itertools', 'functools', 'types',
'traceback', 'copy', 'shutil', 'tempfile', 'glob', 'pathlib', 'uuid',
'hashlib', 'base64', 'urllib', 'http', 'socket', 'email', 'smtplib',
'threading', 'multiprocessing', 'subprocess', 'asyncio', 'concurrent',
'unittest', 'pytest', 'yaml', 'csv', 'xml', 'html', 'argparse', 'configparser',
'decimal', 'fractions', 'numbers', 'statistics', 'pickle', 'codecs', 'platform',
'io', 'enum', 'string', 'calendar', 'zlib', 'gzip', 'tarfile', 'zipfile',
'struct', 'array', 'heapq', 'bisect', 'weakref', 'abc', 'typing', 'importlib',
# Common third-party libraries
'flask', 'flask_cors', 'flask_migrate', 'flask_sqlalchemy', 'flask_caching',
'sqlalchemy', 'pymysql', 'requests', 'numpy', 'pandas', 'sklearn', 'torch',
'openai', 'langchain', 'transformers', 'chromadb', 'tiktoken', 'sqlglot',
'gunicorn', 'pytest', 'jwt', 'pyyaml', 'alembic', 'click', 'werkzeug',
'jinja2', 'markupsafe', 'itsdangerous', 'six', 'setuptools', 'pip',
'wheel', 'virtualenv', 'pytz', 'chardet', 'idna', 'certifi', 'urllib3',
'tqdm', 'packaging', 'antlr4', 'flask_apscheduler', 'langchain_openai',
'langchain_community', 'tenacity', 'langchain_core', 'langchain_community',
'psycopg2', 'sqlalchemy', 'cx_Oracle', 'paramiko', 'func_timeout', 'oracledb'
]
# Import replacement rules
IMPORT_REPLACEMENTS = {
# Import from any module
r'^from ([\w\.]+) import (.*)':
lambda match: process_from_import(match),
# Direct import of module
r'^import ([\w\.]+)(.*)':
lambda match: process_import(match),
}
def process_from_import(match):
"""Process from xxx import yyy style imports"""
module = match.group(1)
imports = match.group(2)
# Check if it's a module that should not be replaced
if any(module == lib or module.startswith(f"{lib}.") for lib in DO_NOT_REPLACE_MODULES):
return f"from {module} import {imports}"
# Simplified import replacement logic, remove the creation of a stub
return f"from cracksql.{module} import {imports}"
def process_import(match):
"""Process import xxx style imports"""
module = match.group(1)
rest = match.group(2)
# Check if it's a module that should not be replaced
if any(module == lib or module.startswith(f"{lib}.") for lib in DO_NOT_REPLACE_MODULES):
return f"import {module}{rest}"
# Create a valid alias (remove dot)
alias = module.replace(".", "_")
# Simplified import replacement logic, remove the creation of a stub
return f"import cracksql.{module} as {alias}{rest}"
def get_files_to_process(base_dir: str, patterns: List[str]) -> List[str]:
"""Get the list of files to process based on patterns"""
files = []
for pattern in patterns:
# Convert pattern to full path
full_pattern = os.path.join(base_dir, pattern)
# Use glob to match files
matched_files = glob.glob(full_pattern, recursive=True)
files.extend(matched_files)
# Filter out .py files and remove duplicates
return sorted(list(set([f for f in files if f.endswith('.py')])))
def modify_file_imports(file_path: str) -> None:
"""Modify import statements in the file"""
print(f"Processing file: {file_path}")
# Read file content
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
except UnicodeDecodeError:
try:
# Try other encoding
with open(file_path, 'r', encoding='latin-1') as f:
content = f.read()
except Exception as e:
print(f" - Unable to read file, skipping: {str(e)}")
return
# Apply all replacement rules
modified_content = content
for pattern, replacement in IMPORT_REPLACEMENTS.items():
def replace_func(match):
if callable(replacement):
return replacement(match)
return replacement
modified_content = re.sub(pattern, replace_func, modified_content, flags=re.MULTILINE)
# Fix potential double dot issues
modified_content = modified_content.replace("cracksql..", "cracksql.")
# If the file was modified, save changes
if modified_content != content:
try:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(modified_content)
print(f" - File modified")
except Exception as e:
print(f" - Error saving file: {str(e)}")
else:
print(f" - File unchanged")
def main():
"""Main function"""
print("Starting import statement fix...")
# Check command line arguments
if len(sys.argv) > 1:
base_dir = sys.argv[1]
else:
# Default to current directory
base_dir = '.'
print(f"Using base directory: {base_dir}")
# Get files to process
files = get_files_to_process(base_dir, FILE_PATTERNS)
print(f"Found {len(files)} files to process")
# Process each file
for file_path in files:
modify_file_imports(file_path)
print("Import fix completed!")
if __name__ == "__main__":
main()