|
5 | 5 | # -------------------------------------------------------------------------- |
6 | 6 | import logging |
7 | 7 | from collections import namedtuple |
| 8 | +import re |
8 | 9 | from typing import List, Any, Union |
9 | 10 | from pathlib import Path |
10 | 11 | from jinja2 import PackageLoader, Environment, FileSystemLoader, StrictUndefined |
@@ -94,6 +95,19 @@ def serialize_loop(self) -> List[AsyncInfo]: |
94 | 95 | async_loop = AsyncInfo(async_mode=True, async_path="aio/") |
95 | 96 | return [sync_loop, async_loop] if self.has_aio_folder else [sync_loop] |
96 | 97 |
|
| 98 | + @property |
| 99 | + def keep_version_file(self) -> bool: |
| 100 | + if self.options.get("keep_version_file"): |
| 101 | + return True |
| 102 | + # If the version file is already there and the version is greater than the current version, keep it. |
| 103 | + try: |
| 104 | + serialized_version_file = self.read_file(self.exec_path(self.code_model.namespace) / "_version.py") |
| 105 | + match = re.search(r'VERSION\s*=\s*"([^"]+)"', str(serialized_version_file)) |
| 106 | + serialized_version = match.group(1) if match else "" |
| 107 | + except (FileNotFoundError, IndexError): |
| 108 | + serialized_version = "" |
| 109 | + return serialized_version > self.code_model.options["package_version"] |
| 110 | + |
97 | 111 | def serialize(self) -> None: |
98 | 112 | env = Environment( |
99 | 113 | loader=PackageLoader("pygen.codegen", "templates"), |
@@ -193,6 +207,9 @@ def _serialize_and_write_package_files(self, client_namespace: str) -> None: |
193 | 207 | file = template_name.replace(".jinja2", "") |
194 | 208 | output_name = root_of_sdk / file |
195 | 209 | if not self.read_file(output_name) or file in _REGENERATE_FILES: |
| 210 | + if self.keep_version_file and file == "setup.py": |
| 211 | + # don't regenerate setup.py file if the version file is more up to date |
| 212 | + continue |
196 | 213 | self.write_file( |
197 | 214 | output_name, |
198 | 215 | serializer.serialize_package_file(template_name, **params), |
@@ -329,10 +346,9 @@ def _write_version_file(original_version_file_name: str) -> None: |
329 | 346 | _read_version_file(original_version_file_name), |
330 | 347 | ) |
331 | 348 |
|
332 | | - keep_version_file = self.code_model.options["keep_version_file"] |
333 | | - if keep_version_file and _read_version_file("_version.py"): |
| 349 | + if self.keep_version_file and _read_version_file("_version.py"): |
334 | 350 | _write_version_file(original_version_file_name="_version.py") |
335 | | - elif keep_version_file and _read_version_file("version.py"): |
| 351 | + elif self.keep_version_file and _read_version_file("version.py"): |
336 | 352 | _write_version_file(original_version_file_name="version.py") |
337 | 353 | elif self.code_model.options["package_version"]: |
338 | 354 | self.write_file( |
|
0 commit comments