-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathrepo_validator.py
More file actions
189 lines (148 loc) · 4.7 KB
/
repo_validator.py
File metadata and controls
189 lines (148 loc) · 4.7 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
"""
Repository validation utilities for documentation generation.
"""
from pathlib import Path
from typing import Tuple, List
import os
from codewiki.cli.utils.errors import RepositoryError
from codewiki.cli.utils.validation import validate_repository_path, detect_supported_languages
# Supported file extensions by language
SUPPORTED_EXTENSIONS = {
'.py', # Python
'.java', # Java
'.js', # JavaScript
'.jsx', # JavaScript (React)
'.ts', # TypeScript
'.tsx', # TypeScript (React)
'.c', # C
'.h', # C headers
'.cpp', # C++
'.hpp', # C++ headers
'.cc', # C++
'.hh', # C++ headers
'.cxx', # C++
'.hxx', # C++ headers
'.cs', # C#
'.php', # PHP
'.phtml', # PHP templates
'.inc', # PHP includes
'.kt', # Kotlin
'.kts', # Kotlin Scripts
}
def validate_repository(repo_path: Path) -> Tuple[Path, List[Tuple[str, int]]]:
"""
Validate repository for documentation generation.
Checks:
- Path exists and is a directory
- Contains supported code files
- Has sufficient files for meaningful documentation
Args:
repo_path: Path to repository
Returns:
Tuple of (validated_path, language_counts)
Raises:
RepositoryError: If validation fails
"""
# Validate path exists
repo_path = validate_repository_path(repo_path)
# Detect languages
languages = detect_supported_languages(repo_path)
if not languages:
raise RepositoryError(
f"No supported code files found in {repo_path}\n\n"
"CodeWiki supports: Python, Java, JavaScript, TypeScript, C, C++, C#, PHP\n\n"
"Please navigate to a code repository or specify a custom directory:\n"
" cd /path/to/your/project\n"
" codewiki generate"
)
return repo_path, languages
def check_writable_output(output_dir: Path) -> Path:
"""
Check if output directory is writable.
Args:
output_dir: Output directory path
Returns:
Validated output directory path
Raises:
RepositoryError: If output directory is not writable
"""
output_dir = Path(output_dir).expanduser().resolve()
# Check if output directory exists
if output_dir.exists():
if not output_dir.is_dir():
raise RepositoryError(
f"Output path exists but is not a directory: {output_dir}"
)
# Check if writable
if not os.access(output_dir, os.W_OK):
raise RepositoryError(
f"Output directory is not writable: {output_dir}\n\n"
f"Try: chmod u+w {output_dir}"
)
else:
# Check if parent is writable
parent = output_dir.parent
if not parent.exists():
raise RepositoryError(
f"Parent directory does not exist: {parent}"
)
if not os.access(parent, os.W_OK):
raise RepositoryError(
f"Cannot create output directory (parent not writable): {parent}\n\n"
f"Try: chmod u+w {parent}"
)
return output_dir
def is_git_repository(repo_path: Path) -> bool:
"""
Check if path is a git repository.
Args:
repo_path: Path to check
Returns:
True if git repository, False otherwise
"""
git_dir = repo_path / ".git"
return git_dir.exists() and git_dir.is_dir()
def get_git_commit_hash(repo_path: Path) -> str:
"""
Get current git commit hash.
Args:
repo_path: Repository path
Returns:
Commit hash or empty string if not a git repo
"""
if not is_git_repository(repo_path):
return ""
try:
import git
repo = git.Repo(repo_path)
return repo.head.commit.hexsha
except Exception:
return ""
def get_git_branch(repo_path: Path) -> str:
"""
Get current git branch name.
Args:
repo_path: Repository path
Returns:
Branch name or empty string if not a git repo
"""
if not is_git_repository(repo_path):
return ""
try:
import git
repo = git.Repo(repo_path)
return repo.active_branch.name
except Exception:
return ""
def count_code_files(repo_path: Path) -> int:
"""
Count supported code files in repository.
Args:
repo_path: Repository path
Returns:
Number of code files
"""
count = 0
for ext in SUPPORTED_EXTENSIONS:
count += len(list(repo_path.rglob(f"*{ext}")))
return count