Files
railseek6/setup_git_final.py

372 lines
11 KiB
Python

#!/usr/bin/env python3
"""
Final Git setup script for railseek6 repository.
This script initializes the local git repository and pushes to the Gitea repository.
"""
import os
import sys
import subprocess
from pathlib import Path
def run_command(cmd, cwd=None):
"""Run a command and return output"""
print(f"Running: {cmd}")
try:
result = subprocess.run(cmd, shell=True, cwd=cwd, capture_output=True, text=True, encoding='utf-8')
if result.returncode != 0:
print(f"Error: {result.stderr}")
return False, result.stderr
print(f"Success: {result.stdout[:200] if result.stdout else 'No output'}")
return True, result.stdout
except Exception as e:
print(f"Exception: {e}")
return False, str(e)
def setup_git_repository():
"""Set up git repository and push to Gitea"""
print("=" * 60)
print("Setting up Git Repository for railseek6")
print("=" * 60)
# Repository information
repo_url = "https://git.mtrcompute.com/jleu3482/railseek6.git"
project_path = os.path.dirname(os.path.abspath(__file__))
print(f"Project path: {project_path}")
print(f"Repository URL: {repo_url}")
print()
# Step 1: Check if .git directory exists
git_dir = Path(project_path) / ".git"
if git_dir.exists():
print("✓ Git repository already initialized")
else:
print("Step 1: Initializing git repository...")
success, output = run_command("git init", project_path)
if not success:
print("✗ Failed to initialize git repository")
return False
# Step 2: Create .gitignore if not exists
gitignore_path = Path(project_path) / ".gitignore"
if not gitignore_path.exists():
print("\nStep 2: Creating .gitignore...")
gitignore_content = """# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual Environment
venv/
env/
ENV/
env.bak/
venv.bak/
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# LightRAG specific
rag_storage/
inputs/__enqueued__/
extracted_images/
extracted_images_test/
openclip_env/
openclip_gpu_env/
*.log
stderr.txt
stdout.txt
download_test/
"""
gitignore_path.write_text(gitignore_content, encoding='utf-8')
print("✓ Created .gitignore")
# Step 3: Add remote
print("\nStep 3: Setting up remote repository...")
# Check if remote already exists
success, output = run_command("git remote -v", project_path)
if "origin" in output:
print("✓ Remote 'origin' already exists")
# Update URL if needed
run_command(f'git remote set-url origin "{repo_url}"', project_path)
else:
success, output = run_command(f'git remote add origin "{repo_url}"', project_path)
if not success:
print("✗ Failed to add remote")
return False
print("✓ Added remote 'origin'")
# Step 4: Stage all files
print("\nStep 4: Staging files...")
success, output = run_command("git add .", project_path)
if not success:
print("✗ Failed to stage files")
return False
print("✓ Staged all files")
# Step 5: Check if there are changes to commit
print("\nStep 5: Checking for changes...")
success, output = run_command("git status --porcelain", project_path)
if not output.strip():
print("✓ No changes to commit (repository may already be up to date)")
else:
change_count = len(output.strip().split('\n'))
print(f"Changes detected: {change_count} files")
# Step 6: Commit changes
print("\nStep 6: Committing changes...")
commit_message = """Initial commit: LightRAG project with document download and auto-commit
Includes:
- LightRAG main codebase with GPU-accelerated OCR
- Document download endpoint implementation
- Web UI with clickable document references
- Auto-commit system for Git
- Test scripts for workflow validation
- Configuration files and documentation
Features implemented:
1. Document download API endpoint (/api/documents/download/{filename})
2. Clickable document references in search results
3. Secure file serving with authentication
4. Auto-commit functionality for major changes
5. Complete test workflow validation"""
success, output = run_command(f'git commit -m "{commit_message}"', project_path)
if not success:
print("✗ Failed to commit changes")
return False
print("✓ Committed changes")
# Step 7: Push to remote
print("\nStep 7: Pushing to remote repository...")
success, output = run_command("git push -u origin main", project_path)
if not success:
# Try with master branch if main doesn't exist
print("Trying with master branch...")
success, output = run_command("git push -u origin master", project_path)
if not success:
print("✗ Failed to push to remote")
print("You may need to create the branch on remote first")
print("Try: git push --set-upstream origin main")
return False
print("✓ Successfully pushed to remote repository")
# Step 8: Create auto-commit helper
print("\nStep 8: Creating auto-commit helper...")
auto_commit_script = """#!/usr/bin/env python3
"""
auto_commit_script += f'''
"""
Auto-commit helper for railseek6 project.
Usage: python git_auto_commit.py "Description of changes"
"""
import subprocess
import sys
import os
from datetime import datetime
def auto_commit(description):
"""Perform auto-commit with given description"""
project_path = os.path.dirname(os.path.abspath(__file__))
# Add timestamp to commit message
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
full_message = f"{description}\\n\\nAuto-commit at: {timestamp}\\nRepository: https://git.mtrcompute.com/jleu3482/railseek6"
# Git commands
commands = [
"git add .",
f'git commit -m "{full_message}"',
"git push"
]
for cmd in commands:
print(f"Running: {{cmd}}")
try:
result = subprocess.run(cmd, shell=True, cwd=project_path, capture_output=True, text=True, encoding='utf-8')
if result.returncode != 0:
print(f"Error: {{result.stderr}}")
return False
print(f"Success: {{result.stdout[:100] if result.stdout else 'No output'}}")
except Exception as e:
print(f"Exception: {{e}}")
return False
return True
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python git_auto_commit.py \\"Description of changes\\"")
print("Example: python git_auto_commit.py \\"Added document download endpoint\\"")
sys.exit(1)
description = sys.argv[1]
if auto_commit(description):
print("✓ Auto-commit completed successfully")
print(f"✓ Changes pushed to: https://git.mtrcompute.com/jleu3482/railseek6")
sys.exit(0)
else:
print("✗ Auto-commit failed")
sys.exit(1)
'''
auto_commit_path = Path(project_path) / "git_auto_commit_final.py"
auto_commit_path.write_text(auto_commit_script, encoding='utf-8')
print(f"✓ Created auto-commit helper: {auto_commit_path.name}")
# Step 9: Create README for the repository
print("\nStep 9: Creating README.md...")
readme_content = f"""# railseek6 - LightRAG Project
GPU-accelerated RAG (Retrieval-Augmented Generation) system with OCR and image classification capabilities.
## Repository Information
- **URL**: https://git.mtrcompute.com/jleu3482/railseek6
- **Description**: LightRAG project with document download and auto-commit functionality
- **Created**: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
## Features Implemented
### 1. Document Download System
- **API Endpoint**: `/api/documents/download/{filename}`
- **Security**: Filename sanitization, path traversal prevention
- **Authentication**: Bearer token required (jleu1212)
- **Supported Formats**: PDF, DOCX, TXT, MD, HTML, JSON, CSV, XLSX, PPTX
### 2. Web UI with Clickable References
- Search results show clickable document references
- Each reference has a download button
- References section lists all unique document sources
### 3. Auto-Commit System
- Automatic commits for major changes
- Descriptive commit messages
- Easy push to Gitea repository
### 4. Test Workflow
- Complete test suite for validation
- API testing, download testing, UI integration checks
## Setup Instructions
### 1. Start the Server
```bash
python start_server_fixed.py
```
### 2. Access Web UI
- Open http://localhost:3015 in browser
- Use API key: jleu1212 for authentication
### 3. Auto-Commit Changes
```bash
# Commit and push changes with description
python git_auto_commit_final.py "Added new feature or fix"
```
## Project Structure
```
railseek6/
├── LightRAG-main/ # Main LightRAG codebase
├── inputs/ # Document input directory
├── rag_storage/ # RAG storage (excluded from git)
├── git_setup.py # Git setup utilities
├── git_auto_commit_final.py # Auto-commit helper
├── test_workflow.py # Workflow testing
└── COMPLETION_SUMMARY.md # Project documentation
```
## API Documentation
### Search Documents
```bash
POST /api/search
Authorization: Bearer jleu1212
Content-Type: application/json
{{
"query": "search text",
"top_k": 5,
"mode": "naive" # or 'local', 'global', 'hybrid', 'mix', 'bypass'
}}
```
### Download Document
```bash
GET /api/documents/download/{{filename}}
Authorization: Bearer jleu1212
```
## Recent Updates
- Added document download endpoint to API
- Updated Web UI with clickable document references
- Implemented auto-commit functionality
- Created comprehensive test suite
- Set up Gitea repository integration
## License
This project is part of the LightRAG system with custom modifications for document download and auto-commit functionality.
"""
readme_path = Path(project_path) / "README.md"
readme_path.write_text(readme_content, encoding='utf-8')
print("✓ Created README.md")
# Final summary
print("\n" + "=" * 60)
print("SETUP COMPLETE!")
print("=" * 60)
print(f"\n✅ Repository: https://git.mtrcompute.com/jleu3482/railseek6")
print("✅ Local git repository initialized")
print("✅ Files committed and pushed")
print("✅ Auto-commit helper created: git_auto_commit_final.py")
print("✅ README.md created with documentation")
print("\n📋 Next steps:")
print("1. Restart LightRAG server to activate download endpoint")
print("2. Test workflow: python test_workflow.py")
print("3. Make changes and auto-commit: python git_auto_commit_final.py \"Description of changes\"")
print("4. View repository: https://git.mtrcompute.com/jleu3482/railseek6")
return True
if __name__ == "__main__":
try:
from datetime import datetime
success = setup_git_repository()
sys.exit(0 if success else 1)
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)