Skip to content

Commit 70020e2

Browse files
committed
chore(python): autogenerate docs/index.rst
1 parent 41ccd8c commit 70020e2

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.. include:: README.rst
2+
3+
.. include:: multiprocessing.rst
4+
{% if default_version %}
5+
{% if versions|length > 1 %}
6+
This package includes clients for multiple versions of {{ metadata['repo']['name_pretty'] }}.
7+
By default, you will get version ``{{ default_version }}``.
8+
{% endif %}
9+
{% endif %}
10+
{% for version in versions %}
11+
API Reference
12+
-------------
13+
.. toctree::
14+
:maxdepth: 2
15+
16+
{{ version }}/services
17+
{{ version }}/types
18+
{% endfor %}
19+
{%- if migration_guide_version %}
20+
Migration Guide
21+
---------------
22+
23+
See the guide below for instructions on migrating to the {{ migration_guide_version }} release of this library.
24+
25+
.. toctree::
26+
:maxdepth: 2
27+
28+
UPGRADING
29+
{% endif %}
30+
Changelog
31+
---------
32+
33+
For a list of all ``{{ metadata['repo']['distribution_name'] }}`` releases:
34+
35+
.. toctree::
36+
:maxdepth: 2
37+
38+
changelog

synthtool/languages/python.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
import re
1617
import sys
1718
from pathlib import Path
18-
from typing import Any, Dict
19+
from typing import Any, Dict, List, Optional
1920

2021
import yaml
2122

@@ -93,6 +94,36 @@ def _get_sample_readme_metadata(sample_dir: Path) -> dict:
9394
return sample_metadata
9495

9596

97+
def detect_versions(
98+
path: str = "./src", default_version: Optional[str] = None
99+
) -> List[str]:
100+
"""
101+
Detects the versions a library has, based on distinct folders
102+
within path. This is based on the fact that our GAPIC libraries are
103+
structured as follows:
104+
105+
src/v1
106+
src/v1beta
107+
src/v1alpha
108+
109+
With folder names mapping directly to versions.
110+
111+
Returns: a list of the subdirectories; for the example above:
112+
['v1', 'v1alpha', 'v1beta']
113+
If specified, the default_version is guaranteed to be listed last.
114+
Otherwise, the list is sorted alphabetically.
115+
"""
116+
versions = []
117+
if os.path.isdir(path):
118+
for directory in os.listdir(path):
119+
if os.path.isdir(os.path.join(path, directory)):
120+
versions.append(directory)
121+
versions.sort()
122+
if default_version is not None:
123+
versions = [v for v in versions if v != default_version] + [default_version]
124+
return versions
125+
126+
96127
def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:
97128
"""
98129
Find all samples projects and render templates.

tests/test_python_library.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
from pathlib import Path
1717

1818
import pytest
19+
import tempfile
1920

2021
from synthtool import gcp
2122
from synthtool.sources import templates
23+
from synthtool.languages import python
2224
from . import util
2325

2426

@@ -126,3 +128,46 @@ def test_split_system_tests():
126128
with open(templated_files / ".kokoro/presubmit/system-3.8.cfg", "r") as f:
127129
contents = f.read()
128130
assert "system-3.8" in contents
131+
132+
def test_detect_versions_src():
133+
temp_dir = Path(tempfile.mkdtemp())
134+
src_dir = temp_dir / "src"
135+
for v in ("v1", "v2", "v3"):
136+
os.makedirs(src_dir / v)
137+
138+
with util.chdir(temp_dir):
139+
versions = python.detect_versions()
140+
assert ["v1", "v2", "v3"] == versions
141+
142+
143+
def test_detect_versions_staging():
144+
temp_dir = Path(tempfile.mkdtemp())
145+
staging_dir = temp_dir / "owl-bot-staging"
146+
for v in ("v1", "v2", "v3"):
147+
os.makedirs(staging_dir / v)
148+
149+
versions = python.detect_versions(staging_dir)
150+
assert ["v1", "v2", "v3"] == versions
151+
152+
153+
def test_detect_versions_dir_not_found():
154+
temp_dir = Path(tempfile.mkdtemp())
155+
156+
versions = python.detect_versions(temp_dir / "does-not-exist")
157+
assert [] == versions
158+
159+
160+
def test_detect_versions_with_default():
161+
temp_dir = Path(tempfile.mkdtemp())
162+
src_dir = temp_dir / "src"
163+
vs = ("v1", "v2", "v3")
164+
for v in vs:
165+
os.makedirs(src_dir / v)
166+
167+
with util.chdir(temp_dir):
168+
versions = python.detect_versions(default_version="v1")
169+
assert ["v2", "v3", "v1"] == versions
170+
versions = python.detect_versions(default_version="v2")
171+
assert ["v1", "v3", "v2"] == versions
172+
versions = python.detect_versions(default_version="v3")
173+
assert ["v1", "v2", "v3"] == versions

0 commit comments

Comments
 (0)