Commit 7d47fa70 authored by liziwl's avatar liziwl
Browse files

更新脚本

parent 53a69a98
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -29,6 +29,8 @@ Changelog 维护应保持最新版本在文件上面的原则,日期格式按
- 预发布版本使用形如 "`[1.0.2-rc.1] - 2021-06-11`" 作为二级标题,
- 预发布版本使用形如 "`[1.0.2-rc.1] - 2021-06-11`" 作为二级标题,
- 正式发布版本使用形如 "`[1.0.1] - 2020-07-25`" 作为二级标题。
- 正式发布版本使用形如 "`[1.0.1] - 2020-07-25`" 作为二级标题。


<!-- BODY -->

## [Unreleased](https://github.com/SUSTech-CRA/sustech-master-thesis/compare/v1.3.21...HEAD)
## [Unreleased](https://github.com/SUSTech-CRA/sustech-master-thesis/compare/v1.3.21...HEAD)


## [1.3.21](https://github.com/SUSTech-CRA/sustech-master-thesis/compare/v1.3.20...v1.3.21) - 2024-04-29
## [1.3.21](https://github.com/SUSTech-CRA/sustech-master-thesis/compare/v1.3.20...v1.3.21) - 2024-04-29
+66 −19
Original line number Original line Diff line number Diff line
import os
import os
import re
import re
import sys


filename = "sustechthesis.dtx"
filename = "sustechthesis.dtx"
changelog_file = "CHANGELOG.md"
repository_url = "https://github.com/SUSTech-CRA/sustech-master-thesis"




def change_meta(version, date):
def change_meta(version, date):
    with open(filename, "r", encoding="utf-8") as f1:
    with open(filename, "r", encoding="utf-8") as f1:
        with open(f"{filename}.part", "w", encoding="utf-8") as f2:
        with open(f"{filename}.part", "w", encoding="utf-8") as f2:
            for line in f1:
            for line in f1:
                if "Southern University of Science and Technology Thesis Template" in line:
                if (
                    "Southern University of Science and Technology Thesis Template"
                    in line
                ):
                    print(line)
                    print(line)
                    line = re.sub("\d+\.\d+\.\d+", version, line)
                    line = re.sub("\d+\.\d+\.\d+", version, line)
                    line = re.sub("\d{4}/\d{2}/\d{2}", date, line)
                    line = re.sub("\d{4}/\d{2}/\d{2}", date, line)
@@ -26,6 +30,45 @@ def change_meta(version, date):
    os.rename(f"{filename}.part", filename)
    os.rename(f"{filename}.part", filename)




def generate_changelog_md(old_version, new_version, date):
    new_ver = f"v{new_version}"
    old_ver = "v" + ".".join(old_version)
    md_template = f"""## [Unreleased]({repository_url}/compare/{new_ver}...HEAD)

## [{new_ver}]({repository_url}/compare/{old_ver}...{new_ver}) - {date}

### Fixed:
- 这里可以添加具体的修复内容,根据实际情况调整。

### Changed
- 这里可以添加具体的修复内容,根据实际情况调整。

### Added
- 这里可以添加具体的修复内容,根据实际情况调整。

### Removed
- 这里可以添加具体的修复内容,根据实际情况调整。
"""

    return md_template


def update_changelog(old_version, new_version, date):
    date_hyphen = date.replace("/", "-")
    with open(changelog_file, "r", encoding="utf-8") as f:
        changelog_content = f.read()
        body_index = changelog_content.index("<!-- BODY -->") + len("<!-- BODY -->")
        new_entry = "\n" * 2 + generate_changelog_md(
            old_version, new_version, date_hyphen
        )
        print(new_entry)
        updated_content = (
            changelog_content[:body_index] + new_entry + changelog_content[body_index:]
        )
    with open(changelog_file, "w", encoding="utf-8") as f:
        f.write(updated_content)


def get_meta():
def get_meta():
    with open(filename, "r", encoding="utf-8") as f1:
    with open(filename, "r", encoding="utf-8") as f1:
        for line in f1:
        for line in f1:
@@ -33,19 +76,16 @@ def get_meta():
                try:
                try:
                    version = re.findall("(\d+)\.(\d+)\.(\d+)", line)[0]
                    version = re.findall("(\d+)\.(\d+)\.(\d+)", line)[0]
                    date = re.findall("\d{4}/\d{2}/\d{2}", line)
                    date = re.findall("\d{4}/\d{2}/\d{2}", line)
                    return {
                    return {"version": version, "release_date": date}
                        "version": version,
                        "release_date": date
                    }
                except:
                except:
                    return None
                    return None




def bump_version(version, level='patch'):
def bump_version(version, level="patch"):
    a, b, c = version
    a, b, c = version
    if level == 'major':
    if level == "major":
        a = int(a) + 1
        a = int(a) + 1
    elif level == 'minor':
    elif level == "minor":
        b = int(b) + 1
        b = int(b) + 1
    else:  # level == 'patch'
    else:  # level == 'patch'
        c = int(c) + 1
        c = int(c) + 1
@@ -56,6 +96,7 @@ def bump_version(version, level='patch'):
if __name__ == "__main__":
if __name__ == "__main__":
    import argparse
    import argparse
    import time
    import time

    try:
    try:
        meta = get_meta()
        meta = get_meta()
        if meta:
        if meta:
@@ -67,16 +108,21 @@ if __name__ == "__main__":
    now_date = time.strftime("%Y/%m/%d", time.localtime())
    now_date = time.strftime("%Y/%m/%d", time.localtime())
    nan_commit_sha = "NAN.SHA"
    nan_commit_sha = "NAN.SHA"
    parser = argparse.ArgumentParser()
    parser = argparse.ArgumentParser()
    parser.add_argument("--version", "-v", default=current_version,
    parser.add_argument(
                        help='release version')
        "--version", "-v", default=current_version, help="release version"
    parser.add_argument("--sha", "-s", default=nan_commit_sha,
    )
                        help='git commit hash')
    parser.add_argument("--sha", "-s", default=nan_commit_sha, help="git commit hash")
    parser.add_argument("--date", "-d", default=now_date,
    parser.add_argument("--date", "-d", default=now_date, help="release date")
                        help='release date')
    parser.add_argument(
    parser.add_argument("--level", "-l", choices=['major', 'minor', 'patch'], default='patch',
        "--level",
                        help='specify the version level to bump (major, minor, or patch)')
        "-l",
    parser.add_argument("--dev", action='store_true',
        choices=["major", "minor", "patch"],
                        help='change the meta-data for develop build')
        default="patch",
        help="specify the version level to bump (major, minor, or patch)",
    )
    parser.add_argument(
        "--dev", action="store_true", help="change the meta-data for develop build"
    )
    args = parser.parse_args()
    args = parser.parse_args()


    if args.dev:
    if args.dev:
@@ -85,3 +131,4 @@ if __name__ == "__main__":
    else:
    else:
        new_version = bump_version(args.version, args.level)
        new_version = bump_version(args.version, args.level)
        change_meta(new_version, args.date)
        change_meta(new_version, args.date)
        update_changelog(current_version, new_version, args.date)