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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
PyGitWeb configuration: env-driven settings via pydantic-settings, constants, and loadavg.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
from pydantic import Field, field_validator, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
# Actions (most identical to gitweb.perl actions)
ACTIONS: set[str] = {
"blame",
"blame_incremental",
"blame_data",
"blame_raw",
"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).
When AUTH is true, PyGitWeb uses OAuth2 password flow (POST /token), Bearer tokens,
and browser login. Local credentials live in the auth JSON file (see PYGITWEB_AUTH_CONFIG).
If PYGITWEB_AUTH is omitted entirely (or empty), authentication defaults to enabled
and a random password is written to the auth config file on first run (see stderr).
Set PYGITWEB_AUTH=0 or PYGITWEB_AUTH=1 explicitly as needed.
"""
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: bool | None = None
AUTH_CONFIG: str = ""
@field_validator("AUTH", mode="before")
@classmethod
def _coerce_auth(cls, v: object) -> bool | None:
if v is None:
return None
if isinstance(v, str) and v.strip() == "":
return None
if isinstance(v, bool):
return v
if isinstance(v, int):
return v != 0
if isinstance(v, str):
s = v.strip().lower()
if s in ("none", "false", "0", "no", "off"):
return False
if s in ("true", "1", "yes", "on"):
return True
return None
return bool(v)
@model_validator(mode="after")
def _defaults(self) -> Settings:
if not self.PROJECTS_LIST:
self.PROJECTS_LIST = self.PROJECTROOT
return self
def _finalize_auth_settings(s: Settings) -> None:
implicit_auth = s.AUTH is None
if implicit_auth:
s.AUTH = True
if implicit_auth and s.AUTH:
cfg_path = Path(s.AUTH_CONFIG).expanduser() if s.AUTH_CONFIG else Path.home() / ".pygitweb" / "auth.json"
print(
"WARNING: PyGitWeb: PYGITWEB_AUTH was not set; authentication defaults to ON.\n"
" Set PYGITWEB_AUTH=0 to disable, or PYGITWEB_AUTH=1 and configure "
f"{cfg_path}.\n",
file=sys.stderr,
)
settings = Settings()
_finalize_auth_settings(settings)
from pygitweb.auth_config import init_auth_config # noqa: E402
auth_config = init_auth_config(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