Skip to content

Commit

Permalink
chore(python): Run owlbot_main() when owlbot.py doesn't exist (#1244)
Browse files Browse the repository at this point in the history
* chore(python): Run owlbot_main() when owlbot.py doesn't exist

* lint

* add versions
  • Loading branch information
parthea authored Oct 14, 2021
1 parent c1e5e1c commit d4ff3cd
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions synthtool/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

import re
import sys

import json
from pathlib import Path
from typing import Any, Dict, List

import yaml

import synthtool as s
from synthtool import _tracked_paths, log, shell
from synthtool.gcp.common import CommonTemplates
from synthtool.gcp.common import CommonTemplates, detect_versions
from synthtool.sources import templates

PathOrStr = templates.PathOrStr
Expand All @@ -45,7 +46,6 @@
# See the License for the specific language governing permissions and
# limitations under the License."""

SAMPLES_VERSIONS = ["3.6", "3.7", "3.8"]
IGNORED_VERSIONS: List[str] = []

SAMPLES_TEMPLATE_PATH = Path(CommonTemplates()._template_root) / "python_samples"
Expand Down Expand Up @@ -145,3 +145,55 @@ def py_samples(*, root: PathOrStr = None, skip_readmes: bool = False) -> None:
result = t.render(subdir=sample_project_dir, **sample_readme_metadata)
_tracked_paths.add(result)
s.copy([result], excludes=excludes)


def owlbot_main() -> None:
"""Copies files from staging and template directories into current working dir.
When there is no owlbot.py file, run this function instead.
Depends on owl-bot copying into a staging directory, so your .Owlbot.yaml should
look a lot like this:
docker:
image: docker pull gcr.io/cloud-devrel-public-resources/owlbot-python:latest
deep-remove-regex:
- /owl-bot-staging
deep-copy-regex:
- source: /google/cloud/video/transcoder/(.*)/.*-nodejs/(.*)
dest: /owl-bot-staging/$1/$2
Also, this function requires a default_version in your .repo-metadata.json. Ex:
"default_version": "v1",
"""

try:
# Load the default version defined in .repo-metadata.json.
default_version = json.load(open(".repo-metadata.json", "rt")).get(
"default_version"
)
except FileNotFoundError:
default_version = None

if default_version:
for library in s.get_staging_dirs(default_version):
s.move([library], excludes=["setup.py", "README.rst", "docs/index.rst"])
s.remove_staging_dirs()

templated_files = CommonTemplates().py_library(
microgenerator=True,
versions=detect_versions(path="./google/cloud", default_first=True),
)
s.move(
[templated_files], excludes=[".coveragerc"]
) # the microgenerator has a good coveragerc file

py_samples(skip_readmes=True)

s.shell.run(["nox", "-s", "blacken"], hide_output=False)


if __name__ == "__main__":
owlbot_main()

0 comments on commit d4ff3cd

Please sign in to comment.