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

feat: move synthtool templates to library_generation/owlbot #2443

Merged
merged 33 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ba38e9c
transfer templates from synthtool to owlbot
diegomarquezp Feb 2, 2024
37af910
use locally hosted templates
diegomarquezp Feb 2, 2024
9e9e743
fix regex parsing
diegomarquezp Feb 2, 2024
d2a8ca4
update protobuf and generator versions in IT
diegomarquezp Feb 2, 2024
2cb0a31
minor fixes
diegomarquezp Feb 6, 2024
e1c262d
install synthtool for unit tests
diegomarquezp Feb 6, 2024
79b41bc
use repo level googleapis commitish for kms
diegomarquezp Feb 6, 2024
7d6dabc
comment fixes
diegomarquezp Feb 6, 2024
7c503c9
Update library_generation/owlbot/bin/entrypoint.sh
diegomarquezp Feb 6, 2024
5587d0f
use config yaml to compute exclusions
diegomarquezp Feb 7, 2024
65d8ef9
remove unused __init__.py files
diegomarquezp Feb 7, 2024
a0b0cfd
Merge remote-tracking branch 'origin/move-synthtool-templates' into m…
diegomarquezp Feb 7, 2024
b980b88
remove owlbot.py check in template rendering postprocessing step
diegomarquezp Feb 7, 2024
f2cd5bf
add comment for setup.py
diegomarquezp Feb 7, 2024
f416b4c
remove unit test
diegomarquezp Feb 7, 2024
8d348a4
simplify module processing script
diegomarquezp Feb 7, 2024
9c778f6
update googleapis commitish
diegomarquezp Feb 7, 2024
dbecc0e
update googleapis commitish ii
diegomarquezp Feb 7, 2024
a17d36a
Merge remote-tracking branch 'origin/main' into move-synthtool-templates
diegomarquezp Feb 8, 2024
07bc35f
Merge remote-tracking branch 'origin/main' into move-synthtool-templates
diegomarquezp Feb 8, 2024
2539e1e
post-merge fixes
diegomarquezp Feb 8, 2024
560644c
fix unit tests
diegomarquezp Feb 8, 2024
e0aade6
delete generated goldens
diegomarquezp Feb 8, 2024
1663a0f
restore necessary golden files
diegomarquezp Feb 8, 2024
0fb4b7e
snippet bot to ignore templates
diegomarquezp Feb 8, 2024
ccfee71
fix snippet bot ii
diegomarquezp Feb 8, 2024
07ea2a6
fix snippet bot iii
diegomarquezp Feb 8, 2024
b38f755
use full path to conflicting pomxml
diegomarquezp Feb 8, 2024
0e8b19e
use full path to actual conflicting pomxml
diegomarquezp Feb 8, 2024
f4e7867
lint
diegomarquezp Feb 8, 2024
f86635d
install lirbary_generation python package via setup.py
diegomarquezp Feb 8, 2024
02bac22
include owlbot python sources in python lint check
diegomarquezp Feb 8, 2024
e540631
lint owlbot
diegomarquezp Feb 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/snippet-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ ignoreFiles:
- src/test/**
- test/**
- showcase/**
- library_generation/owlbot/templates/java_library/samples/install-without-bom/pom.xml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2443 (comment) has the context. Basically the region tag check thinks that template has a region tag (wrong). I'm yet to get a response from the automation team

16 changes: 16 additions & 0 deletions .github/workflows/verify_library_generation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
set -ex
pushd library_generation
pip install -r requirements.in
pip install .
popd
- name: Run integration tests
shell: bash
Expand Down Expand Up @@ -74,6 +75,21 @@ jobs:
pushd library_generation
pip install -r requirements.in
popd
- name: install synthtool
shell: bash
run: |
set -ex
mkdir -p /tmp/synthtool
pushd /tmp/synthtool
if [ ! -d "synthtool" ]; then
git clone https://github.com/googleapis/synthtool.git
fi
pushd "synthtool"

git reset --hard origin/no-java-templates

python3 -m pip install -e .
python3 -m pip install -r requirements.in
- name: Run shell unit tests
run: |
set -x
Expand Down
1 change: 1 addition & 0 deletions library_generation/generate_composed_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def generate_composed_library(
config.owlbot_cli_image,
config.synthtool_commitish,
str(is_monorepo).lower(),
config.path_to_yaml,
],
"Library postprocessing",
)
Expand Down
6 changes: 6 additions & 0 deletions library_generation/model/generation_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(
googleapis_commitish: str,
owlbot_cli_image: str,
synthtool_commitish: str,
template_excludes: str,
path_to_yaml: str,
libraries: List[LibraryConfig],
grpc_version: Optional[str] = None,
protobuf_version: Optional[str] = None,
Expand All @@ -39,6 +41,8 @@ def __init__(
self.googleapis_commitish = googleapis_commitish
self.owlbot_cli_image = owlbot_cli_image
self.synthtool_commitish = synthtool_commitish
self.template_excludes = template_excludes
self.path_to_yaml = path_to_yaml
self.libraries = libraries
self.grpc_version = grpc_version
self.protobuf_version = protobuf_version
Expand Down Expand Up @@ -94,6 +98,8 @@ def from_yaml(path_to_yaml: str):
googleapis_commitish=__required(config, "googleapis_commitish"),
owlbot_cli_image=__required(config, "owlbot_cli_image"),
synthtool_commitish=__required(config, "synthtool_commitish"),
template_excludes=__required(config, "template_excludes"),
path_to_yaml=path_to_yaml,
libraries=parsed_libraries,
)

Expand Down
86 changes: 47 additions & 39 deletions library_generation/owlbot/bin/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,14 @@
set -ex
scripts_root=$1
versions_file=$2

# Runs template and etc in current working directory
function processModule() {
# templates as well as retrieving files from owl-bot-staging
echo "Generating templates and retrieving files from owl-bot-staging directory..."
if [ -f "owlbot.py" ]
then
# defaults to run owlbot.py
python3 owlbot.py
fi
echo "...done"

# write or restore pom.xml files
echo "Generating missing pom.xml..."
python3 "${scripts_root}/owlbot/src/fix-poms.py" "${versions_file}" "true"
echo "...done"

# write or restore clirr-ignored-differences.xml
echo "Generating clirr-ignored-differences.xml..."
${scripts_root}/owlbot/bin/write_clirr_ignore.sh "${scripts_root}"
echo "...done"

# fix license headers
echo "Fixing missing license headers..."
python3 "${scripts_root}/owlbot/src/fix-license-headers.py"
echo "...done"

# TODO: re-enable this once we resolve thrashing
# restore license headers years
# echo "Restoring copyright years..."
# /owlbot/bin/restore_license_headers.sh
# echo "...done"

# ensure formatting on all .java files in the repository
echo "Reformatting source..."
mvn fmt:format -q
echo "...done"
}
configuration_yaml=$3

# This script can be used to process HW libraries and monorepo
# (google-cloud-java) libraries, which require a slightly different treatment
# monorepo folders have an .OwlBot.yaml file in the module folder (e.g.
# java-asset/.OwlBot.yaml), whereas HW libraries have the yaml in
# `.github/.OwlBot.yaml`
monorepo="false"
if [[ -f "$(pwd)/.OwlBot.yaml" ]]; then
monorepo="true"
fi
Expand All @@ -80,4 +44,48 @@ if [[ "${monorepo}" == "true" ]]; then
mv temp owl-bot-staging
fi

processModule

# Runs template and etc in current working directory
monorepo=$1

# apply repo templates
echo "Rendering templates"
python3 "${scripts_root}/owlbot/src/apply_repo_templates.py" "${configuration_yaml}" "${monorepo}"

# templates as well as retrieving files from owl-bot-staging
echo "Retrieving files from owl-bot-staging directory..."
if [ -f "owlbot.py" ]
then
# defaults to run owlbot.py
python3 owlbot.py
fi
echo "...done"

# write or restore pom.xml files
echo "Generating missing pom.xml..."
python3 "${scripts_root}/owlbot/src/fix-poms.py" "${versions_file}" "true"
echo "...done"

# write or restore clirr-ignored-differences.xml
echo "Generating clirr-ignored-differences.xml..."
${scripts_root}/owlbot/bin/write_clirr_ignore.sh "${scripts_root}"
echo "...done"

# fix license headers
echo "Fixing missing license headers..."
python3 "${scripts_root}/owlbot/src/fix-license-headers.py"
echo "...done"

# TODO: re-enable this once we resolve thrashing
# restore license headers years
# echo "Restoring copyright years..."
# /owlbot/bin/restore_license_headers.sh
# echo "...done"

# ensure formatting on all .java files in the repository
echo "Reformatting source..."
mvn fmt:format
echo "...done"



42 changes: 42 additions & 0 deletions library_generation/owlbot/src/apply_repo_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This script parses an owlbot.py file, specifically the call to `java.common_templates` in
order to extract the excluded files so it can be called with a custom template path
pointing to the templates hosted in `sdk-platform-java/library_generation/owlbot/templates`.
Briefly, this wraps the call to synthtool's common templates using a custom template folder.
"""

import os
import sys
from collections.abc import Sequence
from synthtool.languages.java import common_templates
from pathlib import Path
from library_generation.model.generation_config import from_yaml

script_dir = os.path.dirname(os.path.realpath(__file__))
repo_templates_path = os.path.join(script_dir, "..", "templates", "java_library")


def apply_repo_templates(configuration_yaml_path: str, monorepo: bool) -> None:
config = from_yaml(configuration_yaml_path)
print(f"repo_templates_path: {repo_templates_path}")
print(f"excludes: {config.template_excludes}")
common_templates(
excludes=config.template_excludes,
template_path=Path(repo_templates_path),
monorepo=monorepo,
)


def main(argv: Sequence[str]) -> None:
if len(argv) != 3:
raise ValueError(
"Usage: python apply-repo-templates.py configuration_yaml_path monorepo"
)

configuration_yaml_path = argv[1]
monorepo = argv[2]
apply_repo_templates(configuration_yaml_path, monorepo.lower() == "true")


if __name__ == "__main__":
main(sys.argv)
72 changes: 44 additions & 28 deletions library_generation/owlbot/src/fix-poms.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,11 @@ def update_bom_pom(filename: str, modules: List[module.Module]):
# https://github.com/googleapis/google-cloud-java/issues/9304
def __proto_group_id(main_artifact_group_id: str) -> str:
prefix = "com.google"
list_of_group_id = ["com.google.cloud",
"com.google.area120",
"com.google.analytics"]
list_of_group_id = [
"com.google.cloud",
"com.google.area120",
"com.google.analytics",
]
if main_artifact_group_id not in list_of_group_id:
prefix = main_artifact_group_id
return f"{prefix}.api.grpc"
Expand Down Expand Up @@ -343,11 +345,17 @@ def main(versions_file, monorepo):
main_module = existing_modules[artifact_id]

# Artifact ID is part of distribution name field in .repo-metadata.json
if artifact_id in ["grafeas", "google-cloud-dns",
"google-cloud-notification", "google-iam-policy"]:
if artifact_id in [
"grafeas",
"google-cloud-dns",
"google-cloud-notification",
"google-iam-policy",
]:
# There are special libraries that are not automatically generated
print(f"Skipping a special case library {artifact_id} that do not have "
" the standard module structure.")
print(
f"Skipping a special case library {artifact_id} that do not have "
" the standard module structure."
)
return

parent_artifact_id = f"{artifact_id}-parent"
Expand Down Expand Up @@ -383,8 +391,10 @@ def main(versions_file, monorepo):
version=main_module.version,
release_version=main_module.release_version,
)
if path not in excluded_dependencies_list \
and path not in main_module.artifact_id:
if (
path not in excluded_dependencies_list
and path not in main_module.artifact_id
):
required_dependencies[path] = module.Module(
group_id=__proto_group_id(group_id),
artifact_id=path,
Expand All @@ -401,8 +411,10 @@ def main(versions_file, monorepo):
main_module=main_module,
monorepo=monorepo,
)
if path not in excluded_dependencies_list \
and path not in main_module.artifact_id:
if (
path not in excluded_dependencies_list
and path not in main_module.artifact_id
):
required_dependencies[path] = module.Module(
group_id=__proto_group_id(group_id),
artifact_id=path,
Expand All @@ -418,8 +430,10 @@ def main(versions_file, monorepo):
version=main_module.version,
release_version=main_module.release_version,
)
if path not in excluded_dependencies_list \
and path not in main_module.artifact_id:
if (
path not in excluded_dependencies_list
and path not in main_module.artifact_id
):
required_dependencies[path] = module.Module(
group_id=__proto_group_id(group_id),
artifact_id=path,
Expand All @@ -439,8 +453,10 @@ def main(versions_file, monorepo):
proto_module=existing_modules[proto_artifact_id],
monorepo=monorepo,
)
if path not in excluded_dependencies_list \
and path not in main_module.artifact_id:
if (
path not in excluded_dependencies_list
and path not in main_module.artifact_id
):
required_dependencies[path] = module.Module(
group_id=__proto_group_id(group_id),
artifact_id=path,
Expand All @@ -451,13 +467,13 @@ def main(versions_file, monorepo):
module
for module in required_dependencies.values()
if module.artifact_id.startswith("proto-")
and module.artifact_id not in parent_artifact_id
and module.artifact_id not in parent_artifact_id
]
grpc_modules = [
module
for module in required_dependencies.values()
if module.artifact_id.startswith("grpc-") \
and module.artifact_id not in parent_artifact_id
if module.artifact_id.startswith("grpc-")
and module.artifact_id not in parent_artifact_id
]
if main_module in grpc_modules or main_module in proto_modules:
modules = grpc_modules + proto_modules
Expand Down Expand Up @@ -489,12 +505,11 @@ def main(versions_file, monorepo):

if os.path.isfile(f"{artifact_id}-bom/pom.xml"):
print("updating modules in bom pom.xml")
if artifact_id+"-bom" not in excluded_poms_list:
if artifact_id + "-bom" not in excluded_poms_list:
update_bom_pom(f"{artifact_id}-bom/pom.xml", modules)
elif artifact_id+"-bom" not in excluded_poms_list:
elif artifact_id + "-bom" not in excluded_poms_list:
print("creating missing bom pom.xml")
monorepo_version = __get_monorepo_version(versions_file) \
if monorepo else ""
monorepo_version = __get_monorepo_version(versions_file) if monorepo else ""
templates.render(
template_name="bom_pom.xml.j2",
output_name=f"{artifact_id}-bom/pom.xml",
Expand All @@ -503,16 +518,15 @@ def main(versions_file, monorepo):
modules=modules,
main_module=main_module,
monorepo=monorepo,
monorepo_version=monorepo_version
monorepo_version=monorepo_version,
)

if os.path.isfile("pom.xml"):
print("updating modules in parent pom.xml")
update_parent_pom("pom.xml", modules)
else:
print("creating missing parent pom.xml")
monorepo_version = __get_monorepo_version(versions_file) \
if monorepo else ""
monorepo_version = __get_monorepo_version(versions_file) if monorepo else ""
templates.render(
template_name="parent_pom.xml.j2",
output_name="./pom.xml",
Expand All @@ -521,7 +535,7 @@ def main(versions_file, monorepo):
main_module=main_module,
name=name,
monorepo=monorepo,
monorepo_version=monorepo_version
monorepo_version=monorepo_version,
)

print(f"updating modules in {versions_file}")
Expand All @@ -537,13 +551,15 @@ def main(versions_file, monorepo):
release_version=main_module.release_version,
)
templates.render(
template_name="versions.txt.j2", output_name=versions_file, modules=existing_modules.values(),
template_name="versions.txt.j2",
output_name=versions_file,
modules=existing_modules.values(),
)


if __name__ == "__main__":
versions_file = sys.argv[1]
monorepo = sys.argv[2]
if monorepo == 'true':
if monorepo == "true":
monorepo = True
main(versions_file, monorepo)