197 lines
5.2 KiB
Python
Executable File
197 lines
5.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
QR Code Manager - Data Cleanup Script
|
|
|
|
This script removes all persistent data to prepare for a clean deployment.
|
|
It will delete:
|
|
- All QR codes and their data
|
|
- All dynamic link pages
|
|
- All short URLs
|
|
- All uploaded QR code images
|
|
- Flask session files
|
|
|
|
Use this script when you want to start fresh or prepare for deployment.
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import json
|
|
from pathlib import Path
|
|
|
|
def clean_json_data():
|
|
"""Clean all JSON data files"""
|
|
data_dir = Path('data')
|
|
json_files = [
|
|
'qr_codes.json',
|
|
'link_pages.json',
|
|
'short_urls.json'
|
|
]
|
|
|
|
print("🗑️ Cleaning JSON data files...")
|
|
|
|
for json_file in json_files:
|
|
file_path = data_dir / json_file
|
|
if file_path.exists():
|
|
# Reset to empty object
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
json.dump({}, f, indent=2)
|
|
print(f" ✅ Cleared {json_file}")
|
|
else:
|
|
print(f" ⚠️ {json_file} not found")
|
|
|
|
def clean_qr_images():
|
|
"""Clean all QR code image files"""
|
|
qr_dir = Path('app/static/qr_codes')
|
|
|
|
print("🖼️ Cleaning QR code images...")
|
|
|
|
if qr_dir.exists():
|
|
# Count files before deletion
|
|
files = list(qr_dir.glob('*.png'))
|
|
count = len(files)
|
|
|
|
# Delete all PNG files
|
|
for file in files:
|
|
file.unlink()
|
|
|
|
print(f" ✅ Deleted {count} QR code images")
|
|
else:
|
|
print(" ⚠️ QR codes directory not found")
|
|
|
|
def clean_flask_sessions():
|
|
"""Clean Flask session files"""
|
|
session_dir = Path('flask_session')
|
|
|
|
print("🔐 Cleaning Flask sessions...")
|
|
|
|
if session_dir.exists():
|
|
# Count files before deletion
|
|
files = list(session_dir.iterdir())
|
|
count = len(files)
|
|
|
|
# Delete all session files
|
|
for file in files:
|
|
if file.is_file():
|
|
file.unlink()
|
|
|
|
print(f" ✅ Deleted {count} session files")
|
|
else:
|
|
print(" ⚠️ Flask session directory not found")
|
|
|
|
def clean_logs():
|
|
"""Clean any log files"""
|
|
print("📝 Cleaning log files...")
|
|
|
|
log_patterns = ['*.log', '*.log.*']
|
|
found_logs = False
|
|
|
|
for pattern in log_patterns:
|
|
for log_file in Path('.').glob(pattern):
|
|
log_file.unlink()
|
|
print(f" ✅ Deleted {log_file}")
|
|
found_logs = True
|
|
|
|
if not found_logs:
|
|
print(" ✅ No log files found")
|
|
|
|
def clean_pycache():
|
|
"""Clean Python cache files"""
|
|
print("🐍 Cleaning Python cache...")
|
|
|
|
cache_dirs = list(Path('.').rglob('__pycache__'))
|
|
pyc_files = list(Path('.').rglob('*.pyc'))
|
|
|
|
# Remove __pycache__ directories
|
|
for cache_dir in cache_dirs:
|
|
if cache_dir.is_dir():
|
|
shutil.rmtree(cache_dir)
|
|
|
|
# Remove .pyc files
|
|
for pyc_file in pyc_files:
|
|
pyc_file.unlink()
|
|
|
|
total_cleaned = len(cache_dirs) + len(pyc_files)
|
|
print(f" ✅ Cleaned {total_cleaned} cache files/directories")
|
|
|
|
def create_fresh_directories():
|
|
"""Ensure required directories exist"""
|
|
print("📁 Creating fresh directories...")
|
|
|
|
directories = [
|
|
'data',
|
|
'app/static/qr_codes',
|
|
'app/static/logos',
|
|
'flask_session'
|
|
]
|
|
|
|
for directory in directories:
|
|
Path(directory).mkdir(parents=True, exist_ok=True)
|
|
print(f" ✅ Ensured {directory} exists")
|
|
|
|
def main():
|
|
"""Main cleanup function"""
|
|
print("🧹 QR Code Manager - Data Cleanup Script")
|
|
print("=" * 50)
|
|
|
|
# Change to script directory
|
|
script_dir = Path(__file__).parent
|
|
os.chdir(script_dir)
|
|
|
|
print(f"📂 Working directory: {os.getcwd()}")
|
|
print()
|
|
|
|
# Ask for confirmation
|
|
print("⚠️ WARNING: This will delete ALL persistent data!")
|
|
print(" - All QR codes and their images")
|
|
print(" - All dynamic link pages")
|
|
print(" - All short URLs")
|
|
print(" - All Flask sessions")
|
|
print(" - All log files")
|
|
print(" - All Python cache files")
|
|
print()
|
|
|
|
confirm = input("Are you sure you want to continue? Type 'YES' to confirm: ")
|
|
|
|
if confirm != 'YES':
|
|
print("❌ Cleanup cancelled.")
|
|
return
|
|
|
|
print()
|
|
print("🚀 Starting cleanup process...")
|
|
print()
|
|
|
|
try:
|
|
# Clean different types of data
|
|
clean_json_data()
|
|
print()
|
|
|
|
clean_qr_images()
|
|
print()
|
|
|
|
clean_flask_sessions()
|
|
print()
|
|
|
|
clean_logs()
|
|
print()
|
|
|
|
clean_pycache()
|
|
print()
|
|
|
|
create_fresh_directories()
|
|
print()
|
|
|
|
print("✅ Cleanup completed successfully!")
|
|
print()
|
|
print("🎉 Your QR Code Manager is now ready for a fresh deployment!")
|
|
print(" Next steps:")
|
|
print(" 1. Start the application: python main.py")
|
|
print(" 2. Login with: admin / admin123")
|
|
print(" 3. Change the default password")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error during cleanup: {e}")
|
|
print("Please check the error and try again.")
|
|
|
|
if __name__ == '__main__':
|
|
main()
|