wip
This commit is contained in:
parent
f939933a7c
commit
be6f7cfcbb
35 changed files with 1897 additions and 733 deletions
94
scripts/check_routes.py
Executable file
94
scripts/check_routes.py
Executable file
|
@ -0,0 +1,94 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Check for unused routes in Flask application
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
from flask import Flask
|
||||
|
||||
# Add the parent directory to sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
def find_all_routes():
|
||||
"""Find all route definitions in Python files"""
|
||||
routes = []
|
||||
route_pattern = re.compile(r'@\w+\.route\([\'"]([^\'"]+)[\'"]')
|
||||
|
||||
for root, _, files in os.walk('app'):
|
||||
for file in files:
|
||||
if file.endswith('.py'):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
content = f.read()
|
||||
matches = route_pattern.findall(content)
|
||||
for match in matches:
|
||||
routes.append(match)
|
||||
|
||||
return routes
|
||||
|
||||
def find_template_references():
|
||||
"""Find all url_for calls in template files"""
|
||||
references = []
|
||||
url_for_pattern = re.compile(r'url_for\([\'"]([^\'"]+)[\'"]')
|
||||
|
||||
for root, _, files in os.walk('app/templates'):
|
||||
for file in files:
|
||||
if file.endswith('.html'):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
content = f.read()
|
||||
matches = url_for_pattern.findall(content)
|
||||
for match in matches:
|
||||
references.append(match)
|
||||
|
||||
# Also check Python files for url_for calls
|
||||
for root, _, files in os.walk('app'):
|
||||
for file in files:
|
||||
if file.endswith('.py'):
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, 'r') as f:
|
||||
content = f.read()
|
||||
matches = url_for_pattern.findall(content)
|
||||
for match in matches:
|
||||
references.append(match)
|
||||
|
||||
return references
|
||||
|
||||
def check_unused_routes():
|
||||
"""Find routes that are not referenced by url_for"""
|
||||
from app import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
# Get all route endpoints from the app
|
||||
all_endpoints = set()
|
||||
for rule in app.url_map.iter_rules():
|
||||
all_endpoints.add(rule.endpoint)
|
||||
|
||||
# Get all url_for references
|
||||
all_references = set(find_template_references())
|
||||
|
||||
# Find unused endpoints
|
||||
unused_endpoints = all_endpoints - all_references
|
||||
|
||||
if unused_endpoints:
|
||||
print("The following routes are defined but not referenced in templates or code:")
|
||||
for endpoint in sorted(unused_endpoints):
|
||||
# Skip static routes, error handlers, etc.
|
||||
if endpoint.startswith('static') or endpoint == 'static':
|
||||
continue
|
||||
|
||||
print(f" - {endpoint}")
|
||||
|
||||
# Find the URL for this endpoint
|
||||
for rule in app.url_map.iter_rules():
|
||||
if rule.endpoint == endpoint:
|
||||
print(f" URL: {rule}")
|
||||
break
|
||||
else:
|
||||
print("All routes are referenced in templates or code. Good job!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_unused_routes()
|
63
scripts/cleanup.py
Executable file
63
scripts/cleanup.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Cleanup script for Flask applications
|
||||
Removes __pycache__ directories, .pyc files, and database files
|
||||
"""
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import argparse
|
||||
|
||||
def cleanup(directory, verbose=False):
|
||||
"""Clean up cache files and database files"""
|
||||
cleaned_dirs = 0
|
||||
cleaned_files = 0
|
||||
|
||||
# Files to clean
|
||||
file_patterns = ['.pyc', '.pyo', '.~', '.swp', '.swo']
|
||||
db_patterns = ['.db', '.sqlite', '.sqlite3', '-journal']
|
||||
|
||||
# Directories to clean
|
||||
dir_patterns = ['__pycache__', '.pytest_cache', '.coverage', 'htmlcov']
|
||||
|
||||
# Clean main directory
|
||||
for root, dirs, files in os.walk(directory):
|
||||
# Clean directories
|
||||
for dir_name in list(dirs):
|
||||
if dir_name in dir_patterns:
|
||||
dir_path = os.path.join(root, dir_name)
|
||||
if verbose:
|
||||
print(f"Removing directory: {dir_path}")
|
||||
shutil.rmtree(dir_path)
|
||||
cleaned_dirs += 1
|
||||
dirs.remove(dir_name)
|
||||
|
||||
# Clean files
|
||||
for file in files:
|
||||
if any(file.endswith(pattern) for pattern in file_patterns + db_patterns):
|
||||
file_path = os.path.join(root, file)
|
||||
if verbose:
|
||||
print(f"Removing file: {file_path}")
|
||||
os.remove(file_path)
|
||||
cleaned_files += 1
|
||||
|
||||
# Clean instance directory
|
||||
instance_dir = os.path.join(directory, 'instance')
|
||||
if os.path.exists(instance_dir):
|
||||
for file in os.listdir(instance_dir):
|
||||
if any(file.endswith(pattern) for pattern in db_patterns):
|
||||
file_path = os.path.join(instance_dir, file)
|
||||
if verbose:
|
||||
print(f"Removing database file: {file_path}")
|
||||
os.remove(file_path)
|
||||
cleaned_files += 1
|
||||
|
||||
print(f"Cleanup completed! Removed {cleaned_dirs} directories and {cleaned_files} files.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Clean up Flask application cache and database files")
|
||||
parser.add_argument("-v", "--verbose", action="store_true", help="Show detailed output")
|
||||
parser.add_argument("-d", "--directory", default=".", help="Directory to clean (default: current directory)")
|
||||
|
||||
args = parser.parse_args()
|
||||
cleanup(args.directory, args.verbose)
|
68
scripts/create_admin.py
Normal file
68
scripts/create_admin.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Create an admin user for the application
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import getpass
|
||||
from flask import Flask
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
# Add the parent directory to sys.path
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from app import create_app
|
||||
from app.core.extensions import db
|
||||
from app.core.auth import User
|
||||
|
||||
def create_admin_user(email=None, password=None):
|
||||
"""Create an admin user in the database"""
|
||||
app = create_app()
|
||||
|
||||
with app.app_context():
|
||||
# Check if users already exist
|
||||
if User.query.count() > 0:
|
||||
print("Users already exist in the database.")
|
||||
choice = input("Do you want to create another admin user? (y/n): ")
|
||||
if choice.lower() != 'y':
|
||||
print("Operation cancelled.")
|
||||
return
|
||||
|
||||
# Prompt for email if not provided
|
||||
if not email:
|
||||
email = input("Enter admin email: ")
|
||||
|
||||
# Check if user with this email already exists
|
||||
existing_user = User.query.filter_by(email=email).first()
|
||||
if existing_user:
|
||||
print(f"User with email {email} already exists!")
|
||||
return
|
||||
|
||||
# Prompt for password if not provided
|
||||
if not password:
|
||||
password = getpass.getpass("Enter admin password: ")
|
||||
confirm_password = getpass.getpass("Confirm password: ")
|
||||
|
||||
if password != confirm_password:
|
||||
print("Passwords do not match!")
|
||||
return
|
||||
|
||||
# Create the admin user
|
||||
admin = User(email=email, is_admin=True)
|
||||
admin.set_password(password)
|
||||
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
|
||||
print(f"Admin user created successfully: {email}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="Create an admin user")
|
||||
parser.add_argument("--email", help="Admin user email")
|
||||
parser.add_argument("--password", help="Admin user password")
|
||||
|
||||
args = parser.parse_args()
|
||||
create_admin_user(args.email, args.password)
|
Loading…
Add table
Add a link
Reference in a new issue