Browse Source

upgrade sm/mm

master
gsd 6 months ago
parent
commit
82bd2a39f4
  1. 200
      SourceManager.py

200
SourceManager.py

@ -2,7 +2,8 @@
VERSION = 1.0
#SYS IMPORTS
from genericpath import isfile
from genericpath import exists, isfile
from logging import root
import sys
import os
import types
@ -438,9 +439,6 @@ class Server:
create_symbolic_file(other_vpk_file, server_vpk_file)
return True
def upgrade_metamod(self):
pass
def add_plugin(self, plugin_path, need_reloads_plugins = True):
plugins_list = glob(f"{self.root}/addons/sourcemod/plugins/*.smx") + glob(f"{self.root}/addons/sourcemod/plugins/*/*.smx")
plugin_file = plugin_path.split("/")[-1]
@ -485,6 +483,194 @@ class Server:
remove_file(map_path)
return True
def upgrade_metamod(self, metamod_arch):
from tarfile import open
from time import time
import os
backup_directory = os.path.join(self.root, "addons_backup")
if not os.path.exists(backup_directory):
print("Backup dir not created, create: ", backup_directory)
os.mkdir(backup_directory)
class File:
def __init__(self, root, directory, file, new_root = None) -> None:
self.root = root
self.directory = directory
self.file = file
self.new_root = new_root
def __str__(self) -> str:
return str(self.internalPath)
def __repr__(self) -> str:
return self.__str__()
@property
def fullPath(self):
#self.directory.replace(self.root, "." if self.new_root == None else self.new_root)
return os.path.join(self.directory if self.new_root == None else self.directory.replace(self.root, "." if self.new_root == None else self.new_root), self.file)
@property
def internalPath(self):
return os.path.join(self.directory.replace(self.root, "."), self.file)
def copyPath(self, root):########TODO
return File(root, self.directory, self.file)
class UpgradedFile:
config_exts = [".ini", ".cfg"]
ignore_files = ["basevotes.smx", "admin-flatfile.smx"]
def __init__(self, new, old, equals_path = True) -> None:
self.new : File = new
self.old : File|None = old
self.equals_path = equals_path
@property
def isUpgrade(self):
return self.old != None
@property
def isConfig(self):
return "."+os.path.split(self.new.fullPath)[-1].split(".")[-1] in self.config_exts
@property
def isIgnore(self):
return os.path.split(self.new.fullPath)[-1] in self.ignore_files
def __str__(self):
return f"[{'~' if self.isUpgrade else '+'}] [{'P' if self.equals_path else 'F'}] {self.new.fullPath} -> {self.old.fullPath if self.isUpgrade else 'new'}"
def __repr__(self) -> str:
return self.__str__()
def Process(self, game_root, check = True):
source = self.new.fullPath
dest = self.old.fullPath if self.old != None else File(self.new.root, self.new.directory, self.new.file, game_root).fullPath
if os.path.exists(dest):
print("[-]", dest)
if not check:
os.remove(dest)
print("[+]", source, "->", dest)
dir_check = "/".join(os.path.split(dest)[:-1])
if not os.path.exists(dir_check):
print("[!] dest directory is not exist, create: ", dir_check)
os.makedirs(dir_check, exist_ok = True)
if not check:
shutil.copy2(source, dest)
print()
#extract sourcemod/metamod
arch_directory = f"/tmp/mm.arch.{time()}"
with open(metamod_arch) as arch:
os.mkdir(arch_directory)
arch.extractall(arch_directory)
#new files
new_filelist = []
for (dirpath, dirnames, filenames) in os.walk(arch_directory):
#print(dirpath, dirnames, filenames)
for file in filenames:
new_filelist.append(File(arch_directory, dirpath, file))
print(new_filelist[0])
#old files
old_filelist = []
for (dirpath, dirnames, filenames) in os.walk(os.path.join(self.root, "addons")):
#print(dirpath, dirnames, filenames)
for file in filenames:
old_filelist.append(File(self.root, dirpath, file))
print(old_filelist[0])
class FilesToUpgrade:
def __init__(self):
self.files_to_upgrade = []
def append(self, el):
self.files_to_upgrade.append(el)
def Upgraded(self):
return [f for f in self.files_to_upgrade if f.isUpgrade and not f.isConfig and not f.isIgnore]
def Appended(self):
return [f for f in self.files_to_upgrade if not f.isUpgrade and not f.isConfig and not f.isIgnore]
def Configs(self):
return [f for f in self.files_to_upgrade if f.isConfig]
def PrintListing(self):
print("Files to upgrade")
for f in self.Upgraded():
print(f)
print("Files to append")
for f in self.Appended():
print(f)
print("Updated config files")
for f in self.Configs():
print(f)
def BackupUpgradedFiles(self, path = f"/tmp/smmmbackup.{time()}.zip"):
print("Backup files to:", path)
import zipfile
zf = zipfile.ZipFile(path, "w")
for f in self.Upgraded():
zf.write(f.old.fullPath)
zf.close()
def Process(self, gameroot, check = True):
print(f"Processing {'check to upgrade' if check else 'upgrade'}...")
for f in self.Upgraded():
f.Process(gameroot, check)
print(f"Processing {'check to append' if check else 'append'}...")
for f in self.Appended():
f.Process(gameroot, check)
print("Config files dont touched!")
#collect
print("")
i = 0
files_to_upgrade = FilesToUpgrade()
for nf in new_filelist:
print(f"[{i}/{len(new_filelist)}] search...", end="\r")
founded = False
for of in old_filelist:
if nf.internalPath == of.internalPath:
files_to_upgrade.append(UpgradedFile(nf, of))
founded = True
break
if not founded:
for of in old_filelist:
if os.path.split(nf.internalPath)[-1] == os.path.split(of.internalPath)[-1]:
if os.path.split(nf.internalPath)[0][-2:] != "64":
#print(os.path.split(nf.internalPath), os.path.split(of.internalPath))
files_to_upgrade.append(UpgradedFile(nf, of, False))
founded = True
break
if not founded:
files_to_upgrade.append(UpgradedFile(nf, None))
i+=1
#show file list
files_to_upgrade.PrintListing()
files_to_upgrade.Process(self.root, check=True)
if self.wait_input():
files_to_upgrade.BackupUpgradedFiles(os.path.join(backup_directory, f"{time()}.zip"))
files_to_upgrade.Process(self.root, check=False)
#remove unzip archive
from shutil import rmtree
rmtree(arch_directory)
def clear_download_cache(self):
cache_files = glob(f"{self.root}/download/user_custom/*/*.dat")
self.o.info(f"Delete {len(cache_files)} cache files?")
@ -959,6 +1145,8 @@ if __name__ == "__main__":
################################################################################################################################################
parser.add_argument("--UpdateTF2IDB", help = "Update tf2idb database", default = False, action = "store_true")
################################################################################################################################################
parser.add_argument("--UpgradeMetaMod", help = "Upgrade current version of metamod", type = str, default="")
################################################################################################################################################
args = parser.parse_args()
ALL_YES = 1 if args.yes else 0
##################################
@ -1067,3 +1255,7 @@ if __name__ == "__main__":
if args.DeleteLogsFiles:
manager.execute("clear_logs")
sys.exit(0)
###################################
if args.UpgradeMetaMod:
manager.execute("upgrade_metamod", args.UpgradeMetaMod)
sys.exit(0)

Loading…
Cancel
Save