|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (c) 2024, APT Group, Department of Computer Science, |
| 5 | +# The University of Manchester. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import xml.etree.ElementTree as ET |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +def generate_backend_profiles_as_xml_entries(BACKEND_PROFILES): |
| 27 | + xml_entries = [] |
| 28 | + for backend in BACKEND_PROFILES: |
| 29 | + xml_entry_line = f'<entry key="{backend}" value="true" />' |
| 30 | + xml_entries.append(xml_entry_line) |
| 31 | + |
| 32 | + return "\n".join(xml_entries) |
| 33 | + |
| 34 | +def read_template(template_path): |
| 35 | + with open(template_path, 'r') as file: |
| 36 | + template_content = file.read() |
| 37 | + return template_content |
| 38 | + |
| 39 | +def write_generated_template(xml_content, xml_file_path): |
| 40 | + with open(xml_file_path, "w") as file: |
| 41 | + file.write(xml_content) |
| 42 | + |
| 43 | +def define_and_get_internal_maven_content(xml_templates_directory, project_directory, java_home, backend_profiles): |
| 44 | + maven_directory = os.path.join(project_directory, "etc", "dependencies", "apache-maven-3.9.3") |
| 45 | + xml_internal_maven_build_content_directory = os.path.join(xml_templates_directory, "maven_template.xml") |
| 46 | + xml_internal_maven_build_content = read_template(xml_internal_maven_build_content_directory) |
| 47 | + |
| 48 | + if "graal" in java_home: |
| 49 | + java_profile = "graal-jdk-21" |
| 50 | + else: |
| 51 | + java_profile = "jdk21" |
| 52 | + |
| 53 | + xml_backend_profiles = generate_backend_profiles_as_xml_entries(backend_profiles) |
| 54 | + |
| 55 | + xml_internal_maven_build_content = xml_internal_maven_build_content.replace("[@@MAVEN_DIRECTORY@@]", maven_directory) |
| 56 | + xml_internal_maven_build_content = xml_internal_maven_build_content.replace("[@@JAVA_PROFILE@@]", java_profile) |
| 57 | + xml_internal_maven_build_content = xml_internal_maven_build_content.replace("[@@BACKEND_PROFILES@@]", xml_backend_profiles) |
| 58 | + xml_internal_maven_build_content = xml_internal_maven_build_content.replace("[@@PROJECT_DIR@@]", project_directory) |
| 59 | + |
| 60 | + return xml_internal_maven_build_content |
| 61 | + |
| 62 | +def define_and_get_tornadovm_build_content(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles): |
| 63 | + post_installation_script_directory = os.path.join(project_directory, "bin", "post_installation.py") |
| 64 | + xml_TornadoVM_build_content_directory = os.path.join(xml_templates_directory, "tornadovm_build_template.xml") |
| 65 | + xml_TornadoVM_build_content = read_template(xml_TornadoVM_build_content_directory) |
| 66 | + |
| 67 | + python_version = subprocess.check_output([str(python_home), "--version"]).decode().strip() |
| 68 | + python_version_name = python_version.split()[1] |
| 69 | + python_sdk_home = str(python_home) |
| 70 | + python_sdk_name = f"Python {python_version_name}" |
| 71 | + |
| 72 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@TORNADO_SDK@@]", tornado_sdk) |
| 73 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@JAVA_HOME@@]", java_home) |
| 74 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@BACKEND_PROFILES@@]", backend_profiles) |
| 75 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@PYTHON_SDK_HOME@@]", python_sdk_home) |
| 76 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@PYTHON_SDK_NAME@@]", python_sdk_name) |
| 77 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@PROJECT_DIR@@]", project_directory) |
| 78 | + xml_TornadoVM_build_content = xml_TornadoVM_build_content.replace("[@@POST_INSTALLATION_SCRIPT_DIRECTORY@@]", post_installation_script_directory) |
| 79 | + |
| 80 | + return xml_TornadoVM_build_content |
| 81 | + |
| 82 | +def define_and_get_tornadovm_tests_content(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles): |
| 83 | + test_script_directory = os.path.join(tornado_sdk, "bin", "tornado-test") |
| 84 | + xml_TornadoVM_tests_content_directory = os.path.join(xml_templates_directory, "tornadovm_tests_template.xml") |
| 85 | + xml_TornadoVM_tests_content = read_template(xml_TornadoVM_tests_content_directory) |
| 86 | + |
| 87 | + python_version = subprocess.check_output([str(python_home), "--version"]).decode().strip() |
| 88 | + python_version_name = python_version.split()[1] |
| 89 | + python_sdk_home = str(python_home) |
| 90 | + python_sdk_name = f"Python {python_version_name}" |
| 91 | + |
| 92 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@BACKEND_PROFILES@@]", backend_profiles) |
| 93 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@TORNADO_SDK@@]", tornado_sdk) |
| 94 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@JAVA_HOME@@]", java_home) |
| 95 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@PYTHON_SDK_HOME@@]", python_sdk_home) |
| 96 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@PYTHON_SDK_NAME@@]", python_sdk_name) |
| 97 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@PROJECT_DIR@@]", project_directory) |
| 98 | + xml_TornadoVM_tests_content = xml_TornadoVM_tests_content.replace("[@@TORNADO_TEST_DIR@@]", test_script_directory) |
| 99 | + |
| 100 | + return xml_TornadoVM_tests_content |
| 101 | + |
| 102 | +def generate_internal_maven_build_xml(xml_templates_directory, project_directory, java_home, backend_profiles): |
| 103 | + xml_internal_content = define_and_get_internal_maven_content(xml_templates_directory, project_directory, java_home, backend_profiles) |
| 104 | + xml_build_directory = os.path.join(project_directory, ".build", "_internal_TornadoVM_Maven-cleanAndinstall.run.xml") |
| 105 | + print("Generating " + xml_build_directory) |
| 106 | + write_generated_template(xml_internal_content, xml_build_directory) |
| 107 | + |
| 108 | +def generate_tornadovm_build_xml(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles): |
| 109 | + xml_build_content = define_and_get_tornadovm_build_content(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles) |
| 110 | + xml_build_directory = os.path.join(project_directory, ".build", "TornadoVM-Build.run.xml") |
| 111 | + print("Generating " + xml_build_directory) |
| 112 | + write_generated_template(xml_build_content, xml_build_directory) |
| 113 | + |
| 114 | +def generate_tornadovm_tests_xml(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles): |
| 115 | + xml_tests_content = define_and_get_tornadovm_tests_content(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backend_profiles) |
| 116 | + xml_tests_directory = os.path.join(project_directory, ".build", "TornadoVM-Tests.run.xml") |
| 117 | + print("Generating " + xml_tests_directory) |
| 118 | + write_generated_template(xml_tests_content, xml_tests_directory) |
| 119 | + |
| 120 | +def generate_xml_files(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backends): |
| 121 | + backends_separated_comma = ",".join(backends) |
| 122 | + generate_internal_maven_build_xml(xml_templates_directory, project_directory, java_home, backends) |
| 123 | + generate_tornadovm_build_xml(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backends_separated_comma) |
| 124 | + generate_tornadovm_tests_xml(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backends_separated_comma) |
| 125 | + print("IntelIj Files Generated ............... [ok]") |
| 126 | + |
| 127 | +def cleanup_build_directory(build_directory_string): |
| 128 | + tornado_root_directory = Path(build_directory_string) |
| 129 | + if tornado_root_directory.exists() and tornado_root_directory.is_dir(): |
| 130 | + for file in tornado_root_directory.iterdir(): |
| 131 | + if file.name == ".gitignore": |
| 132 | + continue |
| 133 | + if file.is_file(): |
| 134 | + file.unlink() |
| 135 | + |
| 136 | +def tornadovm_ide_init(tornado_sdk, java_home, backends): |
| 137 | + """ |
| 138 | + Function to generate IDEA xml files for building and running TornadoVM. |
| 139 | + """ |
| 140 | + |
| 141 | + if tornado_sdk == None: |
| 142 | + print("Cannot initiate ide. TORNADO_SDK is not defined") |
| 143 | + sys.exit(0) |
| 144 | + |
| 145 | + if java_home == None: |
| 146 | + print("Cannot initiate ide. JAVA_HOME is not defined") |
| 147 | + sys.exit(0) |
| 148 | + |
| 149 | + if backends == None: |
| 150 | + print("Cannot initiate ide. Backends are not defined") |
| 151 | + sys.exit(0) |
| 152 | + |
| 153 | + if os.name == 'nt': |
| 154 | + # Multiple paths of python executables may be listed, the first is selected as default |
| 155 | + python_home = subprocess.check_output(["where", "python"]).decode().strip().split("\r\n") |
| 156 | + python_home = Path(python_home[0]) |
| 157 | + else: |
| 158 | + python_home = subprocess.check_output(["which", "python3"]).decode().strip() |
| 159 | + |
| 160 | + if python_home == None: |
| 161 | + print("python is not found in the PATH.") |
| 162 | + sys.exit(0) |
| 163 | + |
| 164 | + tornado_sdk_path = Path(tornado_sdk) |
| 165 | + tornado_root = tornado_sdk_path.parents[1] |
| 166 | + project_directory = str(tornado_root) |
| 167 | + |
| 168 | + cleanup_build_directory(os.path.join(project_directory, ".build")) |
| 169 | + xml_templates_directory = os.path.join(project_directory, "scripts", "templates", "intellij-settings", "ideinit") |
| 170 | + |
| 171 | + generate_xml_files(xml_templates_directory, project_directory, tornado_sdk, java_home, python_home, backends) |
0 commit comments