forked from pikasTech/PikaPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease_helper.py
More file actions
149 lines (128 loc) · 5.46 KB
/
Copy pathrelease_helper.py
File metadata and controls
149 lines (128 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import toml
REPO_PATH = "../.."
PACKAGE_PATH = REPO_PATH + "/package"
LINUX_PACKAGE_PATH = REPO_PATH + "/port/linux/package/pikascript/pikascript-lib"
PACKAGE_RELEASE_PATH = REPO_PATH + "/packages.toml"
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
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
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']))
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)
def findPackage(self, pkg_name:str):
for package in self.packages:
if package.name == pkg_name:
return package
return None