-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
114 lines (89 loc) · 3.62 KB
/
main.py
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
import os
import subprocess
import sys
import typing as t
from pathlib import Path
from importlib.resources import files
TMP_DIR = Path("tmp")
WORKDIR = Path.home() / "workspace" / "multirepo-prototype"
def get_abspath(name: str) -> Path:
return Path(WORKDIR / name).absolute()
def cast_bool(value: str) -> bool:
return False if value.lower() in ("f", "false") else True
def cast_list(value: str) -> t.List[str]:
return [v.strip() for v in value.split(",") if v]
class Config:
"""
Configuration shared among CLI and mkdocs_macro.py hooks.
Params:
mkdocs_file: the base mkdocs used in serving/building
repolist: the configuration repositories (which and how to fetch)
clear_cache: whether to clear cache before downloading from remote
"""
def __init__(self, from_environ: bool = False):
if from_environ is False:
self.verbose = False
self.workdir = Path().absolute()
self.mkdocs_file = files("pulp_docs").joinpath("data/mkdocs.yml")
self.repolist = files("pulp_docs").joinpath("data/repolist.yml")
self.clear_cache = False
if env_mkdocs := os.environ.get("PULPDOCS_MKDOCS_FILE"):
self.mkdocs_file = Path(env_mkdocs)
self.disabled = []
else:
self.verbose = cast_bool(os.environ["PULPDOCS_VERBOSE"])
self.workdir = Path(os.environ["PULPDOCS_WORKDIR"])
self.mkdocs_file = Path(os.environ["PULPDOCS_MKDOCS_FILE"])
self.repolist = Path(os.environ["PULPDOCS_REPOLIST"])
self.clear_cache = cast_bool(os.environ["PULPDOCS_CLEAR_CACHE"])
self.disabled = cast_list(os.environ.get("PULPDOCS_DISABLED", ""))
self.watch: list[Path] = []
self.livereload = True
self.test_mode = cast_bool(os.environ.get("PULPDOCS_TEST_MODE", "f"))
def get_environ_dict(self):
return {f"PULPDOCS_{k.upper()}": str(v) for k, v in self.__dict__.items()}
class PulpDocs:
"""Main instance of pulp docs"""
def serve(self, config: Config, dry_run: bool = False):
# Process option to pass to command
cmd = ["mkdocs", "serve"]
env = os.environ.copy()
env.update(config.get_environ_dict())
watch_list = [("--watch", watched) for watched in config.watch]
flag_list = []
if config.livereload is False:
flag_list.append(("--no-livereload",))
options: t.List[tuple] = [("--config-file", config.mkdocs_file)]
options.extend(watch_list)
options.extend(flag_list)
for opt in options:
cmd.extend(opt)
# Run command
print("Running:", " ".join(str(s) for s in cmd))
if dry_run is True:
print("Dry run mode.")
return
subprocess.run(cmd, env=env)
def build(
self, config: Config, dry_run: bool = False, target: t.Optional[Path] = None
):
# TODO: implement target
# Process option to pass to command
cmd = ["mkdocs", "build"]
env = os.environ.copy()
env.update(config.get_environ_dict())
options = (
("--config-file", config.mkdocs_file),
("--site-dir", str(Path("site").absolute())),
)
for opt in options:
cmd.extend(opt)
# Run command
print("Building:", " ".join(str(s) for s in cmd))
if dry_run is True:
print("Dry run mode.")
return
result = subprocess.run(cmd, env=env)
sys.exit(result.returncode)
def status(self, config: Config, dry_run: bool = False):
raise NotImplementedError