Skip to content

Commit

Permalink
Do not keep extra docs content.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed May 25, 2021
1 parent 0450088 commit 78fa7be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
12 changes: 6 additions & 6 deletions antsibull/extra_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]


Expand Down Expand Up @@ -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.
Expand All @@ -183,20 +183,20 @@ 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
dummy, errors = lint_required_conditions(content, collection_name)

# 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

Expand Down
24 changes: 15 additions & 9 deletions antsibull/write_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 78fa7be

Please sign in to comment.