summaryrefslogtreecommitdiff
path: root/src/tox/journal/main.py
blob: 891c65828a66824707d13bb03c3779f2353a4f3f (plain)
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
"""Generate json report of a tox run"""
import socket
import sys
from typing import Any, Dict

from tox.version import version

from .env import EnvJournal


class Journal:
    """The result of a tox session"""

    def __init__(self, enabled: bool) -> None:
        self._enabled = enabled
        self._content: Dict[str, Any] = {}
        self._env: Dict[str, EnvJournal] = {}

        if self._enabled:
            self._content.update(
                {
                    "reportversion": "1",
                    "toxversion": version,
                    "platform": sys.platform,
                    "host": socket.getfqdn(),
                },
            )

    def get_env_journal(self, name: str) -> EnvJournal:
        """Return the env log of an environment (create on first call)"""
        if name not in self._env:
            env = EnvJournal(self._enabled, name)
            self._env[name] = env
        return self._env[name]

    @property
    def content(self) -> Dict[str, Any]:
        test_env_journals: Dict[str, Any] = {}
        for name, value in self._env.items():
            test_env_journals[name] = value.content
        if test_env_journals:
            self._content["testenvs"] = test_env_journals
        return self._content

    def __bool__(self) -> bool:
        return self._enabled


__all__ = ("Journal",)