Skip to content

Commit aac3331

Browse files
authored
chore(python): fix erroneous versions detected in detect_versions() (#1248)
* chore(python): fix erroneous versions detected in detect_versions() * make detect_versions() more flexible for non-cloud apis
1 parent 949c010 commit aac3331

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

synthtool/gcp/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,13 @@ def detect_versions(
384384
except FileNotFoundError:
385385
pass
386386

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

390395
if sub_dirs:
391396
# if `default_version` is not specified, return the sorted directories.

synthtool/languages/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def owlbot_main() -> None:
184184

185185
templated_files = CommonTemplates().py_library(
186186
microgenerator=True,
187-
versions=detect_versions(path="./google/cloud", default_first=True),
187+
versions=detect_versions(path="./google", default_first=True),
188188
)
189189
s.move(
190190
[templated_files], excludes=[".coveragerc"]

tests/test_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,12 @@ def test_detect_versions_with_default_version_from_metadata():
237237

238238
def test_detect_versions_nested_directory():
239239
temp_dir = Path(tempfile.mkdtemp())
240-
src_dir = temp_dir / "src" / "src2" / "src3"
240+
src_dir = temp_dir / "src" / "src2" / "src3" / "src4"
241241
vs = ("v1", "v2", "v3")
242242
for v in vs:
243243
os.makedirs(src_dir / v)
244+
# this folder should be ignored
245+
os.makedirs(src_dir / v / "some_other_service_v1_beta")
244246

245247
with util.chdir(temp_dir):
246248
versions = detect_versions(default_version="v1")

0 commit comments

Comments
 (0)