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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
PyGitWeb configuration: env-driven settings via pydantic-settings, constants, and loadavg.
"""
from __future__ import annotations
import os
from pathlib import Path
from pydantic import Field, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
# Actions (most identical to gitweb.perl actions)
ACTIONS: set[str] = {
"blame",
"blame_incremental",
"blame_data",
"blobdiff",
"blobpatch",
"blob",
"blob_plain",
"commitdiff",
"commit",
"heads",
"history",
"log",
"patch",
"patches",
"remotes",
"rss",
"atom",
"search",
"search_help",
"shortlog",
"summary",
"tag",
"tags",
"tree",
"snapshot",
"object",
"opml",
"project_list",
"project_index",
}
# Map file extension to highlight.js language
BLOB_LANG: dict[str, str] = {
"py": "python",
"js": "javascript",
"ts": "typescript",
"jsx": "javascript",
"tsx": "typescript",
"html": "html",
"htm": "html",
"css": "css",
"scss": "scss",
"json": "json",
"md": "markdown",
"sh": "bash",
"bash": "bash",
"yml": "yaml",
"yaml": "yaml",
"xml": "xml",
"go": "go",
"rs": "rust",
"java": "java",
"c": "c",
"h": "c",
"cpp": "cpp",
"cc": "cpp",
"cxx": "cpp",
"sql": "sql",
"r": "r",
"rb": "ruby",
"php": "php",
"swift": "swift",
"kt": "kotlin",
"vue": "xml",
"toml": "toml",
"ini": "ini",
"cfg": "ini",
"dockerfile": "dockerfile",
}
class Settings(BaseSettings):
"""
Runtime settings sourced from PYGITWEB_* env vars (and optionally a .env file).
The auth provider:
"None" or unset disables authentication. Otherwise "module.path.ClassName" is imported
lazily in main.py. Planned providers: Root, SSH, OAuth2, OIDC, Matrix, LDAP.
"""
model_config = SettingsConfigDict(
env_prefix="PYGITWEB_",
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
validate_assignment=True,
extra="ignore",
)
PROJECTROOT: str = Field(default_factory=lambda: str(Path.home()))
PROJECTS_LIST: str = "" # falls back to PROJECTROOT when blank
PROJECT_MAXDEPTH: int = 1
SITE_NAME: str = "PyGitWeb"
EXPORT_OK: str = ""
LIST_ALL: bool = True
STRICT_EXPORT: bool = False
GIT: str = "git"
MAXLOAD: float | None = None
AUTH: str | None = None
ADMIN_USER: str | None = None
ADMIN_PASSWORD: str | None = None
SESSION_TIMEOUT: int = 3600 * 24 * 7
@model_validator(mode="after")
def _defaults(self) -> Settings:
if not self.PROJECTS_LIST:
self.PROJECTS_LIST = self.PROJECTROOT
if self.AUTH == "None":
self.AUTH = None
return self
settings = Settings()
def get_loadavg() -> float:
"""First element of load average, or 0 if unavailable. Port of get_loadavg."""
try:
return os.getloadavg()[0]
except (OSError, AttributeError):
pass
try:
with open("/proc/loadavg") as f:
return float(f.read().split()[0])
except (OSError, ValueError):
return 0.0
def check_loadavg() -> None:
"""Raise 503 if load exceeds MAXLOAD."""
if settings.MAXLOAD is not None and get_loadavg() > settings.MAXLOAD:
raise RuntimeError("503:The load average on the server is too high")