-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
management.py
186 lines (161 loc) · 5.65 KB
/
management.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import yaml, toml
import os, sys, platform
import Levenshtein as le
from pexpect.popen_spawn import PopenSpawn as ps
import pexpect as px
import re
class Pack:
match platform.system():
case "Windows": _extension = ".exe"
case "Linux": _extension = ""
case _: raise OSError("Unsupported platform")
def __init__(self, source,flavors):
"""
Args:
source: path to source file (usually pack.toml)
flavors: path to flavor file when using unsup (usually unsup.toml)
"""
self.source = source
self.flavors = flavors
def getInfo(self): return toml.load(self.source)
def getModlist(self):
modlist = {modname.replace(".pw.toml", "")
for modname in os.listdir("./mods")}
return modlist
def updateFlavors(self):
modlist = self.getModlist()
flavor_groups, flavors = open("pack-config/flavorgroups.yaml", "r"), open("pack-config/flavors.yaml")
fl = yaml.safe_load(flavors)
flavorlist = fl if fl is not None else {}
flls = yaml.safe_load(flavor_groups)
flavorgrouplist = flls if flls is not None else {}
modflavors:dict[str, str] = {modname: "misc_on" for modname in modlist} | flavorlist
data = flavorgrouplist | {"metafile": {k: {"flavors": v} for k, v in flavorlist.items()}}
new:set = {(mm if mm not in flavorlist.keys() else ...)for mm in modlist};new.discard(...)
newly_added:set = set() if new is None else new
old:set = {(mod if mod not in modlist else ...)for mod in modflavors.keys()};old.discard(...)
outdated:set = set() if old is None else old
if len(outdated) > 0:
print("These mods are being removed!!")
for mod in outdated: print("- ", mod)
for mod in outdated:
for m in modlist:
if le.ratio(mod,m)>=0.85:
print(mod,"might be related to",m);
print('%s: %s' % (mod, modflavors.get(mod)))
modflavors.pop(mod)
if len(newly_added) > 0:
print("These mods are being added!!")
for mod in newly_added: print("- ", mod)
with open(self.flavors, "w") as f, open("pack-config/flavors.yaml", "w") as flavorfile:
yaml.dump(modflavors, flavorfile)
toml.dump(data, f)
def updateCustom(self):
print("Updating custom sources...")
SuccessfulUpdates=[]
FailedUpdates=[]
with open("pack-config/custom_sources.yaml") as sources:
custom_sources = yaml.safe_load(sources); sources.close()
for mod, mod_url in custom_sources.items():
customUpdater = ps(
'bin/packwiz%s url add "%s" %s' % (self._extension, mod, mod_url),
encoding='utf-8',
)
customUpdater.expect(px.EOF)
log=customUpdater.before
status = re.match("(?:Success)|(?:Fail)",log).group(0)# type: ignore
if status == "Success":
localPath=re.search("\(.*\)",log).group(0) # type: ignore
SuccessfulUpdates.append([mod,localPath,mod_url])
elif status == "Fail": FailedUpdates.append(mod)
else: print('\n---------------------------\n',log,'\n---------------------------\n')
if len(FailedUpdates) <= 0: pass
else:
print("Failed updating:")
for mod in FailedUpdates: print("- "+mod)
if len(SuccessfulUpdates) <= 0:pass
else:
print('Succesfully updated:')
for mod,path,url in SuccessfulUpdates: print("- %s %s" %(mod,path))
# os.system('bin/packwiz%s url add "%s" %s' % (self._extension, mod, mod_url))
def addMod(self,source:str=...,mod:str=..., ):
"""
Mod: name or URL of the mod
Source: name of the source.
* cf for curseforge
* mr for modrinth
"""
if source is ...: source = input("Source site [mr/cf]: ")
if mod is ...: mod = input("Mod name/URL: ")
packwiz = ps(
"bin/packwiz%s %s add %s" % (self._extension, source, mod),
encoding='utf-8',
)
i = packwiz.expect(["Choose a number", px.EOF])
print(packwiz.before)
if i == 0:
packwiz.sendline(input("Choose a number: "))
print(packwiz.readlines()[-1])
def update(self):
p = re.compile(r'A supported update system for ".*" cannot be found\.\n')
updater = ps(
"bin/packwiz%s update --all" % (self._extension),
encoding='utf-8',
)
i = updater.expect([r"Do you want to update\?", px.EOF])
print(p.sub("",updater.before)) # type: ignore
if i == 0:
userinput = input("Update? (y/n): ")
updater.sendline(userinput)
print(updater.readlines()[-1])
def refresh(self):
self.updateFlavors()
os.system("bin/packwiz%s refresh" % self._extension)
def export(self):
pattern=re.compile(
"(Disclaimer:.*\n)"
+"|(Note that mods bundled.*\n)"
+"|(packwiz is currently unable to match metadata.*\n)",
re.M
)
for file in [
self.getInfo()['name']+'-'+self.getInfo()['version']+side+'.zip'
for side in ['[C]','[S]','']
]:
try:
os.remove(file)
print('removing file: '+file)
except:pass
print()
for side in ['[C]','[S]']:
print("------------------------------------------")
print()
print('Exporting','server' if side == '[S]' else 'client','side pack...')
exporter = ps(
"bin/packwiz%s cf export -s %s" % (
self._extension,
'server' if side == '[S]' else 'client',
),
encoding='utf-8'
)
i=exporter.expect(["Error","failed",px.EOF])
print(pattern.sub('',exporter.before)) # type: ignore
if i in [0,1]: pass
elif i == 2:
print("renaming...")
os.rename(
self.getInfo()['name']+'-'+self.getInfo()['version']+".zip",
self.getInfo()['name']+'-'+self.getInfo()['version']+side+'.zip'
)
print('-',self.getInfo()['name']+'-'+self.getInfo()['version']+side+'.zip','created!\n')
print()
pack = Pack("pack.toml","unsup.toml")
def updateAll():
print()
pack.update()
print()
pack.updateCustom()
print()
pack.updateFlavors()
print()
pack.refresh()