File size: 3,188 Bytes
3060aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import importlib.util
import datetime
import logging
from logging.handlers import RotatingFileHandler

class PyFundamentsDebug:
    """
    Debug-Klasse für PyFundaments.
    Liest ENV-Variablen und konfiguriert Logging.
    Gibt Startup-Infos aus, wenn PYFUNDAMENTS_DEBUG=true.
    """

    def __init__(self):
        # Debug aktivieren mit ENV VAR (default: False)
        self.enabled = os.getenv("PYFUNDAMENTS_DEBUG", "false").lower() == "true"

        # Log-Level aus ENV lesen, default INFO
        log_level_str = os.getenv("LOG_LEVEL", "INFO").upper()
        self.log_level = getattr(logging, log_level_str, logging.INFO)

        # Logs in /tmp oder stdout? (default False)
        self.log_to_tmp = os.getenv("LOG_TO_TMP", "false").lower() == "true"

        # Öffentliches Logging an/aus (default True)
        self.enable_public_logs = os.getenv("ENABLE_PUBLIC_LOGS", "true").lower() == "true"

        # Logger Setup
        self.logger = logging.getLogger('pyfundaments_debug')
        self._setup_logger()

    def _setup_logger(self):
        if not self.enable_public_logs:
            # Nur kritische Fehler im Log, wenn deaktiviert
            logging.basicConfig(level=logging.CRITICAL)
            return

        handlers = []
        if self.log_to_tmp:
            log_file = '/tmp/pyfundaments_debug.log'
            file_handler = RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=3)
            handlers.append(file_handler)
        handlers.append(logging.StreamHandler(sys.stdout))

        logging.basicConfig(
            level=self.log_level,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=handlers
        )

    def run(self):
        if not self.enabled:
            return

        self.logger.info(f"==== PYFUNDAMENTS DEBUG STARTUP ({datetime.datetime.now()}) ====")
        self.logger.info(f"Python version: {sys.version}")
        self.logger.info(f"CWD: {os.getcwd()}")
        self.logger.info(f"sys.path: {sys.path}")

        fund_dir = "fundaments"
        self.logger.info(f"fundaments exists: {os.path.isdir(fund_dir)}")

        required_files = [
            "__init__.py",
            "access_control.py",
            "postgresql.py",
            "config_handler.py",
            "security.py",
            "user_handler.py",
            "encryption.py"
        ]

        for file in required_files:
            path = os.path.join(fund_dir, file)
            exists = os.path.isfile(path)
            readable = os.access(path, os.R_OK)
            self.logger.info(f"{file} exists: {exists} | readable: {readable}")

        if os.path.isdir(fund_dir):
            self.logger.info(f"Listing fundaments contents: {os.listdir(fund_dir)}")
        else:
            self.logger.warning("fundaments folder not found.")

        spec = importlib.util.find_spec("fundaments")
        self.logger.info(f"fundaments importable: {spec is not None}")

        conflicts = [mod for mod in sys.modules if mod == "fundaments"]
        self.logger.info(f"Name conflict in sys.modules: {conflicts}")

        self.logger.info("==== PYFUNDAMENTS DEBUG END ====")