ddocs.templates.repo_cloner¶
Clone a git repository into a temporary directory and copy files out of it,
with optional HTTPS authentication. Used to fetch the Deltares
LatexInstallation templates for the get-tex-template command.
ddocs.templates.repo_cloner ¶
Clone the Deltares LaTeX template repository and copy files out of it.
Authentication precedence (first available wins):
- Token --
tokenarg orGITHUB_TOKEN/GH_TOKENenv (HTTPS). - Basic --
username/passwordargs orGIT_USERNAME/GIT_PASSWORDenv vars. - SSH -- when
prefer_sshis set and no credentials are given, clone via an SSH URL using the machine's key (typical on developer laptops). - Anonymous HTTPS -- public repos only.
CI should set GITHUB_TOKEN; laptops typically use their existing SSH key.
RepoCloner ¶
Clone a git repository into a temporary directory and manage file operations.
Authentication is resolved from the supplied arguments or the environment with a fixed
precedence: token, then username+password, then SSH (when prefer_ssh is set), then an
anonymous HTTPS clone. This lets the same code authenticate via a token in CI and via the
developer's SSH key on a laptop.
Examples:
- Construct with a token and read back the stored credentials:
>>> from ddocs.templates.repo_cloner import RepoCloner >>> cloner = RepoCloner("https://github.com/Deltares/LatexInstallation", token="ghp_demo") >>> cloner.token 'ghp_demo' >>> cloner.repo_url 'https://github.com/Deltares/LatexInstallation' - A token turns the public URL into an authenticated HTTPS clone URL:
>>> from ddocs.templates.repo_cloner import RepoCloner >>> RepoCloner("https://github.com/Deltares/LatexInstallation", token="ghp_demo")._resolve_clone_url() 'https://x-access-token:ghp_demo@github.com/Deltares/LatexInstallation'
See Also
clone_repo_cli: Uses this class to fetch the Deltares LaTeX templates.
Source code in src\ddocs\templates\repo_cloner.py
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
clone ¶
clone() -> Path
Clone the repository into a temporary directory.
The clone URL is built by :meth:_resolve_clone_url, so authentication
(token, basic auth, or SSH) is applied automatically.
Security note: when a token/password is used over HTTPS, git stores the
credential in the temporary clone's .git/config remote URL. This is removed
with the temp directory on :meth:cleanup / context-manager exit, but the
credential lives on disk for the lifetime of the clone.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the cloned repository. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the clone fails. The message is scrubbed of any token/password so secrets are never written to logs. |
Examples:
- Clone a repository and use the returned path (network access required):
>>> from ddocs.templates.repo_cloner import RepoCloner >>> cloner = RepoCloner("https://github.com/owner/repo", token="ghp_demo") >>> repo_path = cloner.clone() # doctest: +SKIP >>> sorted(p.name for p in repo_path.iterdir()) # doctest: +SKIP ['.git', 'README.md'] - A failed clone raises a RuntimeError whose message hides the token:
>>> from ddocs.templates.repo_cloner import RepoCloner >>> cloner = RepoCloner("https://github.com/owner/does-not-exist", token="ghp_demo") >>> try: # doctest: +SKIP ... cloner.clone() ... except RuntimeError as err: ... "ghp_demo" in str(err) False
Source code in src\ddocs\templates\repo_cloner.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
move_file ¶
move_file(source_rel_path: str | Path, destination: str | Path) -> Path
Move a file from the cloned repository to a new location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_rel_path
|
str | Path
|
Relative path to the file within the cloned repository |
required |
destination
|
str | Path
|
Destination path (can be absolute or relative) |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the moved file |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source file doesn't exist |
RuntimeError
|
If repository hasn't been cloned yet |
Source code in src\ddocs\templates\repo_cloner.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
copy_file ¶
copy_file(source_rel_path: str | Path, destination: str | Path) -> Path
Copy a file from the cloned repository to a new location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_rel_path
|
str | Path
|
Relative path to the file within the cloned repository |
required |
destination
|
str | Path
|
Destination path (can be absolute or relative) |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the copied file |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source file doesn't exist |
RuntimeError
|
If repository hasn't been cloned yet |
Source code in src\ddocs\templates\repo_cloner.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
list_files ¶
list_files(relative_path: str = '', pattern: str = '*') -> list[Path]
List files in the cloned repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relative_path
|
str
|
Relative path within the repository to list files from |
''
|
pattern
|
str
|
Glob pattern to filter files (default: "*") |
'*'
|
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of Path objects matching the pattern |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If repository hasn't been cloned yet |
Source code in src\ddocs\templates\repo_cloner.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
cleanup ¶
cleanup()
Remove the temporary directory and all its contents.
Source code in src\ddocs\templates\repo_cloner.py
366 367 368 369 370 371 372 373 374 | |
clone_repo_cli ¶
clone_repo_cli(output_dir: Path, token: Optional[str] = None, username: Optional[str] = None, password: Optional[str] = None, prefer_ssh: bool = True) -> int
Clone the Deltares LatexInstallation repo and copy template files to output_dir.
Uses :class:RepoCloner as a context manager so the temporary clone is always
removed, even if copying fails. Credentials are resolved by RepoCloner with the
precedence token -> username/password -> SSH (when prefer_ssh) -> anonymous, so
passing nothing falls back to the machine's SSH key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_dir
|
Path
|
Directory the template files are copied into. Created if missing. |
required |
token
|
Optional[str]
|
Personal access / app token for HTTPS auth. Falls back to the
|
None
|
username
|
Optional[str]
|
Username for HTTPS basic auth. Falls back to |
None
|
password
|
Optional[str]
|
Password/token for HTTPS basic auth. Falls back to |
None
|
prefer_ssh
|
bool
|
When no token or username/password is available, clone via SSH (using the machine's key) instead of anonymously. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
|
Examples:
- Fetch the templates into a local folder (network access required):
>>> from pathlib import Path >>> from ddocs.templates.repo_cloner import clone_repo_cli >>> clone_repo_cli(Path("./templates")) # doctest: +SKIP 0 - The returned code can drive a process exit status:
>>> import sys >>> from pathlib import Path >>> from ddocs.templates.repo_cloner import clone_repo_cli >>> sys.exit(clone_repo_cli(Path("./templates"))) # doctest: +SKIP
See Also
RepoCloner: The context manager used to clone and clean up the temporary repo.
Source code in src\ddocs\templates\repo_cloner.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |