Browse Source

Merge "refactor: Add convenient callback for processing site repos"

Zuul 5 months ago
parent
commit
420f7ae281
2 changed files with 21 additions and 11 deletions
  1. 20
    11
      pegleg/cli.py
  2. 1
    0
      pegleg/engine/util/git.py

+ 20
- 11
pegleg/cli.py View File

@@ -29,6 +29,18 @@ CONTEXT_SETTINGS = {
29 29
     'help_option_names': ['-h', '--help'],
30 30
 }
31 31
 
32
+
33
+def _process_repositories_callback(ctx, param, value):
34
+    """Convenient callback for ``@click.argument(site_name)``.
35
+
36
+    Automatically processes repository information for the specified site. This
37
+    entails cloning all requires repositories and checking out specified
38
+    references for each repository.
39
+    """
40
+    engine.repository.process_repositories(value)
41
+    return value
42
+
43
+
32 44
 MAIN_REPOSITORY_OPTION = click.option(
33 45
     '-r',
34 46
     '--site-repository',
@@ -103,6 +115,9 @@ WARN_LINT_OPTION = click.option(
103 115
     multiple=True,
104 116
     help='Warn if linting check fails. -w takes priority over -x.')
105 117
 
118
+SITE_REPOSITORY_ARGUMENT = click.argument(
119
+    'site_name', callback=_process_repositories_callback)
120
+
106 121
 
107 122
 @click.group(context_settings=CONTEXT_SETTINGS)
108 123
 @click.option(
@@ -234,7 +249,7 @@ def site(*, site_repository, clone_path, extra_repositories, repo_key,
234 249
     'warn_lint',
235 250
     multiple=True,
236 251
     help='Warn if linting check fails. -w takes priority over -x.')
237
-@click.argument('site_name')
252
+@SITE_REPOSITORY_ARGUMENT
238 253
 def collect(*, save_location, validate, exclude_lint, warn_lint, site_name):
239 254
     """Collects documents into a single site-definition.yaml file, which
240 255
     defines the entire site definition and contains all documents required
@@ -246,9 +261,6 @@ def collect(*, save_location, validate, exclude_lint, warn_lint, site_name):
246 261
     Collect can lint documents prior to collection if the ``--validate``
247 262
     flag is optionally included.
248 263
     """
249
-
250
-    engine.repository.process_repositories(site_name)
251
-
252 264
     if validate:
253 265
         # Lint the primary repo prior to document collection.
254 266
         _lint_helper(
@@ -267,7 +279,7 @@ def collect(*, save_location, validate, exclude_lint, warn_lint, site_name):
267 279
     type=click.File(mode='w'),
268 280
     default=sys.stdout,
269 281
     help='Where to output. Defaults to sys.stdout.')
270
-def list_(*, output_stream):
282
+def list_sites(*, output_stream):
271 283
     engine.repository.process_site_repository(update_config=True)
272 284
     engine.site.list_(output_stream)
273 285
 
@@ -280,9 +292,8 @@ def list_(*, output_stream):
280 292
     type=click.File(mode='w'),
281 293
     default=sys.stdout,
282 294
     help='Where to output. Defaults to sys.stdout.')
283
-@click.argument('site_name')
295
+@SITE_REPOSITORY_ARGUMENT
284 296
 def show(*, output_stream, site_name):
285
-    engine.repository.process_repositories(site_name)
286 297
     engine.site.show(site_name, output_stream)
287 298
 
288 299
 
@@ -294,9 +305,8 @@ def show(*, output_stream, site_name):
294 305
     type=click.File(mode='w'),
295 306
     default=sys.stdout,
296 307
     help='Where to output. Defaults to sys.stdout.')
297
-@click.argument('site_name')
308
+@SITE_REPOSITORY_ARGUMENT
298 309
 def render(*, output_stream, site_name):
299
-    engine.repository.process_repositories(site_name)
300 310
     engine.site.render(site_name, output_stream)
301 311
 
302 312
 
@@ -304,12 +314,11 @@ def render(*, output_stream, site_name):
304 314
 @ALLOW_MISSING_SUBSTITUTIONS_OPTION
305 315
 @EXCLUDE_LINT_OPTION
306 316
 @WARN_LINT_OPTION
307
-@click.argument('site_name')
317
+@SITE_REPOSITORY_ARGUMENT
308 318
 def lint_site(*, fail_on_missing_sub_src, exclude_lint, warn_lint, site_name):
309 319
     """Lint a given site using checks defined in
310 320
     :mod:`pegleg.engine.errorcodes`.
311 321
     """
312
-    engine.repository.process_repositories(site_name)
313 322
     _lint_helper(
314 323
         site_name=site_name,
315 324
         fail_on_missing_sub_src=fail_on_missing_sub_src,

+ 1
- 0
pegleg/engine/util/git.py View File

@@ -169,6 +169,7 @@ def _try_git_clone(repo_url,
169 169
     # and ensure we handle url/foo.git/ cases. prefix is 'tmp' by default.
170 170
     repo_name = repo_url.rstrip('/').split('/')[-1]
171 171
     temp_dir = os.path.join(clone_path, repo_name)
172
+
172 173
     try:
173 174
         os.makedirs(temp_dir)
174 175
     except FileExistsError:

Loading…
Cancel
Save