-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
520 lines (416 loc) · 14.1 KB
/
Copy pathdev.py
File metadata and controls
520 lines (416 loc) · 14.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python3
"""
Development utility script for LLM-LoRa project.
This script provides convenient commands for common development tasks including
linting, formatting, testing, and project setup.
Usage:
python dev.py <command> [options]
Available commands:
setup - Install dependencies and setup development environment
format - Format code with black and ruff
lint - Run linting with ruff
test - Run tests with pytest
check - Run all quality checks (format, lint, test)
clean - Clean up temporary files and caches
docs - Build documentation
env-info - Display environment information
validate - Validate configuration files
help - Show this help message
Examples:
python dev.py setup
python dev.py format
python dev.py test --gpu
python dev.py check
"""
import argparse
import logging
import os
import shutil
import subprocess
import sys
from pathlib import Path
try:
import torch
except Exception:
torch = None
logger = logging.getLogger(__name__)
class Colors:
"""ANSI color codes for terminal output."""
HEADER = "\033[95m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
END = "\033[0m"
BOLD = "\033[1m"
def log_header(message: str) -> None:
"""Log a formatted header message."""
logging.info("\n%s%s%s%s", Colors.BOLD, Colors.BLUE, "=" * 60, Colors.END)
logging.info("%s%s%s%s", Colors.BOLD, Colors.BLUE, message.center(60), Colors.END)
logging.info("%s%s%s%s\n", Colors.BOLD, Colors.BLUE, "=" * 60, Colors.END)
def log_success(message: str) -> None:
"""Log a success message."""
logging.info("%s✅ %s%s", Colors.GREEN, message, Colors.END)
def log_error(message: str) -> None:
"""Log an error message."""
logging.error("%s❌ %s%s", Colors.RED, message, Colors.END)
def log_warning(message: str) -> None:
"""Log a warning message."""
logging.warning("%s⚠️ %s%s", Colors.YELLOW, message, Colors.END)
def log_info(message: str) -> None:
"""Log an info message."""
logging.info("%si️ %s%s", Colors.BLUE, message, Colors.END)
def run_command(
cmd: list[str],
description: str,
*,
check: bool = True,
cwd: Path | None = None,
) -> bool:
"""Run a command and handle output."""
log_info(f"Running: {description}")
logging.info("Command: %s", " ".join(cmd))
try:
result = subprocess.run(cmd, check=check, cwd=cwd, capture_output=False, text=True)
success = result.returncode == 0
if success:
log_success(f"Completed: {description}")
else:
log_error(f"Failed: {description}")
except subprocess.CalledProcessError as e:
log_error(f"Command failed: {description}")
log_error(f"Return code: {e.returncode}")
return False
except FileNotFoundError:
log_error(f"Command not found for: {description}")
log_error("Make sure all dependencies are installed")
return False
else:
return success
def setup_environment() -> bool:
"""Setup development environment."""
log_header("Setting up Development Environment")
project_root = Path(__file__).parent
# Check if we're in a git repository
if not (project_root / ".git").exists():
log_warning("Not in a git repository")
# Install dependencies with Poetry
if not run_command(["poetry", "install", "--no-root"], "Installing dependencies"):
return False
# Install pre-commit hooks
if not run_command(
["poetry", "run", "pre-commit", "install"],
"Installing pre-commit hooks",
):
return False
# Verify CUDA availability
cuda_check = [
"python",
"-c",
"import torch; print(f'CUDA available: {torch.cuda.is_available()}')",
]
run_command(["poetry", "run", *cuda_check], "Checking CUDA availability", check=False)
log_success("Development environment setup completed!")
return True
def format_code() -> bool:
"""Format code with black and ruff."""
log_header("Formatting Code")
success = True
# Run ruff formatting
if not run_command(
["poetry", "run", "ruff", "check", ".", "--fix", "--config=pyproject.toml"],
"Running ruff formatter",
):
success = False
# Run black formatting
if not run_command(["poetry", "run", "black", "."], "Running black formatter"):
success = False
if success:
log_success("Code formatting completed!")
else:
log_error("Code formatting had issues")
return success
def run_linting() -> bool:
"""Run linting with ruff."""
log_header("Running Linting")
success = run_command(
["poetry", "run", "ruff", "check", ".", "--config=pyproject.toml"],
"Running ruff linter",
)
if success:
log_success("Linting passed!")
else:
log_error("Linting found issues")
return success
def run_tests(*, gpu: bool = False, unit: bool = False, integration: bool = False) -> bool:
"""Run tests with pytest."""
log_header("Running Tests")
cmd = ["poetry", "run", "pytest", "tests/", "-v"]
# Add markers based on options
markers = []
if gpu:
markers.append("gpu")
if unit:
markers.append("unit")
if integration:
markers.append("integration")
if markers:
cmd.extend(["-m", " or ".join(markers)])
success = run_command(cmd, f"Running tests{' (GPU)' if gpu else ''}")
if success:
log_success("All tests passed!")
else:
log_error("Some tests failed")
return success
def run_quality_checks() -> bool:
"""Run all quality checks."""
log_header("Running Quality Checks")
all_passed = True
# Format code
if not format_code():
all_passed = False
# Run linting
if not run_linting():
all_passed = False
# Run tests
if not run_tests():
all_passed = False
# Run pre-commit hooks
if not run_command(
["poetry", "run", "pre-commit", "run", "--all-files"],
"Running pre-commit hooks",
):
all_passed = False
if all_passed:
log_success("All quality checks passed!")
else:
log_error("Some quality checks failed")
return all_passed
def clean_project() -> None:
"""Clean up temporary files and caches."""
log_header("Cleaning Project")
project_root = Path(__file__).parent
# Directories to clean
clean_dirs = [
"__pycache__",
".pytest_cache",
".ruff_cache",
"*.egg-info",
"build",
"dist",
".mypy_cache",
"docs/_build",
]
for pattern in clean_dirs:
for path in project_root.rglob(pattern):
if path.is_dir():
log_info(f"Removing directory: {path}")
try:
shutil.rmtree(path)
except OSError as e:
log_warning(f"Could not remove {path}: {e}")
# Files to clean
clean_files = ["*.pyc", "*.pyo", ".coverage", "coverage.xml"]
for pattern in clean_files:
for path in project_root.rglob(pattern):
if path.is_file():
log_info(f"Removing file: {path}")
try:
path.unlink()
except OSError as e:
log_warning(f"Could not remove {path}: {e}")
log_success("Project cleanup completed!")
def build_docs() -> bool:
"""Build documentation."""
log_header("Building Documentation")
docs_dir = Path(__file__).parent / "docs"
if not docs_dir.exists():
log_error("Documentation directory not found")
return False
# Build HTML documentation
success = run_command(
["sphinx-build", "-b", "html", ".", "_build/html"],
"Building HTML documentation",
cwd=docs_dir,
)
if success:
log_success("Documentation built successfully!")
log_info(f"Documentation available at: {docs_dir / '_build' / 'html' / 'index.html'}")
else:
log_error("Documentation build failed")
return success
def show_env_info() -> None:
"""Display environment information."""
log_header("Environment Information")
project_root = Path(__file__).parent
# Python version
python_version = (
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
)
logger.info("Python Version: %s", python_version)
logger.info("Python Executable: %s", sys.executable)
# Project info
logger.info("Project Root: %s", project_root)
logger.info("Working Directory: %s", Path.cwd())
# Git info
try:
git_path = shutil.which("git")
if git_path is None:
raise FileNotFoundError("Git executable not found.")
result = subprocess.run(
[git_path, "branch", "--show-current"],
capture_output=True,
text=True,
cwd=project_root,
check=False,
)
if result.returncode == 0:
logger.info("Git Branch: %s", result.stdout.strip())
git_path = shutil.which("git")
if git_path is None:
raise FileNotFoundError("Git executable not found.")
result = subprocess.run(
[git_path, "rev-parse", "--short", "HEAD"],
capture_output=True,
text=True,
cwd=project_root,
check=False,
)
if result.returncode == 0:
logger.info("Git Commit: %s", result.stdout.strip())
except FileNotFoundError:
logging.info("Git: Not available")
# Virtual environment info
venv_path = os.environ.get("VIRTUAL_ENV")
if venv_path:
logger.info("Virtual Environment: %s", venv_path)
else:
logger.info("Virtual Environment: Not detected")
# CUDA info
if torch is not None:
logger.info("PyTorch Version: %s", getattr(torch, "__version__", "unknown"))
try:
cuda_available = torch.cuda.is_available()
except Exception:
cuda_available = False
logger.info("CUDA Available: %s", cuda_available)
cuda_version = getattr(torch, "version", None)
cuda_version_str = (
getattr(cuda_version, "cuda", None) if cuda_version is not None else None
)
logger.info("CUDA Version: %s", cuda_version_str)
if cuda_available:
try:
gpu_count = torch.cuda.device_count()
except Exception:
gpu_count = 0
logger.info("GPU Count: %s", gpu_count)
if gpu_count > 0:
try:
logger.info("GPU Name: %s", torch.cuda.get_device_name(0))
except Exception:
logger.info("GPU Name: unknown")
else:
logger.info("PyTorch: Not available")
def validate_config() -> None:
"""Validate configuration files."""
log_header("Validating Configuration")
project_root = Path(__file__).parent
# Validate main config
config_file = project_root / "conf" / "config.yaml"
if config_file.exists():
cmd = [
"python",
"-c",
f"import yaml; yaml.safe_load(open('{config_file}')); "
"print('✅ config.yaml is valid')",
]
run_command(["poetry", "run", *cmd], "Validating config.yaml", check=False)
else:
log_warning("config.yaml not found")
# Validate pyproject.toml
pyproject_file = project_root / "pyproject.toml"
if pyproject_file.exists():
cmd = [
"python",
"-c",
f"import tomllib; tomllib.load(open('{pyproject_file}', 'rb')); "
"print('✅ pyproject.toml is valid')",
]
run_command(["poetry", "run", *cmd], "Validating pyproject.toml", check=False)
# Test imports of main modules
modules = ["training_model", "evaluation", "testing_model"]
for module in modules:
cmd = ["python", "-c", f"import {module}; print('✅ {module} imports successfully')"]
run_command(["poetry", "run", *cmd], f"Testing import of {module}", check=False)
def main() -> None:
"""Main entry point."""
# Configure logging for colored output
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[logging.StreamHandler()],
)
parser = argparse.ArgumentParser(
description="Development utility script for LLM-LoRa project",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument(
"command",
choices=[
"setup",
"format",
"lint",
"test",
"check",
"clean",
"docs",
"env-info",
"validate",
"help",
],
help="Command to run",
)
# Test options
parser.add_argument("--gpu", action="store_true", help="Run GPU tests (for test command)")
parser.add_argument(
"--unit",
action="store_true",
help="Run unit tests only (for test command)",
)
parser.add_argument(
"--integration",
action="store_true",
help="Run integration tests only (for test command)",
)
args = parser.parse_args()
if args.command == "help":
parser.print_help()
return
# Ensure we're in the project directory
project_root = Path(__file__).parent
os.chdir(project_root)
success = True
if args.command == "setup":
success = setup_environment()
elif args.command == "format":
success = format_code()
elif args.command == "lint":
success = run_linting()
elif args.command == "test":
success = run_tests(gpu=args.gpu, unit=args.unit, integration=args.integration)
elif args.command == "check":
success = run_quality_checks()
elif args.command == "clean":
clean_project()
elif args.command == "docs":
success = build_docs()
elif args.command == "env-info":
show_env_info()
elif args.command == "validate":
validate_config()
if not success:
sys.exit(1)
if __name__ == "__main__":
main()