Skip to content

Commit

Permalink
chore(python): fix erroneous versions detected in detect_versions() (#…
Browse files Browse the repository at this point in the history
…1248)

* chore(python): fix erroneous versions detected in detect_versions()

* make detect_versions() more flexible for non-cloud apis
  • Loading branch information
parthea committed Oct 20, 2021
1 parent 949c010 commit aac3331
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,13 @@ def detect_versions(
except FileNotFoundError:
pass

# Sort the sub directories alphabetically.
sub_dirs = sorted([p.name for p in Path(path).rglob("*v[1-9]*") if p.is_dir()])
# Detect versions up to a depth of 4 in directory hierarchy
for level in ("*v[1-9]*", "*/*v[1-9]*", "*/*/*v[1-9]*", "*/*/*/*v[1-9]*"):
# Sort the sub directories alphabetically.
sub_dirs = sorted([p.name for p in Path(path).glob(level) if p.is_dir()])
# Don't proceed to the next level if we've detected versions in this depth level
if sub_dirs:
break

if sub_dirs:
# if `default_version` is not specified, return the sorted directories.
Expand Down
2 changes: 1 addition & 1 deletion synthtool/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def owlbot_main() -> None:

templated_files = CommonTemplates().py_library(
microgenerator=True,
versions=detect_versions(path="./google/cloud", default_first=True),
versions=detect_versions(path="./google", default_first=True),
)
s.move(
[templated_files], excludes=[".coveragerc"]
Expand Down
4 changes: 3 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,12 @@ def test_detect_versions_with_default_version_from_metadata():

def test_detect_versions_nested_directory():
temp_dir = Path(tempfile.mkdtemp())
src_dir = temp_dir / "src" / "src2" / "src3"
src_dir = temp_dir / "src" / "src2" / "src3" / "src4"
vs = ("v1", "v2", "v3")
for v in vs:
os.makedirs(src_dir / v)
# this folder should be ignored
os.makedirs(src_dir / v / "some_other_service_v1_beta")

with util.chdir(temp_dir):
versions = detect_versions(default_version="v1")
Expand Down

0 comments on commit aac3331

Please sign in to comment.