|
| 1 | +# ------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) 2023 General Motors GTO LLC |
| 4 | +# |
| 5 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 6 | +# or more contributor license agreements. See the NOTICE file |
| 7 | +# distributed with this work for additional information |
| 8 | +# regarding copyright ownership. The ASF licenses this file |
| 9 | +# to you under the Apache License, Version 2.0 (the |
| 10 | +# "License"); you may not use this file except in compliance |
| 11 | +# with the License. You may obtain a copy of the License at |
| 12 | +# |
| 13 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +# |
| 15 | +# Unless required by applicable law or agreed to in writing, |
| 16 | +# software distributed under the License is distributed on an |
| 17 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | +# KIND, either express or implied. See the License for the |
| 19 | +# specific language governing permissions and limitations |
| 20 | +# under the License. |
| 21 | +# SPDX-FileType: SOURCE |
| 22 | +# SPDX-FileCopyrightText: 2023 General Motors GTO LLC |
| 23 | +# SPDX-License-Identifier: Apache-2.0 |
| 24 | +# |
| 25 | +# ------------------------------------------------------------------------- |
| 26 | + |
| 27 | +import os |
| 28 | +import re |
| 29 | +import shutil |
| 30 | +import subprocess |
| 31 | + |
| 32 | +import git |
| 33 | +from git import Repo |
| 34 | + |
| 35 | +REPO_URL = "https://github.com/eclipse-uprotocol/up-core-api.git" |
| 36 | +PROTO_REPO_DIR = "target" |
| 37 | +TAG_NAME = "uprotocol-core-api-1.5.6" |
| 38 | +PROTO_OUTPUT_DIR = os.path.join("uprotocol", "proto") |
| 39 | + |
| 40 | + |
| 41 | +def clone_or_pull(repo_url, PROTO_REPO_DIR): |
| 42 | + try: |
| 43 | + repo = Repo.clone_from(repo_url, PROTO_REPO_DIR) |
| 44 | + print(f"Repository cloned successfully from {repo_url} to {PROTO_REPO_DIR}") |
| 45 | + # Checkout the specific tag |
| 46 | + repo.git.checkout(TAG_NAME) |
| 47 | + except git.exc.GitCommandError as clone_error: |
| 48 | + try: |
| 49 | + git_pull_command = ["git", "pull", "origin", TAG_NAME] |
| 50 | + subprocess.run(git_pull_command, cwd=PROTO_REPO_DIR, check=True) |
| 51 | + print("Git pull successful after clone failure.") |
| 52 | + except subprocess.CalledProcessError as pull_error: |
| 53 | + print(f"Error during Git pull: {pull_error}") |
| 54 | + |
| 55 | + |
| 56 | +def execute_maven_command(project_dir, command): |
| 57 | + try: |
| 58 | + with subprocess.Popen(command, cwd=os.path.join(os.getcwd(), project_dir), shell=True, stdout=subprocess.PIPE, |
| 59 | + stderr=subprocess.PIPE, text=True) as process: |
| 60 | + stdout, stderr = process.communicate() |
| 61 | + print(stdout) |
| 62 | + |
| 63 | + if process.returncode != 0: |
| 64 | + print(f"Error: {stderr}") |
| 65 | + else: |
| 66 | + print("Maven command executed successfully.") |
| 67 | + src_directory = os.path.join(os.getcwd(), project_dir, "target", "generated-sources", "protobuf", |
| 68 | + "python") |
| 69 | + # if not os.path.exists(PROTO_OUTPUT_DIR): |
| 70 | + # os.makedirs(PROTO_OUTPUT_DIR) |
| 71 | + |
| 72 | + shutil.copytree(src_directory, PROTO_OUTPUT_DIR, dirs_exist_ok=True) |
| 73 | + process_python_protofiles(PROTO_OUTPUT_DIR) |
| 74 | + except Exception as e: |
| 75 | + print(f"Error executing Maven command: {e}") |
| 76 | + |
| 77 | + |
| 78 | +def replace_in_file(file_path, search_pattern, replace_pattern): |
| 79 | + with open(file_path, 'r') as file: |
| 80 | + file_content = file.read() |
| 81 | + |
| 82 | + updated_content = re.sub(search_pattern, replace_pattern, file_content) |
| 83 | + |
| 84 | + with open(file_path, 'w') as file: |
| 85 | + file.write(updated_content) |
| 86 | + |
| 87 | + |
| 88 | +def process_python_protofiles(directory): |
| 89 | + for root, dirs, files in os.walk(directory): |
| 90 | + create_init_py(root) |
| 91 | + for file in files: |
| 92 | + if file.endswith('.py'): |
| 93 | + file_path = os.path.join(root, file) |
| 94 | + replace_in_file(file_path, r'import uri_pb2', 'import uprotocol.proto.uri_pb2') |
| 95 | + replace_in_file(file_path, r'import uuid_pb2', 'import uprotocol.proto.uuid_pb2') |
| 96 | + replace_in_file(file_path, r'import uprotocol_options_pb2', |
| 97 | + 'import uprotocol.proto.uprotocol_options_pb2') |
| 98 | + replace_in_file(file_path, r'import uattributes_pb2', 'import uprotocol.proto.uattributes_pb2') |
| 99 | + replace_in_file(file_path, r'import upayload_pb2', 'import uprotocol.proto.upayload_pb2') |
| 100 | + replace_in_file(file_path, r'import ustatus_pb2', 'import uprotocol.proto.ustatus_pb2') |
| 101 | + replace_in_file(file_path, r'import upayload_pb2', 'import uprotocol.proto.upayload_pb2') |
| 102 | + replace_in_file(file_path, r'import umessage_pb2', 'import uprotocol.proto.umessage_pb2') |
| 103 | + |
| 104 | + |
| 105 | +def create_init_py(directory): |
| 106 | + init_file_path = os.path.join(directory, "__init__.py") |
| 107 | + |
| 108 | + # Check if the file already exists |
| 109 | + if not os.path.exists(init_file_path): |
| 110 | + # Create an empty __init__.py file |
| 111 | + with open(init_file_path, "w"): |
| 112 | + pass |
| 113 | + |
| 114 | + |
| 115 | +def execute(): |
| 116 | + clone_or_pull(REPO_URL, PROTO_REPO_DIR) |
| 117 | + |
| 118 | + # Execute mvn compile-python |
| 119 | + maven_command = "mvn protobuf:compile-python" |
| 120 | + execute_maven_command(PROTO_REPO_DIR, maven_command) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + execute() |
0 commit comments