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): Run owlbot_main() when owlbot.py doesn't exist #1244

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Changes from 2 commits
Commits
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
53 changes: 51 additions & 2 deletions synthtool/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import re
import sys

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

import yaml

import synthtool as s
Expand Down Expand Up @@ -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,52 @@ 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)
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()