From a59794b3122e4298560a5eab8b821360a4440d00 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 27 Sep 2019 11:58:47 -0400 Subject: [PATCH] feat: add java helper for fixing missing license headers (#294) * feat: add java helper for fixing missing license headers * chore: fix lint --- synthtool/languages/java.py | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/synthtool/languages/java.py b/synthtool/languages/java.py index 5a437ad59..8ab6cbd6f 100644 --- a/synthtool/languages/java.py +++ b/synthtool/languages/java.py @@ -15,6 +15,7 @@ import glob import os import requests +import synthtool as s from synthtool import cache from synthtool import log from synthtool import shell @@ -22,6 +23,38 @@ JAR_DOWNLOAD_URL = "https://github.com/google/google-java-format/releases/download/google-java-format-{version}/google-java-format-{version}-all-deps.jar" DEFAULT_FORMAT_VERSION = "1.7" +GOOD_LICENSE = """ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +""" +PROTOBUF_HEADER = "// Generated by the protocol buffer compiler. DO NOT EDIT!" +BAD_LICENSE = """/\\* + \\* Copyright 2018 Google LLC + \\* + \\* Licensed under the Apache License, Version 2.0 \\(the "License"\\); you may not use this file except + \\* in compliance with the License. You may obtain a copy of the License at + \\* + \\* http://www.apache.org/licenses/LICENSE-2.0 + \\* + \\* Unless required by applicable law or agreed to in writing, software distributed under the License + \\* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + \\* or implied. See the License for the specific language governing permissions and limitations under + \\* the License. + \\*/ +""" def format_code( @@ -52,3 +85,20 @@ def _download_formatter(version: str, dest: Path) -> None: response.raise_for_status() with open(dest, "wb") as fh: fh.write(response.content) + + +def fix_proto_headers(proto_root: Path) -> None: + s.replace( + [proto_root / "src/**/*.java"], + PROTOBUF_HEADER, + f"{GOOD_LICENSE}{PROTOBUF_HEADER}", + ) + s.replace([proto_root / "src/**/*Name.java"], BAD_LICENSE, GOOD_LICENSE) + + +def fix_grpc_headers(grpc_root: Path, package_name: str) -> None: + s.replace( + [grpc_root / "src/**/*.java"], + f"package {package_name};", + f"{GOOD_LICENSE}package {package_name};", + )