|
| 1 | +# Copyright (c) 2024, Oracle and/or its affiliates. |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License, version 2.0, as |
| 5 | +# published by the Free Software Foundation. |
| 6 | +# |
| 7 | +# This program is designed to work with certain software (including |
| 8 | +# but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | +# as designated in a particular file or component or in included license |
| 10 | +# documentation. The authors of MySQL hereby grant you an |
| 11 | +# additional permission to link the program and your derivative works |
| 12 | +# with the separately licensed software that they have either included with |
| 13 | +# the program or referenced in the documentation. |
| 14 | +# |
| 15 | +# Without limiting anything contained in the foregoing, this file, |
| 16 | +# which is part of MySQL Connector/Python, is also subject to the |
| 17 | +# Universal FOSS Exception, version 1.0, a copy of which can be found at |
| 18 | +# http://oss.oracle.com/licenses/universal-foss-exception. |
| 19 | +# |
| 20 | +# This program is distributed in the hope that it will be useful, but |
| 21 | +# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 23 | +# See the GNU General Public License, version 2.0, for more details. |
| 24 | +# |
| 25 | +# You should have received a copy of the GNU General Public License |
| 26 | +# along with this program; if not, write to the Free Software Foundation, Inc., |
| 27 | +# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 28 | + |
| 29 | +""" |
| 30 | +Script to update the copyright header of the source files of connectors distributed |
| 31 | +with `connector-python`. |
| 32 | +
|
| 33 | +This program traverses each connector package included in the `connector-python` |
| 34 | +looking for source code, configuration, installation, documentation, and supporting |
| 35 | +files that include the `Copyright` header pattern. |
| 36 | +
|
| 37 | +As a result, such a `Copyright` header is updated based on the rules defined in this |
| 38 | +very script. Also, this script expects no command-line arguments, however, a relative |
| 39 | +location to the root folder `connector-python` is assumed, therefore, if you move this |
| 40 | +script to another location, it won't work. |
| 41 | +
|
| 42 | +It's up to the developer to decide where or when this program |
| 43 | +will be executed. |
| 44 | +
|
| 45 | +Use case example: In the event of a "Copyright" change, the developer can update the |
| 46 | +rules found in this program and run it to make the changes effective across the |
| 47 | +relevant code base. |
| 48 | +""" |
| 49 | + |
| 50 | +import datetime |
| 51 | +import logging |
| 52 | +import os |
| 53 | +import re |
| 54 | + |
| 55 | +from pathlib import Path |
| 56 | +from typing import List, Tuple, Union |
| 57 | + |
| 58 | +CURRENT_YEAR = datetime.date.today().year |
| 59 | +DEFAULT_FOUND_YEAR = 2023 |
| 60 | + |
| 61 | +# `sol` stands for start-of-line |
| 62 | +COPYRIGHT_HEADER = """{sol} Copyright (c) {creation_year}{current_year}, Oracle and/or its affiliates. |
| 63 | +{sol} |
| 64 | +{sol} This program is free software; you can redistribute it and/or modify |
| 65 | +{sol} it under the terms of the GNU General Public License, version 2.0, as |
| 66 | +{sol} published by the Free Software Foundation. |
| 67 | +{sol} |
| 68 | +{sol} This program is designed to work with certain software (including |
| 69 | +{sol} but not limited to OpenSSL) that is licensed under separate terms, |
| 70 | +{sol} as designated in a particular file or component or in included license |
| 71 | +{sol} documentation. The authors of MySQL hereby grant you an |
| 72 | +{sol} additional permission to link the program and your derivative works |
| 73 | +{sol} with the separately licensed software that they have either included with |
| 74 | +{sol} the program or referenced in the documentation. |
| 75 | +{sol} |
| 76 | +{sol} Without limiting anything contained in the foregoing, this file, |
| 77 | +{sol} which is part of MySQL Connector/Python, is also subject to the |
| 78 | +{sol} Universal FOSS Exception, version 1.0, a copy of which can be found at |
| 79 | +{sol} http://oss.oracle.com/licenses/universal-foss-exception. |
| 80 | +{sol} |
| 81 | +{sol} This program is distributed in the hope that it will be useful, but |
| 82 | +{sol} WITHOUT ANY WARRANTY; without even the implied warranty of |
| 83 | +{sol} MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 84 | +{sol} See the GNU General Public License, version 2.0, for more details. |
| 85 | +{sol} |
| 86 | +{sol} You should have received a copy of the GNU General Public License |
| 87 | +{sol} along with this program; if not, write to the Free Software Foundation, Inc., |
| 88 | +{sol} 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA""" |
| 89 | + |
| 90 | + |
| 91 | +RE_FLAGS = re.MULTILINE | re.DOTALL |
| 92 | +PATTERN = r"^{sol_pattern}\sCopyright\s.*?\sMA\s+02110-1301\s+USA$" |
| 93 | +PATTERN_HEADER_PY = re.compile(PATTERN.format(sol_pattern=r"#"), flags=RE_FLAGS) |
| 94 | +PATTERN_HEADER_C = re.compile(PATTERN.format(sol_pattern=r"\s\*"), flags=RE_FLAGS) |
| 95 | +PATTERN_HEADER_PLAIN = re.compile( |
| 96 | + r"^Copyright\s.*?\sMA\s+02110-1301\s+USA$", flags=RE_FLAGS |
| 97 | +) |
| 98 | +PATTERN_YEARS = r"Copyright\s+\(c\)\s+\d{4},\s*\d{4},\s+Oracle" |
| 99 | + |
| 100 | +TARGET_SRC_PY_EXTS = [".py", ".sh", ".spec", ".postinst", ".postrm", ".toml"] |
| 101 | +TARGET_SRC_C_EXTS = [".c", ".h", ".cpp", ".cc", ".proto"] |
| 102 | +TARGET_SRC_PLAIN_EXTS = [".rst"] |
| 103 | + |
| 104 | +TARGET_SRC_EXTS = TARGET_SRC_PY_EXTS + TARGET_SRC_C_EXTS + TARGET_SRC_PLAIN_EXTS |
| 105 | + |
| 106 | +SRC_BUT_EXTENSIONLESS = ["rules", "Dockerfile"] |
| 107 | + |
| 108 | +SHARED_FILES = [ |
| 109 | + "pyproject.toml", |
| 110 | +] |
| 111 | + |
| 112 | +MYSQL_CONNECTORS = { |
| 113 | + "classic": "mysql-connector-python", |
| 114 | + "xdevapi": "mysqlx-connector-python", |
| 115 | +} |
| 116 | + |
| 117 | +EXCLUDE = [ |
| 118 | + os.path.join(MYSQL_CONNECTORS["classic"], "build"), |
| 119 | + os.path.join(MYSQL_CONNECTORS["classic"], "lib", "mysql", "opentelemetry"), |
| 120 | + os.path.join(MYSQL_CONNECTORS["xdevapi"], "build"), |
| 121 | +] |
| 122 | + |
| 123 | +logging.basicConfig(level=logging.DEBUG) |
| 124 | +logger = logging.getLogger("cpy.headerfy") |
| 125 | + |
| 126 | + |
| 127 | +def update_header(root: Union[str, Path], filename: str) -> None: |
| 128 | + """Updates the Copyright header of the provided file. |
| 129 | +
|
| 130 | + Args: |
| 131 | + root: Absolute path to the folder where `file` can be found. |
| 132 | + filename: File name to be updated. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + None |
| 136 | +
|
| 137 | + Raises: |
| 138 | + PermissionError, FileNotFoundError, FileExistsError: If the opening of the |
| 139 | + file fails. |
| 140 | + RuntimeError: The file extension isn't supported. |
| 141 | + """ |
| 142 | + path_to_file = os.path.join(root, filename) |
| 143 | + |
| 144 | + pattern = None |
| 145 | + if ( |
| 146 | + any((filename.endswith(ext) for ext in TARGET_SRC_PY_EXTS)) |
| 147 | + or filename in SRC_BUT_EXTENSIONLESS |
| 148 | + ): |
| 149 | + pattern = PATTERN_HEADER_PY |
| 150 | + elif any((filename.endswith(ext) for ext in TARGET_SRC_C_EXTS)): |
| 151 | + pattern = PATTERN_HEADER_C |
| 152 | + elif any((filename.endswith(ext) for ext in TARGET_SRC_PLAIN_EXTS)): |
| 153 | + pattern = PATTERN_HEADER_PLAIN |
| 154 | + else: |
| 155 | + raise RuntimeError(f"Couldn't resolve extension for {filename}") |
| 156 | + |
| 157 | + sol = None |
| 158 | + if pattern == PATTERN_HEADER_PY: |
| 159 | + sol = "#" |
| 160 | + elif pattern == PATTERN_HEADER_C: |
| 161 | + sol = " *" |
| 162 | + elif pattern == PATTERN_HEADER_PLAIN: |
| 163 | + sol = "" |
| 164 | + |
| 165 | + with open(path_to_file, mode="r", encoding="utf8") as fp: |
| 166 | + text_old = fp.read() |
| 167 | + |
| 168 | + match_header = re.search(pattern, text_old) |
| 169 | + |
| 170 | + # no copyright header |
| 171 | + if match_header is None: |
| 172 | + return |
| 173 | + |
| 174 | + two_years_snippet = re.search(PATTERN_YEARS, match_header.group()) |
| 175 | + creation_year = ( |
| 176 | + two_years_snippet.group().split(",")[0].replace("Copyright (c)", "").strip() |
| 177 | + if two_years_snippet is not None |
| 178 | + else "" |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | + logger.info("%s (%s)", path_to_file, creation_year if creation_year else "NULL") |
| 183 | + |
| 184 | + creation_year = f"{creation_year}, " if creation_year else f"{DEFAULT_FOUND_YEAR}, " |
| 185 | + |
| 186 | + text_new = re.sub( |
| 187 | + pattern, |
| 188 | + COPYRIGHT_HEADER.format( |
| 189 | + sol=sol, creation_year=creation_year, current_year=CURRENT_YEAR |
| 190 | + ), |
| 191 | + text_old, |
| 192 | + ) |
| 193 | + |
| 194 | + with open(path_to_file, mode="w", encoding="utf8") as fp: |
| 195 | + fp.write(text_new) |
| 196 | + |
| 197 | + |
| 198 | +def headerify( |
| 199 | + path_base: Union[str, Path], package_name: str |
| 200 | +) -> List[Tuple[str, Exception]]: |
| 201 | + """Traverses the given package directory and hands over the relevant* files to be |
| 202 | + updated to `update_header` which drives the actual update. |
| 203 | +
|
| 204 | + [*]: Including source, configuration, installation and further supporting files |
| 205 | + such as documentation files. |
| 206 | +
|
| 207 | + Args: |
| 208 | + path_base: Absolute path to the folder where `package_name` can be found. |
| 209 | + package_name: The connector to be processed. |
| 210 | +
|
| 211 | + Returns: |
| 212 | + issues: A list of 2-tuple items; 1st includes the absolute path to a file |
| 213 | + that couldn't be updated and 2nd informs the corresponding raised error. |
| 214 | + """ |
| 215 | + warns: List[Tuple[str, Exception]] = [] |
| 216 | + |
| 217 | + for root, _, files in os.walk(Path(path_base, package_name)): |
| 218 | + if any( |
| 219 | + (os.path.join(path_base, path_exclude) in root for path_exclude in EXCLUDE) |
| 220 | + ): |
| 221 | + continue |
| 222 | + for filename in files: |
| 223 | + if ( |
| 224 | + any((filename.endswith(ext) for ext in TARGET_SRC_EXTS)) |
| 225 | + or filename in SRC_BUT_EXTENSIONLESS |
| 226 | + ): |
| 227 | + try: |
| 228 | + update_header(root, filename) |
| 229 | + except (PermissionError, FileNotFoundError, FileExistsError) as err: |
| 230 | + warns.append((os.path.join(root, filename), err)) |
| 231 | + return warns |
| 232 | + |
| 233 | + |
| 234 | +if __name__ == "__main__": |
| 235 | + path_to_base = Path(__file__).parent.parent |
| 236 | + issues: List[Tuple[str, Exception]] = [] |
| 237 | + |
| 238 | + for pkg_name in MYSQL_CONNECTORS.values(): |
| 239 | + issues.extend( |
| 240 | + headerify( |
| 241 | + path_base=path_to_base, |
| 242 | + package_name=pkg_name, |
| 243 | + ) |
| 244 | + ) |
| 245 | + |
| 246 | + for file in SHARED_FILES: |
| 247 | + try: |
| 248 | + update_header(path_to_base, file) |
| 249 | + except (PermissionError, FileNotFoundError, FileExistsError) as error: |
| 250 | + issues.append((os.path.join(path_to_base, file), error)) |
| 251 | + |
| 252 | + if issues: |
| 253 | + print("-" * 10 + "WARNING" + "-" * 10) |
| 254 | + for path_file, exc in issues: |
| 255 | + logger.warning("Couldn't update %s. Got %s", path_file, exc) |
0 commit comments