Skip to content

Commit f6d83e2

Browse files
authored
refactor: separate static and dynamic placeholders (#157)
* refactor #108: separate static and dynamic placeholders Signed-off-by: sushant-suse <[email protected]> * refactor #108: updated test file Signed-off-by: sushant-suse <[email protected]> * refactor #108: align config schema and naming with feedback Signed-off-by: sushant-suse <[email protected]> * docs #108: restore and update naming convention comments in env.example.toml Signed-off-by: sushant-suse <[email protected]> --------- Signed-off-by: sushant-suse <[email protected]>
1 parent efaca8c commit f6d83e2

9 files changed

Lines changed: 101 additions & 66 deletions

File tree

changelog.d/108.refactor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactored environment configuration to separate static path placeholders from runtime dynamic placeholders. This prevents invalid directory creation during configuration validation.

etc/docbuild/env.example.toml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ canonical_url_domain = "https://docs.example.com"
3232
# Key naming convention:
3333
# * Suffix "_dir": contains only directory paths
3434
# * Suffix "_path": contains directory and filename paths
35+
# * Suffix "_dyn": contains placeholders that are replaced at runtime
3536
root_config_dir = "/etc/docbuild"
3637
jinja_dir = "{root_config_dir}/jinja-doc-suse-com"
3738
config_dir = "{root_config_dir}/config.d"
@@ -42,31 +43,38 @@ base_server_cache_dir = "{base_cache_dir}/{server.name}"
4243
base_tmp_dir = "/var/tmp/docbuild"
4344
repo_dir = "/data/docserv/repos/permanent-full/"
4445
tmp_repo_dir = "/data/docserv/repos/temporary-branches/"
45-
# cache_dir = "{base_cache_dir}/{server.name}"
4646
meta_cache_dir = "{base_cache_dir}/{server.name}/meta"
4747

4848

4949
[paths.tmp]
50-
# Section "paths.tmp": Definies temporary paths
50+
# Section "paths.tmp": Defines temporary paths
5151
# Paths can hold placeholders in brackets.
5252
tmp_base_dir = "{paths.base_tmp_dir}"
5353
tmp_dir = "{tmp_base_dir}/doc-example-com"
5454
tmp_metadata_dir = "{tmp_dir}/metadata"
5555
tmp_deliverable_dir = "{paths.tmp.tmp_dir}/deliverable/"
56-
tmp_build_dir = "{tmp_dir}/build/{{product}}-{{docset}}-{{lang}}"
56+
57+
# UPDATED: Split build_dir into base (validated) and dyn (template)
58+
tmp_build_base_dir = "{tmp_dir}/build"
59+
tmp_build_dir_dyn = "{{product}}-{{docset}}-{{lang}}"
60+
5761
tmp_out_dir = "{tmp_dir}/out/"
5862
log_dir = "{tmp_dir}/log/"
59-
tmp_deliverable_name = "{{product}}_{{docset}}_{{lang}}_XXXXXX"
63+
64+
# UPDATED: Renamed to indicate dynamic template
65+
tmp_deliverable_name_dyn = "{{product}}_{{docset}}_{{lang}}_XXXXXX"
6066

6167

6268
[paths.target]
63-
# Section "paths.target": Definies target paths
64-
target_dir = "[email protected]:/srv/docs"
69+
# Section "paths.target": Defines target paths
70+
# UPDATED: Split target_dir into base and dyn
71+
target_base_dir = "[email protected]:/srv/docs"
72+
target_dir_dyn = "{{product}}"
6573
backup_dir = "/data/docbuild/external-builds/"
6674

6775

6876
[build]
69-
# Section "build": General build parameters, independant from any specific
77+
# Section "build": General build parameters, independent from any specific
7078

7179

7280
[build.daps]

src/docbuild/cli/cmd_metadata/metaprocess.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def process_deliverable(
8484
deliverable: Deliverable,
8585
*,
8686
repo_dir: Path,
87-
temp_repo_dir: Path,
87+
tmp_repo_dir: Path,
8888
base_cache_dir: Path,
8989
meta_cache_dir: Path,
9090
dapstmpl: str,
@@ -98,8 +98,8 @@ async def process_deliverable(
9898
:param deliverable: The Deliverable object to process.
9999
:param repo_dir: The permanent repo path taken from the env
100100
config ``paths.repo_dir``
101-
:param temp_repo_dir: The temporary repo path taken from the env
102-
config ``paths.temp_repo_dir``
101+
:param tmp_repo_dir: The temporary repo path taken from the env
102+
config ``paths.tmp_repo_dir``
103103
:param base_cache_dir: The base path of the cache directory taken
104104
from the env config ``paths.base_cache_dir``
105105
:param meta_cache_dir: The ath of the metadata directory taken
@@ -132,7 +132,7 @@ async def process_deliverable(
132132

133133
try:
134134
async with PersistentOnErrorTemporaryDirectory(
135-
dir=str(temp_repo_dir),
135+
dir=str(tmp_repo_dir),
136136
prefix=f"clone-{prefix}_",
137137
) as worktree_dir:
138138
# 1. Ensure the bare repository exists/updated using ManagedGitRepo,
@@ -264,7 +264,7 @@ async def process_doctype(
264264
# if not available:
265265
meta_cache_dir: Path = env.paths.meta_cache_dir
266266
# Cloned temporary repo:
267-
temp_repo_dir: Path = env.paths.temp_repo_dir
267+
tmp_repo_dir: Path = env.paths.tmp_repo_dir
268268

269269
deliverables: list[Deliverable] = await asyncio.to_thread(
270270
get_deliverable_from_doctype,
@@ -284,7 +284,7 @@ async def process_doctype(
284284
process_deliverable(
285285
deliverable,
286286
repo_dir=repo_dir,
287-
temp_repo_dir=temp_repo_dir,
287+
tmp_repo_dir=tmp_repo_dir,
288288
base_cache_dir=base_cache_dir,
289289
meta_cache_dir=meta_cache_dir,
290290
dapstmpl=dapsmetatmpl,

src/docbuild/cli/defaults.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"paths": {
1616
"config_dir": "/etc/docbuild",
1717
"repo_dir": "/data/docserv/repos/permanent-full/",
18-
"temp_repo_dir": "/data/docserv/repos/temporary-branches/",
18+
"tmp_repo_dir": "/data/docserv/repos/temporary-branches/",
1919
},
2020
"paths.tmp": {
2121
"tmp_base_dir": "/tmp",
@@ -43,8 +43,7 @@
4343
"root_config_dir": "/etc/docbuild",
4444
"jinja_dir": "/etc/docbuild/jinja",
4545
"server_rootfiles_dir": "/etc/docbuild/root-files",
46-
"repo_dir": "/data/docserv/repos/permanent-full/",
47-
"temp_repo_dir": "/data/docserv/repos/temporary-branches/",
46+
"tmp_repo_dir": "/data/docserv/repos/temporary-branches/",
4847
"base_cache_dir": "/var/cache/docserv",
4948
"base_server_cache_dir": "/var/cache/docserv/default",
5049
"meta_cache_dir": "/var/cache/docserv/default/meta",
@@ -54,13 +53,17 @@
5453
"tmp_dir": "{tmp_base_dir}/default-local",
5554
"tmp_deliverable_dir": "{tmp_dir}/deliverable",
5655
"tmp_metadata_dir": "{tmp_dir}/metadata",
57-
"tmp_build_dir": "{tmp_dir}/build/default",
56+
# build_dir has become build_base_dir + dir_dyn
57+
"tmp_build_base_dir": "{tmp_dir}/build",
5858
"tmp_out_dir": "{tmp_dir}/out",
5959
"log_dir": "{tmp_dir}/log",
60-
"tmp_deliverable_name": "default_deliverable",
60+
# deliverable_name has become name_dyn
61+
"tmp_deliverable_name_dyn": "{{product}}_{{docset}}_{{lang}}_XXXXXX",
6162
},
6263
"target": {
63-
"target_dir": "file:///tmp/docbuild/target",
64+
# target_dir has become base_dir + dir_dyn
65+
"target_base_dir": "file:///tmp/docbuild/target",
66+
"target_dir_dyn": "{{product}}",
6467
"backup_dir": "/tmp/docbuild/backup",
6568
},
6669
},

src/docbuild/models/config/env.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,23 @@ class EnvTmpPaths(BaseModel):
205205
)
206206
"Temporary metadata directory."
207207

208-
tmp_build_dir: str = Field(
209-
title="Temporary Build Directory",
210-
description="Temporary directory for intermediate files (contains placeholders).",
211-
examples=[
212-
"/var/tmp/docbuild/doc-example-com/build/{{product}}-{{docset}}-{{lang}}"
213-
],
208+
# SPLIT: static base directory (validated)
209+
tmp_build_base_dir: EnsureWritableDirectory = Field(
210+
title="Temporary Build Base Directory",
211+
description="The base directory where intermediate build files are stored.",
212+
examples=["/var/tmp/docbuild/doc-example-com/build/"],
214213
)
215-
"Temporary build output directory."
214+
"Base path for build output."
215+
216+
# SPLIT: dynamic suffix (string only, not validated as path)
217+
# Added a default value so it's not required in defaults.py or user configs
218+
tmp_build_dir_dyn: str = Field(
219+
default="{{product}}-{{docset}}-{{lang}}",
220+
title="Temporary Build Directory Suffix",
221+
description="The dynamic part of the build path containing runtime placeholders.",
222+
examples=["{{product}}-{{docset}}-{{lang}}"],
223+
)
224+
"Dynamic suffix for build directory."
216225

217226
tmp_out_dir: EnsureWritableDirectory = Field(
218227
title="Temporary Output Directory",
@@ -228,28 +237,37 @@ class EnvTmpPaths(BaseModel):
228237
)
229238
"Directory for log files."
230239

231-
tmp_deliverable_name: str = Field(
232-
title="Temporary Deliverable Name",
240+
# RENAMED: To indicate this is a dynamic template
241+
tmp_deliverable_name_dyn: str = Field(
242+
title="Temporary Deliverable Name (Dynamic)",
233243
description=(
234-
"The name used for the current deliverable being built "
235-
"(e.g., branch name or version)."
244+
"The dynamic template name used for the current deliverable being built."
236245
),
237246
examples=["{{product}}_{{docset}}_{{lang}}_XXXXXX"],
238247
)
239-
"Temporary deliverable name."
248+
"Temporary deliverable name template."
240249

241250

242251
class EnvTargetPaths(BaseModel):
243252
"""Defines target paths."""
244253

245254
model_config = ConfigDict(extra="forbid")
246255

247-
target_dir: str = Field(
248-
title="Target Server Deployment Directory",
249-
description="The final remote destination for the built documentation",
256+
# SPLIT: static base directory or remote destination
257+
target_base_dir: str = Field(
258+
title="Target Server Base Directory",
259+
description="The static remote destination or base path for built documentation.",
250260
examples=["[email protected]:/srv/docs"],
251261
)
252-
"The destination directory for final built documentation."
262+
"The base destination for final built documentation."
263+
264+
# SPLIT: dynamic suffix
265+
target_dir_dyn: str = Field(
266+
title="Target Directory Suffix",
267+
description="The dynamic suffix of the remote path containing runtime placeholders.",
268+
examples=["{{product}}"],
269+
)
270+
"Dynamic suffix for final remote destination."
253271

254272
backup_dir: Path = Field(
255273
title="Build Server Backup Directory",

tests/cli/cmd_config/sample-env.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ canonical_url_domain = "https://docs.example.com"
3131
# Paths can hold placeholders in brackets.
3232
config_dir = "/etc/docbuild"
3333
repo_dir = "/data/docserv/repos/permanent-full/"
34-
temp_repo_dir = "/data/docserv/repos/temporary-branches/"
34+
tmp_repo_dir = "/data/docserv/repos/temporary-branches/"
3535
base_cache_dir = "/var/cache/docserv"
3636
cache_dir = "{base_cache_dir}/{server.name}"
3737
meta_cache_dir = "{base_cache_dir}/meta"

tests/cli/cmd_metadata/test_cmd_metadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def mock_envconfig(tmp_path: Path) -> Mock:
3535
mock_paths.repo_dir = tmp_path / "repos"
3636
mock_paths.base_cache_dir = tmp_path / "cache"
3737
mock_paths.meta_cache_dir = tmp_path / "cache" / "metadata"
38-
mock_paths.temp_repo_dir = tmp_path / "temp_repos"
38+
mock_paths.tmp_repo_dir = tmp_path / "tmp_repos"
3939

4040
mock_build = Mock()
4141
mock_build.daps.meta = "daps-command-template"
@@ -275,7 +275,7 @@ def setup_paths(self, tmp_path: Path, deliverable: Deliverable) -> dict:
275275
"""Set up common paths and directories for tests."""
276276
paths = {
277277
"repo_dir": tmp_path / "repos",
278-
"temp_repo_dir": tmp_path / "temp_repos",
278+
"tmp_repo_dir": tmp_path / "tmp_repos",
279279
"base_cache_dir": tmp_path / "cache",
280280
"meta_cache_dir": tmp_path / "cache" / "metadata",
281281
}
@@ -460,7 +460,7 @@ async def test_missing_paths_in_config_raises_error(
460460
# Ensure other paths are set, even if repo_dir is missing
461461
mock_envconfig.paths.base_cache_dir = Path("/fake/cache")
462462
mock_envconfig.paths.meta_cache_dir = Path("/fake/cache/meta")
463-
mock_envconfig.paths.temp_repo_dir = Path("/fake/temp")
463+
mock_envconfig.paths.tmp_repo_dir = Path("/fake/tmp")
464464
context_missing_path.envconfig = mock_envconfig
465465

466466
with pytest.raises(AttributeError):

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def env_content(default_env_config_filename: Path) -> Path:
6565
[paths]
6666
config_dir = "/etc/docbuild"
6767
repo_dir = "/data/docserv/repos/permanent-full/"
68-
temp_repo_dir = "/data/docserv/repos/temporary-branches/"
68+
tmp_repo_dir = "/data/docserv/repos/temporary-branches/"
6969
7070
[paths.tmp]
7171
tmp_base_dir = "/tmp"

0 commit comments

Comments
 (0)