diff --git a/build_helper/prepare.py b/build_helper/prepare.py index 1d573f48..88e059c7 100644 --- a/build_helper/prepare.py +++ b/build_helper/prepare.py @@ -6,6 +6,7 @@ import re import shutil import tarfile +from datetime import datetime, timedelta, timezone from multiprocessing.pool import Pool from typing import TYPE_CHECKING, Any @@ -16,7 +17,7 @@ from .utils.network import dl2, get_gh_repo_last_releases, request_get, wait_dl_tasks from .utils.openwrt import OpenWrt from .utils.paths import paths -from .utils.repo import compiler +from .utils.repo import compiler, user_repo, get_release_suffix from .utils.upload import uploader from .utils.utils import parse_config @@ -415,12 +416,21 @@ def prepare_cfg(config: dict[str, Any], else: f.write(line + "\n") + config["target"], config["subtarget"] = openwrt.get_target() + + with open(os.path.join(openwrt.files, "etc", "openwrt-k_info"), "w", encoding="utf-8") as f: + content = "" + content += f'COMPILE_START_TIME="{datetime.now(timezone(timedelta(hours=8))).strftime('%y.%m.%d-%H')}"\n' + content += f'COMPILER="{compiler}"\n' + content += f'REPOSITORY_URL="https://github.com/{user_repo}"\n' + content += f'TAG_SUFFIX="{get_release_suffix(config)[1]}"\n' + f.write(content) + logger.debug("openwrt-k_info: %s", content) + logger.info("%s生成源代码归档", cfg_name) shutil.rmtree(os.path.join(openwrt.path, ".git")) os.makedirs(os.path.join(paths.uploads, cfg_name), exist_ok=True) tar_path = os.path.join(paths.uploads, cfg_name, "openwrt-source.tar.gz") openwrt.archive(tar_path) - config["target"], config["subtarget"] = openwrt.get_target() - return cfg_name, config, tar_path diff --git a/build_helper/utils/openwrt.py b/build_helper/utils/openwrt.py index 4e8321b9..ec12cbe9 100644 --- a/build_helper/utils/openwrt.py +++ b/build_helper/utils/openwrt.py @@ -18,6 +18,7 @@ class OpenWrtBase: def __init__(self, path: str) -> None: self.path = path + self.files = os.path.join(path, 'files') def get_arch(self) -> tuple[str | None, str | None]: arch = None diff --git a/build_helper/utils/repo.py b/build_helper/utils/repo.py index ef28f915..b4dc341f 100644 --- a/build_helper/utils/repo.py +++ b/build_helper/utils/repo.py @@ -62,9 +62,13 @@ def del_cache(key_prefix: str) -> None: else: logger.error('Failed to get caches list') -def new_release(cfg: dict, assets: list[str], body: str) -> None: +def get_release_suffix(cfg: dict) -> tuple[str, str]: release_suffix = f"({cfg["target"]}-{cfg["subtarget"]})-[{cfg["compile"]["openwrt_tag/branch"]}]" tag_suffix = f"({cfg["target"]}-{cfg["subtarget"]})-({cfg["compile"]["openwrt_tag/branch"]})-{cfg["name"]}" + return release_suffix, tag_suffix + +def new_release(cfg: dict, assets: list[str], body: str) -> None: + release_suffix, tag_suffix = get_release_suffix(cfg) f_release_name = "v" + datetime.now(timezone(timedelta(hours=8))).strftime('%Y.%m.%d') + "-{n}" + release_suffix f_tag_name = "v" + datetime.now(timezone(timedelta(hours=8))).strftime('%Y.%m.%d') + "-{n}" + tag_suffix @@ -106,7 +110,7 @@ def new_release(cfg: dict, assets: list[str], body: str) -> None: def match_releases(cfg: dict) -> github.GitRelease.GitRelease | None: - suffix = f"({cfg["target"]}-{cfg["subtarget"]})-({cfg["compile"]["openwrt_tag/branch"]})-{cfg["name"]}" + _, suffix = get_release_suffix(cfg) releases = repo.get_releases() diff --git a/files/usr/share/cmzj/openwrt-k_tool.sh b/files/usr/share/cmzj/openwrt-k_tool.sh index e4ffd757..8fe7c971 100755 --- a/files/usr/share/cmzj/openwrt-k_tool.sh +++ b/files/usr/share/cmzj/openwrt-k_tool.sh @@ -1,5 +1,5 @@ #!/bin/sh -# Copyright (C) 2023 沉默の金 +# Copyright (C) 2023-2024 沉默の金 trap 'rm -rf "$TMPDIR"' EXIT TMPDIR=$(mktemp -d) || exit 1 @@ -59,8 +59,6 @@ show_info() { echo "固件编译者:"$(sed -n "/^COMPILER=\"/p" /etc/openwrt-k_info | sed -e "s/COMPILER=\"//g" -e "s/\"//g" ) echo "固件编译仓库地址:"$(sed -n "/REPOSITORY_URL=\"/p" /etc/openwrt-k_info | sed -e "s/REPOSITORY_URL=\"//g" -e "s/\"//g" ) echo "固件编译时间:UTC+8 "$(sed -n "/^COMPILE_START_TIME=\"/p" /etc/openwrt-k_info | sed -e "s/COMPILE_START_TIME=\"//g" -e "s/\"/时/g" -e "s/-/日/" -e "s/\./月/g" -e "s/月/年/" ) - echo "固件发布名称:"$(sed -n "/^RELEASE_NAME=\"/p" /etc/openwrt-k_info | sed -e "s/RELEASE_NAME=\"//g" -e "s/\"//g" ) - echo "固件标签名称:"$(sed -n "/RELEASE_TAG_NAME=\"/p" /etc/openwrt-k_info | sed -e "s/RELEASE_TAG_NAME=\"//g" -e "s/\"//g" ) elif [ "$(grep -c "Compiled by" /etc/openwrt_release)" -ne '0' ];then echo "固件编译者:"$(sed -n "/Compiled by /p" /etc/openwrt_release|sed -e "s/.*Compiled by //g" -e "s/'//g" ) fi @@ -188,15 +186,14 @@ update_package() { elif [ "$(grep -c "^REPOSITORY_URL=" /etc/openwrt-k_info)" -eq '0' ]; then echo "错误:未知的固件编译仓库地址" exit 1 - elif [ "$(grep -c "^RELEASE_TAG_NAME=" /etc/openwrt-k_info)" -eq '0' ]; then - echo "错误:未知的固件标签名称" + elif [ "$(grep -c "^TAG_SUFFIX=" /etc/openwrt-k_info)" -eq '0' ]; then + echo "错误:未知的固件标签前缀" exit 1 fi REPOSITORY_URL=$(sed -n "/REPOSITORY_URL=\"/p" /etc/openwrt-k_info | sed -e "s/REPOSITORY_URL=\"//g" -e "s/\"//g") REPOSITORY=$(echo $REPOSITORY_URL|sed -e "s/https:\/\/github.com\///") - RELEASE_TAG_NAME=$(sed -n "/RELEASE_TAG_NAME=\"/p" /etc/openwrt-k_info | sed -e "s/RELEASE_TAG_NAME=\"//g" -e "s/\"//g") - RELEASE_TAG_MAINNAME=$(echo $RELEASE_TAG_NAME | sed "s/v[0-9]\{1,2\}.[0-9]\{2,2\}.[0-9]\{1,2\}-[0-9]\{2,2\}(/(/") - latest_ver="$(curl -s https://api.github.com/repos/$REPOSITORY/releases 2>/dev/null | grep -E 'tag_name' | grep "$RELEASE_TAG_MAINNAME" | sed -e 's/ "tag_name": "//' -e 's/",//' | sed -n '1p')" + TAG_SUFFIX=$(sed -n "/TAG_SUFFIX=\"/p" /etc/openwrt-k_info | sed -e "s/TAG_SUFFIX=\"//g" -e "s/\"//g") + latest_ver="$(curl -s https://api.github.com/repos/$REPOSITORY/releases 2>/dev/null | grep -E 'tag_name' | grep "$TAG_SUFFIX" | sed -e 's/ "tag_name": "//' -e 's/",//' | sed -n '1p')" FILE_NAME=$(curl -s "https://api.github.com/repos/$REPOSITORY/releases/tags/$latest_ver"| grep -E 'name'| grep -E '\.manifest'| sed -e 's/ "name": "//' -e 's/",//' | sed -n '1p') curl -L --retry 3 --connect-timeout 20 $REPOSITORY_URL/releases/download/${latest_ver}/$FILE_NAME -o package.list || download_failed if ! diff installed.list package.list -y -W 80 -B -b | grep '|' |sed -e "s/^/update:/g" -e 's/|/>/g' -e "/kmod-/d" > update.list; then @@ -247,7 +244,7 @@ update_package() { ;; esac [[ -d /usr/share/cmzj/download/ ]] && rm -rf /usr/share/cmzj/download/* - curl -L --retry 3 --connect-timeout 20 $REPOSITORY_URL/releases/download/${latest_ver}/package.zip -o $DOWNLOAD_PATH || download_failed + curl -L --retry 3 --connect-timeout 20 $REPOSITORY_URL/releases/download/${latest_ver}/packages.zip -o $DOWNLOAD_PATH || download_failed unzip -l $DOWNLOAD_PATH |grep "-"|grep ":"|grep " "|sed "s/.*[0-9][0-9]-[0-9]\{1,2\}-[0-9]\{1,5\} [0-9]\{1,2\}:[0-9]\{1,2\} //g"|sed "s/ //g" > newpackage.list unzip $DOWNLOAD_PATH $(grep "$(cat update_package.list)" newpackage.list | sed ':label;N;s/\n/ /;b label') -d $TMPDIR/update/package rm -rf /usr/share/cmzj/download $TMPDIR/update/package/download @@ -292,7 +289,6 @@ usage() { echo "" echo "Commands:" echo "update 更新包/规则/本工具" - #echo "update 更新包/规则/固件/本工具" echo "info 打印固件信息" } main \ No newline at end of file