Skip to content
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

chore(python): autogenerate docs/index.rst #1114

Merged
merged 25 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c0af3ab
chore(python): autogenerate docs/index.rst
parthea Jun 14, 2021
bdee7a8
set default path to owl-bot-staging
parthea Sep 29, 2021
4bc1061
Merge branch 'master' into add-docs-index-rst-to-template
parthea Sep 29, 2021
fb23307
optimize code using glob
parthea Sep 29, 2021
89e2bc4
move the default_version to the front of the list
parthea Sep 29, 2021
9a6f82f
Drop redundant default_version argument
parthea Sep 29, 2021
52541cc
remove unused imports
parthea Sep 29, 2021
3fe0d5b
read default_version from .repo-metadata.json
parthea Oct 5, 2021
83bd511
exclude docs/index.rst if default_version is not specified
parthea Oct 5, 2021
da5be6c
check for versions kwarg
parthea Oct 5, 2021
cf8a2bc
only generate docs/index.rst if default_version is specified
parthea Oct 6, 2021
55e74f8
lint
parthea Oct 6, 2021
77d8cf0
Merge branch 'master' into add-docs-index-rst-to-template
parthea Oct 6, 2021
483c6d0
fix build
parthea Oct 6, 2021
52de68a
fix build
parthea Oct 6, 2021
ecf2d07
Merge branch 'master' into add-docs-index-rst-to-template
parthea Oct 7, 2021
5910c24
Merge branch 'master' into add-docs-index-rst-to-template
parthea Oct 8, 2021
d068abb
Merge branch 'master' into add-docs-index-rst-to-template
parthea Oct 12, 2021
8af8e95
consolidate the duplicated detect_versions function
parthea Oct 13, 2021
d66ea79
remove unused import
parthea Oct 14, 2021
b8c20e0
remove unused imports
parthea Oct 14, 2021
4a9688f
Merge branch 'master' into add-docs-index-rst-to-template
parthea Oct 14, 2021
fc607c0
update comments
parthea Oct 14, 2021
b992238
remove unused import
parthea Oct 14, 2021
c79fc51
run black
parthea Oct 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions synthtool/gcp/templates/python_library/docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.. include:: README.rst

.. include:: multiprocessing.rst
{% if default_version %}
{% if versions|length > 1 %}
This package includes clients for multiple versions of {{ metadata['repo']['name_pretty'] }}.
By default, you will get version ``{{ default_version }}``.
{% endif %}
{% endif %}
{% for version in versions %}
API Reference
-------------
.. toctree::
:maxdepth: 2

{{ version }}/services
{{ version }}/types
{% endfor %}
{%- if migration_guide_version %}
Migration Guide
---------------

See the guide below for instructions on migrating to the {{ migration_guide_version }} release of this library.

.. toctree::
:maxdepth: 2

UPGRADING
{% endif %}
Changelog
---------

For a list of all ``{{ metadata['repo']['distribution_name'] }}`` releases:

.. toctree::
:maxdepth: 2

changelog
33 changes: 32 additions & 1 deletion synthtool/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import sys
from pathlib import Path
from typing import Any, Dict
from typing import Any, Dict, List, Optional

import yaml

Expand Down Expand Up @@ -93,6 +94,36 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict:
return sample_metadata


def detect_versions(
path: str = "./src", default_version: Optional[str] = None
) -> List[str]:
"""
Detects the versions a library has, based on distinct folders
within path. This is based on the fact that our GAPIC libraries are
structured as follows:

src/v1
src/v1beta
src/v1alpha

With folder names mapping directly to versions.

Returns: a list of the subdirectories; for the example above:
['v1', 'v1alpha', 'v1beta']
If specified, the default_version is guaranteed to be listed last.
Otherwise, the list is sorted alphabetically.
"""
versions = []
if os.path.isdir(path):
for directory in os.listdir(path):
if os.path.isdir(os.path.join(path, directory)):
versions.append(directory)
versions.sort()
if default_version is not None:
versions = [v for v in versions if v != default_version] + [default_version]
return versions


def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:
"""
Find all samples projects and render templates.
Expand Down
46 changes: 46 additions & 0 deletions tests/test_python_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from pathlib import Path

import pytest
import tempfile

from synthtool import gcp
from synthtool.sources import templates
from synthtool.languages import python
from . import util


Expand Down Expand Up @@ -126,3 +128,47 @@ def test_split_system_tests():
with open(templated_files / ".kokoro/presubmit/system-3.8.cfg", "r") as f:
contents = f.read()
assert "system-3.8" in contents


def test_detect_versions_src():
temp_dir = Path(tempfile.mkdtemp())
src_dir = temp_dir / "src"
for v in ("v1", "v2", "v3"):
os.makedirs(src_dir / v)

with util.chdir(temp_dir):
versions = python.detect_versions()
assert ["v1", "v2", "v3"] == versions


def test_detect_versions_staging():
temp_dir = Path(tempfile.mkdtemp())
staging_dir = temp_dir / "owl-bot-staging"
for v in ("v1", "v2", "v3"):
os.makedirs(staging_dir / v)

versions = python.detect_versions(staging_dir)
assert ["v1", "v2", "v3"] == versions


def test_detect_versions_dir_not_found():
temp_dir = Path(tempfile.mkdtemp())

versions = python.detect_versions(temp_dir / "does-not-exist")
assert [] == versions


def test_detect_versions_with_default():
temp_dir = Path(tempfile.mkdtemp())
src_dir = temp_dir / "src"
vs = ("v1", "v2", "v3")
for v in vs:
os.makedirs(src_dir / v)

with util.chdir(temp_dir):
versions = python.detect_versions(default_version="v1")
assert ["v2", "v3", "v1"] == versions
versions = python.detect_versions(default_version="v2")
assert ["v1", "v3", "v2"] == versions
versions = python.detect_versions(default_version="v3")
assert ["v1", "v2", "v3"] == versions