Skip to content

Commit

Permalink
[python] don't overwrite version.py file if existing version is higher (
Browse files Browse the repository at this point in the history
#5559)

fixes #5528, #5529

---------

Co-authored-by: iscai-msft <[email protected]>
Co-authored-by: Yuchao Yan <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent 8774715 commit fda51ff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/http-client-python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log - @typespec/http-client-python

## 0.6.2

### Bug Fixes

- Don't automatically overwrite version in `_version.py` file and `setup.py` file if the existing version is newer

## 0.6.1

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# --------------------------------------------------------------------------
import logging
from collections import namedtuple
import re
from typing import List, Any, Union
from pathlib import Path
from jinja2 import PackageLoader, Environment, FileSystemLoader, StrictUndefined
Expand Down Expand Up @@ -94,6 +95,19 @@ def serialize_loop(self) -> List[AsyncInfo]:
async_loop = AsyncInfo(async_mode=True, async_path="aio/")
return [sync_loop, async_loop] if self.has_aio_folder else [sync_loop]

@property
def keep_version_file(self) -> bool:
if self.options.get("keep_version_file"):
return True
# If the version file is already there and the version is greater than the current version, keep it.
try:
serialized_version_file = self.read_file(self.exec_path(self.code_model.namespace) / "_version.py")
match = re.search(r'VERSION\s*=\s*"([^"]+)"', str(serialized_version_file))
serialized_version = match.group(1) if match else ""
except (FileNotFoundError, IndexError):
serialized_version = ""
return serialized_version > self.code_model.options["package_version"]

def serialize(self) -> None:
env = Environment(
loader=PackageLoader("pygen.codegen", "templates"),
Expand Down Expand Up @@ -193,6 +207,9 @@ def _serialize_and_write_package_files(self, client_namespace: str) -> None:
file = template_name.replace(".jinja2", "")
output_name = root_of_sdk / file
if not self.read_file(output_name) or file in _REGENERATE_FILES:
if self.keep_version_file and file == "setup.py":
# don't regenerate setup.py file if the version file is more up to date
continue
self.write_file(
output_name,
serializer.serialize_package_file(template_name, **params),
Expand Down Expand Up @@ -329,10 +346,9 @@ def _write_version_file(original_version_file_name: str) -> None:
_read_version_file(original_version_file_name),
)

keep_version_file = self.code_model.options["keep_version_file"]
if keep_version_file and _read_version_file("_version.py"):
if self.keep_version_file and _read_version_file("_version.py"):
_write_version_file(original_version_file_name="_version.py")
elif keep_version_file and _read_version_file("version.py"):
elif self.keep_version_file and _read_version_file("version.py"):
_write_version_file(original_version_file_name="version.py")
elif self.code_model.options["package_version"]:
self.write_file(
Expand Down
4 changes: 2 additions & 2 deletions packages/http-client-python/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/http-client-python/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/http-client-python",
"version": "0.6.1",
"version": "0.6.2",
"author": "Microsoft Corporation",
"description": "TypeSpec emitter for Python SDKs",
"homepage": "https://typespec.io",
Expand Down

0 comments on commit fda51ff

Please sign in to comment.