Skip to content

Commit

Permalink
ci: use packaging.version.Version in update_version.py (#91)
Browse files Browse the repository at this point in the history
* use packaging.version.Version in update_version.py
* bump minor not patch in release.yml
  • Loading branch information
wpbonelli authored Jul 26, 2023
1 parent 32425e4 commit 2844638
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 92 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,10 @@ jobs:
reset_branch="post-release-${{ steps.latest_tag.outputs.tag }}-reset"
git switch -c $reset_branch
# increment patch version
# increment minor version
major_version=$(echo "${{ steps.latest_tag.outputs.tag }}" | cut -d. -f1)
minor_version=$(echo "${{ steps.latest_tag.outputs.tag }}" | cut -d. -f2)
patch_version=$(echo "${{ steps.latest_tag.outputs.tag }}" | cut -d. -f3)
version="$major_version.$minor_version.$((patch_version + 1))"
version="$major_version.$((minor_version + 1)).0"
python scripts/update_version.py -v "$version"
python scripts/lint.py
Expand Down
102 changes: 13 additions & 89 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,26 @@
from typing import NamedTuple

from filelock import FileLock
from packaging.version import Version

_project_name = "modflow-devtools"
_project_root_path = Path(__file__).parent.parent
_version_txt_path = _project_root_path / "version.txt"
_package_init_path = _project_root_path / "modflow_devtools" / "__init__.py"
_readme_path = _project_root_path / "README.md"
_docs_config_path = _project_root_path / "docs" / "conf.py"
_initial_version = Version("0.0.1")
_current_version = Version(_version_txt_path.read_text().strip())


class Version(NamedTuple):
"""Semantic version number"""

major: int = 0
minor: int = 0
patch: int = 0

def __repr__(self):
return f"{self.major}.{self.minor}.{self.patch}"

@classmethod
def from_string(cls, version: str) -> "Version":
t = version.split(".")

vmajor = int(t[0])
vminor = int(t[1])
vpatch = int(t[2])

return cls(major=vmajor, minor=vminor, patch=vpatch)

@classmethod
def from_file(cls, path: PathLike) -> "Version":
lines = [
line.rstrip("\n")
for line in open(Path(path).expanduser().absolute(), "r")
]
vmajor = vminor = vpatch = None
for line in lines:
line = line.strip()
if not any(line):
continue
t = line.split(".")
vmajor = int(t[0])
vminor = int(t[1])
vpatch = int(t[2])

assert (
vmajor is not None and vminor is not None and vpatch is not None
), "version string must follow semantic version format: major.minor.patch"
return cls(major=vmajor, minor=vminor, patch=vpatch)


class ReleaseType(Enum):
CANDIDATE = "Release Candidate"
RELEASE = "Release"


_initial_version = Version(0, 0, 1)
_current_version = Version.from_file(_version_txt_path)


def update_version_txt(
release_type: ReleaseType, timestamp: datetime, version: Version
):
def update_version_txt(version: Version):
with open(_version_txt_path, "w") as f:
f.write(str(version))
print(f"Updated {_version_txt_path} to version {version}")


def update_init_py(
release_type: ReleaseType, timestamp: datetime, version: Version
timestamp: datetime, version: Version
):
lines = _package_init_path.read_text().rstrip().split("\n")
with open(_package_init_path, "w") as f:
Expand All @@ -89,22 +39,8 @@ def update_init_py(
print(f"Updated {_package_init_path} to version {version}")


def update_readme_markdown(
release_type: ReleaseType, timestamp: datetime, version: Version
):
lines = _readme_path.read_text().rstrip().split("\n")
with open(_readme_path, "w") as f:
for line in lines:
if "### Version " in line:
line = f"### Version {version}"
if release_type != ReleaseType.RELEASE:
line += f" — {release_type.value.lower()}"
f.write(f"{line}\n")
print(f"Updated {_readme_path} to version {version}")


def update_docs_config(
release_type: ReleaseType, timestamp: datetime, version: Version
timestamp: datetime, version: Version
):
lines = _docs_config_path.read_text().rstrip().split("\n")
with open(_docs_config_path, "w") as f:
Expand All @@ -116,25 +52,23 @@ def update_docs_config(


def update_version(
release_type: ReleaseType,
timestamp: datetime = datetime.now(),
version: Version = None,
):
lock_path = Path(_version_txt_path.name + ".lock")
try:
lock = FileLock(lock_path)
previous = Version.from_file(_version_txt_path)
previous = Version(_version_txt_path.read_text().strip())
version = (
version
if version
else Version(previous.major, previous.minor, previous.patch)
else Version(previous.major, previous.minor, previous.micro)
)

with lock:
update_version_txt(release_type, timestamp, version)
update_init_py(release_type, timestamp, version)
# update_readme_markdown(release_type, timestamp, version)
update_docs_config(release_type, timestamp, version)
update_version_txt(timestamp, version)
update_init_py(timestamp, version)
update_docs_config(timestamp, version)
finally:
try:
lock_path.unlink()
Expand Down Expand Up @@ -162,13 +96,6 @@ def update_version(
required=False,
help="Specify the release version",
)
parser.add_argument(
"-a",
"--approve",
required=False,
action="store_true",
help="Indicate release is approved (defaults to false for preliminary/development distributions)",
)
parser.add_argument(
"-g",
"--get",
Expand All @@ -179,14 +106,11 @@ def update_version(
args = parser.parse_args()

if args.get:
print(_current_version)
print(Version(_version_txt_path.read_text().strip()))
else:
update_version(
release_type=ReleaseType.RELEASE
if args.approve
else ReleaseType.CANDIDATE,
timestamp=datetime.now(),
version=Version.from_string(args.version)
version=Version(args.version)
if args.version
else _current_version,
)

0 comments on commit 2844638

Please sign in to comment.