-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
215 lines (179 loc) · 6.77 KB
/
validation.py
File metadata and controls
215 lines (179 loc) · 6.77 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"""
FUSE - (python dev tool) - CLI Validators
CLI - Validators
"""
import keyword
import os
import re
from pathlib import Path
from typing import List
from python_dev_tool.core.exceptions import DevToolError
class ProjectValidator:
"""Validator for project initialization and configuration."""
# Valid project name pattern (Python package naming convention)
PROJECT_NAME_PATTERN = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*")
# Reserved Python keywords and built-in names
RESERVED_NAMES = set(
keyword.kwlist
+ [
"python",
"pip",
"setuptools",
"wheel",
"django",
"flask",
"fastapi",
"streamlit",
"tornado",
"sanic",
"test",
"tests",
"src",
"lib",
"bin",
"etc",
"var",
"tmp",
"home",
"root",
]
)
# Blocked directory names
BLOCKED_DIRECTORIES = {
"con",
"prn",
"aux",
"nul", # Windows reserved
"com1",
"com2",
"com3",
"com4",
"com5",
"com6",
"com7",
"com8",
"com9",
"lpt1",
"lpt2",
"lpt3",
"lpt4",
"lpt5",
"lpt6",
"lpt7",
"lpt8",
"lpt9",
}
def validate_project_name(self, name: str) -> bool:
"""Validate project name according to Python conventions."""
if not name:
raise DevToolError("Project name cannot be empty")
if not self.PROJECT_NAME_PATTERN.match(name):
raise DevToolError(
f"Invalid project name: '{name}'\n"
"Project names must start with a letter or underscore, "
"followed by letters, numbers, or underscores only."
)
if name.lower() in self.RESERVED_NAMES:
raise DevToolError(
f"Project name '{name}' is reserved. Please choose a different name."
)
if name.lower() in self.BLOCKED_DIRECTORIES:
raise DevToolError(f"Project name '{name}' is not allowed on this system.")
if len(name) > 50:
raise DevToolError("Project name must be 50 characters or less")
return True
def validate_project_path(self, path: Path) -> bool:
"""Validate project path."""
try:
# Convert to absolute path
abs_path = path.resolve()
# Check if path is too long (Windows has 260 char limit)
if len(str(abs_path)) > 240:
raise DevToolError(
f"Project path is too long: {abs_path}\n"
"Please choose a shorter path."
)
# Check for invalid characters in path
invalid_chars = '<>:"|?*'
if any(char in str(abs_path) for char in invalid_chars):
raise DevToolError(
f"Project path contains invalid characters: {abs_path}\n"
f"Avoid these characters: {invalid_chars}"
)
# Check if we can create/access the directory
if path.exists():
if not path.is_dir():
raise DevToolError(f"Path exists but is not a directory: {path}")
# Check write permissions
if not os.access(path, os.W_OK):
raise DevToolError(f"No write permission for directory: {path}")
else:
# Check if parent directory exists and is writable
parent = path.parent
if not parent.exists():
# Try to create parent directories
try:
parent.mkdir(parents=True, exist_ok=True)
except PermissionError:
raise DevToolError(
f"Cannot create parent directories: {parent}"
)
if not os.access(parent, os.W_OK):
raise DevToolError(
f"No write permission for parent directory: {parent}"
)
return True
except OSError as e:
raise DevToolError(f"Invalid project path: {e}")
def validate_framework(
self, framework: str, supported_frameworks: List[str]
) -> bool:
"""Validate framework selection."""
if framework not in supported_frameworks:
raise DevToolError(
f"Unsupported framework: '{framework}'\n"
f"Supported frameworks: {', '.join(supported_frameworks)}"
)
return True
def check_directory_conflicts(self, path: Path) -> List[str]:
"""Check for potential conflicts in project directory."""
conflicts = []
if not path.exists():
return conflicts
# Check for existing Python files that might conflict
python_files = list(path.glob("*.py"))
if python_files:
conflicts.append(f"Found {len(python_files)} Python files")
# Check for existing frameworks
framework_indicators = {
"Django": ["manage.py", "settings.py"],
"Flask": ["app.py", "wsgi.py"],
"FastAPI": ["main.py", "api.py"],
"Streamlit": ["streamlit_app.py"],
}
for framework, indicators in framework_indicators.items():
if any((path / indicator).exists() for indicator in indicators):
conflicts.append(f"Detected existing {framework} project")
# Check for virtual environments
venv_dirs = [".venv", "venv", "env", "ENV"]
existing_venvs = [venv for venv in venv_dirs if (path / venv).exists()]
if existing_venvs:
conflicts.append(f"Found virtual environments: {', '.join(existing_venvs)}")
# Check for package managers
package_files = ["requirements.txt", "Pipfile", "pyproject.toml", "setup.py"]
existing_packages = [pkg for pkg in package_files if (path / pkg).exists()]
if existing_packages:
conflicts.append(f"Found package files: {', '.join(existing_packages)}")
return conflicts
def validate_project_path(path: Path) -> None:
"""Standalone function to validate project path."""
validator = ProjectValidator()
validator.validate_project_path(path)
def validate_project_name(name: str) -> None:
"""Standalone function to validate project name."""
validator = ProjectValidator()
validator.validate_project_name(name)
def validate_framework(framework: str, supported_frameworks: List[str]) -> None:
"""Standalone function to validate framework."""
validator = ProjectValidator()
validator.validate_framework(framework, supported_frameworks)