Skip to content

Commit

Permalink
Version 1.2.10 (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhughes-usgs authored Jun 17, 2024
1 parent 6e8dd7b commit 76d3003
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Python package for building MODFLOW-based programs from source files.

### Version 1.2.10.dev0
### Version 1.2.10

[![PyPI Version](https://img.shields.io/pypi/v/mfpymake.png)](https://pypi.python.org/pypi/mfpymake)
[![Anaconda Version](https://anaconda.org/conda-forge/mfpymake/badges/version.svg)](https://anaconda.org/conda-forge/mfpymake)
Expand Down
3 changes: 3 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ autotest-schedule = { cmd = "pytest -v -m='schedule' --durations=0 --cov=pymake

# coverage report
coverage-report = { cmd = "coverage report", cwd = "autotest"}

# release
update-version = { cmd = "python scripts/update_version.py -v"}
7 changes: 5 additions & 2 deletions pymake/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# mfpymake version file automatically created using
# update_version.py on June 16, 2024 18:36:44

__author__ = "Joseph D. Hughes"
__date__ = "February 12, 2024"
__version__ = "1.2.10.dev0"
__date__ = "June 16, 2024"
__version__ = "1.2.10"
__maintainer__ = "Joseph D. Hughes"
__email__ = "[email protected]"
__status__ = "Production"
Expand Down
149 changes: 149 additions & 0 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import argparse
import re
import textwrap
from datetime import datetime
from pathlib import Path

from filelock import FileLock
from packaging.version import Version

_project_name = "mfpymake"
_project_root_path = Path(__file__).parent.parent
_version_txt_path = _project_root_path / "version.txt"
_version_py_path = _project_root_path / "pymake" / "config.py"

# file names and the path to the file relative to the repo root directory
file_paths_list = [
_project_root_path / "README.md",
_project_root_path / "version.txt",
_project_root_path / "pymake" / "config.py",
]
file_paths = {pth.name: pth for pth in file_paths_list} # keys for each file

def split_nonnumeric(s):
match = re.compile("[^0-9]").search(s)
return [s[: match.start()], s[match.start() :]] if match else s


_initial_version = Version("0.0.1")
_current_version = Version(_version_txt_path.read_text().strip())


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_version_py(timestamp: datetime, version: Version):
lines = file_paths["config.py"].read_text().rstrip().split("\n")

with open(_version_py_path, "w") as f:
f.write(
f"# {_project_name} version file automatically created using\n"
f"# {Path(__file__).name} on {timestamp:%B %d, %Y %H:%M:%S}\n\n"
)
for line in lines:
if "__date__" in line:
line = f'__date__ = "{timestamp:%B %d, %Y}"'
elif "__version__" in line:
line = f'__version__ = "{version}"'
f.write(f"{line}\n")
print(f"Updated {_version_py_path} to version {version}")



def update_readme_markdown(
timestamp: datetime, version: Version, approved: bool = False
):
fpth = file_paths["README.md"]

# read README.md into memory
lines = fpth.read_text().rstrip().split("\n")

# rewrite README.md
terminate = False
with open(fpth, "w") as f:
for line in lines:
if "### Version " in line:
line = f"### Version {version}"
f.write(f"{line}\n")
if terminate:
break

print(f"Updated {fpth} to version {version}")

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

with lock:
update_version_txt(version)
update_version_py(timestamp, version)
update_readme_markdown(timestamp, version, approved)
finally:
try:
lock_path.unlink()
except:
pass


if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=f"Update {_project_name} version",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent(
"""\
Update version information stored in version.txt in the project root,
as well as several other files in the repository. If --version is not
provided, the version number will not be changed. A file lock is held
to synchronize file access. The version tag must comply with standard
'<major>.<minor>.<patch>' format conventions for semantic versioning.
"""
),
)
parser.add_argument(
"-v",
"--version",
required=False,
help="Specify the release version",
)
parser.add_argument(
"-a",
"--approve",
required=False,
action="store_true",
help="Approve the release (defaults false)",
)
parser.add_argument(
"-g",
"--get",
required=False,
action="store_true",
help="Just get the current version number, no updates (defaults false)",
)
args = parser.parse_args()

if args.get:
print(
Version((_project_root_path / "version.txt").read_text().strip())
)
else:
update_version(
timestamp=datetime.now(),
version=(
Version(args.version) if args.version else _current_version
),
approved=args.approve,
)
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.2.10

0 comments on commit 76d3003

Please sign in to comment.