|
16 | 16 | from functools import lru_cache
|
17 | 17 | from http.server import BaseHTTPRequestHandler, HTTPServer
|
18 | 18 | from pathlib import Path
|
19 |
| -from typing import Dict, List, Sequence |
| 19 | +from typing import Dict, List, Optional, Sequence |
20 | 20 | from warnings import warn
|
21 | 21 |
|
22 | 22 | import pdoc
|
@@ -419,22 +419,24 @@ def to_url_id(module):
|
419 | 419 |
|
420 | 420 | json_values = [dict(obj, url=urls[obj['url']]) for obj in index]
|
421 | 421 | cmd = ['node', str(Path(__file__).with_name('build-index.js'))]
|
422 |
| - proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
423 |
| - main_path = args.output_dir |
424 |
| - if proc.poll() is None: |
| 422 | + proc = None |
| 423 | + try: |
| 424 | + proc = subprocess.Popen(cmd, text=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
425 | 425 | stdout, stderr = proc.communicate(json.dumps(json_values))
|
426 | 426 | assert proc.poll() == 0, proc.poll()
|
427 |
| - if proc.returncode == 0: |
428 |
| - stdout = 'INDEX=' + stdout |
429 |
| - else: |
430 |
| - warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed: ' |
431 |
| - f'{proc.stderr and proc.stderr.read() or ""!r}. ' |
432 |
| - f'The search feature will still work, ' |
| 427 | + except Exception as ex: |
| 428 | + stderr = proc and proc.stderr and proc.stderr.read() # type: ignore |
| 429 | + warn(f'Prebuilding Lunr index with command `{" ".join(cmd)}` failed with ' |
| 430 | + f'"{ex.__class__.__name__}: {ex}". Stderr: {stderr or ""!r}. ' |
| 431 | + f'Note, the search feature will still work, ' |
433 | 432 | f'but may be slower (with the index rebuilt just before use). '
|
434 | 433 | f'To prebuild an index in advance, ensure `node` is executable in the '
|
435 | 434 | f'pdoc environment.', category=RuntimeWarning)
|
436 | 435 | stdout = ('URLS=' + json.dumps(urls, indent=0, separators=(',', ':')) +
|
437 | 436 | ';\nINDEX=' + json.dumps(index, indent=0, separators=(',', ':')))
|
| 437 | + else: |
| 438 | + stdout = 'INDEX=' + stdout |
| 439 | + main_path = args.output_dir |
438 | 440 | index_path = Path(main_path).joinpath('index.js')
|
439 | 441 | index_path.write_text(stdout)
|
440 | 442 | print(str(index_path))
|
|
0 commit comments