79 lines
2.6 KiB
Python
Executable File
79 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python
|
|
"""Django's command-line utility for administrative tasks."""
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def main():
|
|
"""Run administrative tasks."""
|
|
try:
|
|
from dotenv import load_dotenv
|
|
env_path = Path(__file__).resolve().parent.parent / '.env'
|
|
if env_path.exists():
|
|
load_dotenv(env_path)
|
|
except ImportError:
|
|
pass
|
|
|
|
# --- FIX: Preload libraries for WeasyPrint/Pango ---
|
|
# Manually load libraries using absolute paths since LD_LIBRARY_PATH
|
|
# changes don't affect dlopen() in the running process.
|
|
import ctypes
|
|
import ctypes.util
|
|
import traceback
|
|
|
|
lib_paths = [
|
|
'/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0',
|
|
'/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0',
|
|
'/usr/lib/x86_64-linux-gnu/libfontconfig.so.1',
|
|
'/usr/lib/x86_64-linux-gnu/libcairo.so.2',
|
|
'/usr/lib/x86_64-linux-gnu/libpango-1.0.so.0',
|
|
'/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0',
|
|
]
|
|
|
|
for path in lib_paths:
|
|
try:
|
|
ctypes.CDLL(path)
|
|
except OSError:
|
|
# Continue even if one fails
|
|
pass
|
|
|
|
# Try to import weasyprint to see if it works
|
|
try:
|
|
import weasyprint
|
|
except Exception as e:
|
|
# Log the specific error for debugging
|
|
error_msg = f"WeasyPrint Import Error: {str(e)}\n{traceback.format_exc()}"
|
|
sys.stderr.write(error_msg)
|
|
try:
|
|
with open("weasyprint_error.log", "w") as f:
|
|
f.write(error_msg)
|
|
except Exception:
|
|
pass
|
|
|
|
# Fallback to mock if system dependencies are missing or import fails
|
|
import types
|
|
class MockHTML:
|
|
def __init__(self, string=None, base_url=None):
|
|
pass
|
|
def write_pdf(self, target=None):
|
|
raise OSError("WeasyPrint system dependencies are missing. PDF generation is disabled.")
|
|
|
|
mock_wp = types.ModuleType("weasyprint")
|
|
mock_wp.HTML = MockHTML
|
|
sys.modules["weasyprint"] = mock_wp
|
|
# ---------------------------------------------------
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
|
try:
|
|
from django.core.management import execute_from_command_line
|
|
except ImportError as exc:
|
|
raise ImportError(
|
|
"Couldn't import Django. Are you sure it's installed and "
|
|
"available on your PYTHONPATH environment variable? Did you "
|
|
"forget to activate a virtual environment?"
|
|
) from exc
|
|
execute_from_command_line(sys.argv)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|