-
Notifications
You must be signed in to change notification settings - Fork 1.6k
chore(librarian): create docs/README.rst file during generation #14751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -381,7 +381,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str): | |||||||||||||||||||||||||
| # Safely remove specific files if they exist using pathlib. | ||||||||||||||||||||||||||
| Path(f"{output}/{path_to_library}/CHANGELOG.md").unlink(missing_ok=True) | ||||||||||||||||||||||||||
| Path(f"{output}/{path_to_library}/docs/CHANGELOG.md").unlink(missing_ok=True) | ||||||||||||||||||||||||||
| Path(f"{output}/{path_to_library}/docs/README.rst").unlink(missing_ok=True) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # The glob loops are already safe, as they do nothing if no files match. | ||||||||||||||||||||||||||
| for post_processing_file in glob.glob( | ||||||||||||||||||||||||||
|
|
@@ -501,6 +500,47 @@ def _generate_repo_metadata_file( | |||||||||||||||||||||||||
| _write_json_file(output_repo_metadata, metadata_content) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _copy_readme_to_docs(output: str, library_id: str): | ||||||||||||||||||||||||||
| """Copies the README.rst file for a generated library to docs/README.rst. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| This function is robust against various symlink configurations that could | ||||||||||||||||||||||||||
| cause `shutil.copy` to fail with a `SameFileError`. It reads the content | ||||||||||||||||||||||||||
| from the source and writes it to the destination, ensuring the final | ||||||||||||||||||||||||||
| destination is a real file. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| output(str): Path to the directory in the container where code | ||||||||||||||||||||||||||
| should be generated. | ||||||||||||||||||||||||||
| library_id(str): The library id. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| path_to_library = f"packages/{library_id}" | ||||||||||||||||||||||||||
| source_path = f"{output}/{path_to_library}/README.rst" | ||||||||||||||||||||||||||
| docs_path = f"{output}/{path_to_library}/docs" | ||||||||||||||||||||||||||
| destination_path = f"{docs_path}/README.rst" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # If the source file doesn't exist (not even as a broken symlink), | ||||||||||||||||||||||||||
| # there's nothing to copy. | ||||||||||||||||||||||||||
| if not os.path.lexists(source_path): | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Read the content from the source, which will resolve any symlinks. | ||||||||||||||||||||||||||
| with open(source_path, "r") as f: | ||||||||||||||||||||||||||
| content = f.read() | ||||||||||||||||||||||||||
|
Comment on lines
+526
to
+528
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: use the google-cloud-python/.generator/cli.py Lines 61 to 72 in 79a5261
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Remove any symlinks at the destination to prevent errors. | ||||||||||||||||||||||||||
| if os.path.islink(destination_path): | ||||||||||||||||||||||||||
| os.remove(destination_path) | ||||||||||||||||||||||||||
| elif os.path.islink(docs_path): | ||||||||||||||||||||||||||
| os.remove(docs_path) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Ensure the destination directory exists as a real directory. | ||||||||||||||||||||||||||
| os.makedirs(docs_path, exist_ok=True) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Write the content to the destination, creating a new physical file. | ||||||||||||||||||||||||||
| with open(destination_path, "w") as f: | ||||||||||||||||||||||||||
| f.write(content) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def handle_generate( | ||||||||||||||||||||||||||
| librarian: str = LIBRARIAN_DIR, | ||||||||||||||||||||||||||
| source: str = SOURCE_DIR, | ||||||||||||||||||||||||||
|
|
@@ -542,6 +582,7 @@ def handle_generate( | |||||||||||||||||||||||||
| _copy_files_needed_for_post_processing(output, input, library_id) | ||||||||||||||||||||||||||
| _generate_repo_metadata_file(output, library_id, source, apis_to_generate) | ||||||||||||||||||||||||||
| _run_post_processor(output, library_id) | ||||||||||||||||||||||||||
| _copy_readme_to_docs(output, library_id) | ||||||||||||||||||||||||||
| _clean_up_files_after_post_processing(output, library_id) | ||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||
| raise ValueError("Generation failed.") from e | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we create a follow up issue to track adding docs/README.rst to the output of
gapic-generator?