-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbob_mod_builder.py
62 lines (49 loc) · 2.26 KB
/
bob_mod_builder.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
from typing import Optional
import os, shutil, sys, getopt
import json
class BobModBuilder:
def __init__(self, factorioFolderDir:Optional[str]=None):
self.modNames = [modName for modName in next(os.walk(f"{os.path.dirname(os.path.abspath(__file__))}"))[1] if self.__isReleased(modName)]
if factorioFolderDir is None:
self.modFolderDir = f"{os.getenv('APPDATA')}/Factorio/mods/"
else:
self.modFolderDir = f"{os.path.abspath(factorioFolderDir)}/mods/"
def __isReleased(self, modName:str) -> None:
if modName.find("bob") >= 0:
return True # all bobs mods on git are released
return False # not part of bobs
def __getModVersion(self, modName:str) -> str:
with open("{0}/info.json".format(modName)) as modDataFile:
modData = json.load(modDataFile)
return modData['version']
def __deleteAllVersions(self, modName:str, deleteZip:bool=True) -> None:
# deleting folders
folders = [folderName for folderName in next(os.walk(self.modFolderDir))[1] if folderName.find(modName) >= 0]
for folder in folders:
print(" Removing '{0}/'".format(folder))
shutil.rmtree(self.modFolderDir + folder)
# deleting zip folders
folders = [folderName for folderName in os.listdir(self.modFolderDir) if deleteZip and folderName.find(modName) >= 0 ]
for folder in folders:
print(" Removing '{0}'".format(folder))
os.remove(self.modFolderDir + folder)
def __createNewVersion(self, modName:str) -> None:
folder = "{0}_{1}/".format(modName, self.__getModVersion(modName))
print(" Creating '{0}'".format(folder))
src_dir = "./{0}".format(modName)
dst_dir = self.modFolderDir + folder
shutil.copytree(src_dir, dst_dir)
def createMod(self, modName:str) -> None:
print("Updating '{0}'".format(modName))
self.__deleteAllVersions(modName, True)
self.__createNewVersion(modName)
def createAllMods(self) -> None:
for modName in self.modNames:
self.createMod(modName)
if __name__ == "__main__":
factorioFolderDir = None
opts, args = getopt.getopt(sys.argv[1:], ":m:", ['factoriodir='])
for opt, arg in opts:
if opt in ('-m', '--factoriodir'):
factorioFolderDir = os.path.realpath(arg.strip())
BobModBuilder(factorioFolderDir=factorioFolderDir).createAllMods()