pikapython/port/linux/release_helper.py

150 lines
5.5 KiB
Python
Raw Normal View History

2023-01-15 09:05:41 +00:00
import os
import toml
REPO_PATH = "../.."
PACKAGE_PATH = REPO_PATH + "/package"
2023-03-15 16:20:59 +08:00
LINUX_PACKAGE_PATH = REPO_PATH + "/port/linux/package/pikascript/pikascript-lib"
2023-01-15 09:06:15 +00:00
PACKAGE_RELEASE_PATH = REPO_PATH + "/packages.toml"
2023-01-15 09:05:41 +00:00
WORK_DIR = os.getcwd()
class VersoinType:
MAJOR = 1
MINOR = 2
PATCH = 3
class VersionInfo:
version: str
commit: str
vmajor: int
vminor: int
vpatch: int
def __init__(self, version_discription: str):
# v1.0.0
try:
self.version = version_discription.split(" ")[0]
self.commit = version_discription.split(" ")[1]
self.vmajor = int(self.version.split(".")[0][1:])
self.vminor = int(self.version.split(".")[1])
self.vpatch = int(self.version.split(".")[2])
except:
raise ValueError("Invalid version discription")
class PackageRelease:
name: str
versions: list[VersionInfo]
def __init__(self, pkg_dict: dict, name: str):
self.name = name
self.versions = []
for package in pkg_dict:
if package['name'] == name:
for version_dicription in package['releases']:
try:
self.versions.append(VersionInfo(version_dicription))
except:
continue
2023-01-29 12:51:00 +08:00
def latestVersion(self):
# find the latest version
latest_version = self.versions[0]
for version in self.versions:
if version.vmajor > latest_version.vmajor:
latest_version = version
elif version.vmajor == latest_version.vmajor:
if version.vminor > latest_version.vminor:
latest_version = version
elif version.vminor == latest_version.vminor:
if version.vpatch > latest_version.vpatch:
latest_version = version
return latest_version
2023-01-15 09:05:41 +00:00
class PackageReleaseList:
pkg_dict: dict
packages: list[PackageRelease]
def __init__(self, file_path):
# read releases.toml
with open(file_path, "r") as f:
self.pkg_dict = toml.load(f)
self.packages = []
for package in self.pkg_dict['packages']:
self.packages.append(PackageRelease(
self.pkg_dict['packages'], package['name']))
2023-01-29 12:51:00 +08:00
2023-01-15 09:05:41 +00:00
def latestCommit(self, package_name: str):
# find the package
for package in self.packages:
if package.name == package_name:
# find the latest version
latest_version = package.versions[0]
for version in package.versions:
if version.vmajor > latest_version.vmajor:
latest_version = version
elif version.vmajor == latest_version.vmajor:
if version.vminor > latest_version.vminor:
latest_version = version
elif version.vminor == latest_version.vminor:
if version.vpatch > latest_version.vpatch:
latest_version = version
return latest_version.commit
def versionRelease(self, package_name: str, version_type: VersoinType, commit: str):
# find the package
for package in self.packages:
if package.name == package_name:
# find the latest version
latest_version = package.versions[0]
for version in package.versions:
if version.vmajor > latest_version.vmajor:
latest_version = version
elif version.vmajor == latest_version.vmajor:
if version.vminor > latest_version.vminor:
latest_version = version
elif version.vminor == latest_version.vminor:
if version.vpatch > latest_version.vpatch:
latest_version = version
# release new version
if version_type == VersoinType.MAJOR:
latest_version.vmajor += 1
latest_version.vminor = 0
latest_version.vpatch = 0
elif version_type == VersoinType.MINOR:
latest_version.vminor += 1
latest_version.vpatch = 0
elif version_type == VersoinType.PATCH:
latest_version.vpatch += 1
# solve version overflow
if latest_version.vpatch > 9:
latest_version.vpatch = 0
latest_version.vminor += 1
if latest_version.vminor > 9:
latest_version.vminor = 0
latest_version.vmajor += 1
new_version_str = f"v{latest_version.vmajor}.{latest_version.vminor}.{latest_version.vpatch}"
# add new version to the package
for package in self.pkg_dict['packages']:
if package['name'] == package_name:
package['releases'].append(
f"{new_version_str} {commit}")
return new_version_str
def dump(self, file_path):
with open(file_path, "w") as f:
# dump with formating
toml.dump(self.pkg_dict, f)
2023-01-29 12:53:50 +08:00
def findPackage(self, pkg_name:str):
for package in self.packages:
if package.name == pkg_name:
return package
return None