From 93a70739c465b1878798f679d5e50fe09d8f7791 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 24 May 2021 21:12:39 +0200 Subject: [PATCH] Do not keep extra docs content. --- antsibull/extra_docs.py | 12 ++++++------ antsibull/write_docs.py | 24 +++++++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/antsibull/extra_docs.py b/antsibull/extra_docs.py index 5ddb8e5d..e0153bd4 100644 --- a/antsibull/extra_docs.py +++ b/antsibull/extra_docs.py @@ -36,7 +36,7 @@ def __init__(self, title: str, toctree: t.List[str]): #: A tuple consisting of a list of sections and a list of RST documents as tuples -#: (relative path in docs/docsite/rst, content). +#: (absolute path to source file, relative path in collection's docs directory). CollectionExtraDocsInfoT = t.Tuple[t.List[Section], t.List[t.Tuple[str, str]]] @@ -162,7 +162,7 @@ def load_extra_docs_index(index_path: str) -> t.Tuple[t.List[Section], t.List[st async def load_collection_extra_docs(collection_name: str, collection_path: str, - path_prefix: str = 'docsite/' + path_prefix: str = 'docsite' ) -> CollectionExtraDocsInfoT: '''Given a collection name and collection metadata, load extra docs data. @@ -183,12 +183,12 @@ async def load_collection_extra_docs(collection_name: str, for section in sections: for i, toctree in enumerate(section.toctree): - section.toctree[i] = path_prefix + toctree + section.toctree[i] = f"{path_prefix}/{toctree}" documents = [] - for doc in find_extra_docs(collection_path): + for abs_path, rel_path in find_extra_docs(collection_path): try: # Load content - async with aiofiles.open(doc[0], 'r', encoding='utf-8') as f: + async with aiofiles.open(abs_path, 'r', encoding='utf-8') as f: content = await f.read() # Lint content @@ -196,7 +196,7 @@ async def load_collection_extra_docs(collection_name: str, # When no errors were found, add to output if not errors: - documents.append((path_prefix + doc[1], content)) + documents.append((abs_path, os.path.join(path_prefix, rel_path))) except Exception: pass diff --git a/antsibull/write_docs.py b/antsibull/write_docs.py index d5fdd24c..d836f71e 100644 --- a/antsibull/write_docs.py +++ b/antsibull/write_docs.py @@ -38,17 +38,23 @@ ADD_TOCTREES = True -async def write_file(path: str, content: str) -> None: +async def copy_file(source_path: str, dest_path: str) -> None: """ - Write content to a given path. + Copy content from one file to another. - :arg path: Path of the file to write. - :arg content: Content to write into the file. + Note that this implementation is somewhat naive: it reads the whole content of the source file + and then proceeds to write it to the destination file. + + :arg source_path: Source path. Must be a file. + :arg dest_path: Destination path. """ - flog = mlog.fields(func='write_file') + flog = mlog.fields(func='copy_file') flog.debug('Enter') - async with aiofiles.open(path, 'w') as f: + async with aiofiles.open(source_path, 'rb') as f: + content = await f.read() + + async with aiofiles.open(dest_path, 'wb') as f: await f.write(content) flog.debug('Leave') @@ -567,10 +573,10 @@ async def output_extra_docs(dest_dir: str, collection_dir = os.path.join(collection_toplevel, *(collection_name.split('.'))) else: collection_dir = collection_toplevel - for path, content in documents: - full_path = os.path.join(collection_dir, path) + for source_path, rel_path in documents: + full_path = os.path.join(collection_dir, rel_path) os.makedirs(os.path.dirname(full_path), mode=0o755, exist_ok=True) - writers.append(await pool.spawn(write_file(full_path, content))) + writers.append(await pool.spawn(copy_file(source_path, full_path))) await asyncio.gather(*writers)