SHARE
TWEET

c3mm.py

a guest Oct 1st, 2016 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf8
  2. # Cossacks Mod manager version 1.2.0
  3. # Require python 3.5 to work: https://python.org/dowloads
  4. import sys, os
  5.  
  6. MOD_PATH = "mod/"
  7. CONFIG_PATH = "c3mm/"
  8. COPY_PATH = CONFIG_PATH + "copy/"
  9. BCKUP_PATH = CONFIG_PATH + "bckup/"
  10.  
  11. INI_PATH = CONFIG_PATH + "c3mm.ini"
  12. FILE_PATH = CONFIG_PATH + "file.ini"
  13.  
  14. ldir = [MOD_PATH, CONFIG_PATH, COPY_PATH, BCKUP_PATH]
  15.  
  16. for dir in ldir:
  17.     try:
  18.         os.mkdir(dir)
  19.     except:
  20.         pass
  21.  
  22. def olderversionthan(a, b):
  23.     a = a.split('.')
  24.     b = b.split('.')
  25.     while len(a) > len(b):
  26.         b.append('0')
  27.     while len(a) < len(b):
  28.         a.append('0')
  29.     for _a, _b in zip(a, b):
  30.         if _a < _b:
  31.             return True
  32.         elif _a > _b:
  33.             return False
  34.     return False
  35.  
  36. def loadmodlist():
  37.     try:
  38.         open(INI_PATH, "r")
  39.     except:
  40.         open(INI_PATH, "w").close()
  41.     data = {}
  42.     for name, version in [line.split(" version ") for line in open(INI_PATH, "r").read().split("\n") if line != ""]:
  43.         data[name] = version
  44.     return data
  45.  
  46. def savemodlist(modlist):
  47.     data = ""
  48.     for key in modlist:
  49.         data += key + " version " + modlist[key] + "\n"
  50.  
  51.     open(INI_PATH, "w").write(data)
  52.  
  53. def loadfilelist():
  54.     try:
  55.         open(FILE_PATH, "r")
  56.     except:
  57.         open(FILE_PATH, "w").close()
  58.  
  59.     data = {}
  60.     for line in open(FILE_PATH).read().split('\n'):
  61.         info = line.split(',')
  62.         data[info[0]] = info[1:]
  63.     return data
  64.  
  65. def savefilelist(filelist):
  66.     data = ""
  67.     for file in filelist:
  68.         if file == '' or filelist[file] == []:
  69.             continue
  70.         data += file
  71.         for mod in filelist[file]:
  72.             data += "," + mod
  73.         data += '\n'
  74.     open(FILE_PATH, "w").write(data)
  75.  
  76. def main(argv):
  77.  
  78.     modlist = loadmodlist()
  79.     filelist = loadfilelist()
  80.  
  81.     bad = checkMod(filelist)
  82.     for mod in bad:
  83.         print("Uninstall", mod, "version", modlist[mod])
  84.         uninstall({'name':mod}, modlist[mod], filelist, modlist)
  85.  
  86.     if len (argv) == 1:
  87.         printhelp()
  88.         return
  89.  
  90.     if argv[1] == "install":
  91.         if len(argv) < 2:
  92.             mod = input("Please type name of mod file: ")
  93.         else:
  94.             mod = argv[2]
  95.  
  96.         mod = MOD_PATH + mod
  97.         mod = loadmoddata(mod)
  98.         if not mod["name"] in modlist:
  99.             modlist[mod["name"]] = mod["version"]
  100.         elif modlist[mod["name"]] == mod["version"]:
  101.             print(mod["name"], "version", mod["version"], "already installed!")
  102.             exit()
  103.         elif olderversionthan(modlist[mod["name"]], mod["version"]):
  104.             print("Unistall", mod["name"], "version", modlist[mod["name"]])
  105.             uninstall(mod, modlist[mod["name"]], filelist) 
  106.             modlist[mod["name"]] = mod["version"]
  107.         elif olderversionthan(mod["version"], modlist[mod["name"]]) :
  108.             print(mod["name"], "version", modlist[mod["name"]], " is already installed. Are you sure to install an older version?", "(" + mod["version"] + ")")
  109.             choice = input("yes/no:")
  110.             if choice == "yes":
  111.                 print("Unistall", mod["name"], "version", modlist[mod["name"]])
  112.                 uninstall(mod, modlist[mod["name"]], filelist) 
  113.                 modlist[mod["name"]] = mod["version"]
  114.             else:
  115.                 return
  116.  
  117.         print("Install", mod["name"], "version", mod["version"])
  118.         install(mod, filelist)
  119.  
  120.         mkuninstall(mod)
  121.  
  122.     elif argv[1] == "uninstall":
  123.         if len(argv) < 2:
  124.             mod = input("Please type name of mod file: ")
  125.         else:
  126.             mod = argv[2]
  127.  
  128.         mod = MOD_PATH + mod
  129.         mod = loadmoddata(mod)
  130.  
  131.         if not mod["name"] in modlist or modlist[mod["name"]] != mod["version"]:
  132.             print(mod["name"], "version", mod["version"], "doesn't exist, impossible to uninstall it.")
  133.             exit()
  134.  
  135.         print("Unistall", mod["name"], "version", modlist[mod["name"]])
  136.         uninstall(mod, modlist[mod["name"]], filelist, modlist)
  137.  
  138.     elif argv[1] == "help":
  139.         printhelp ()
  140.  
  141.     else:
  142.         print("Unknow command", argv[1])
  143.         printhelp()
  144.  
  145.     savemodlist(modlist)
  146.     savefilelist(filelist)
  147.  
  148. def checkMod(fileData):#dic of (file:list of mod)
  149.     bad = []
  150.     for file in fileData:
  151.         if file == '':
  152.             continue
  153.         if open(file, 'r').read() != open(COPY_PATH + file, 'r').read():
  154.             print("File", file, "has been modify.")
  155.             for mod in fileData[file]:
  156.                 if not mod in bad:
  157.                     print(mod, "will be uninstall")
  158.                     bad.append(mod)
  159.     return bad
  160.  
  161. def install(mod, filelist, uninstall=False):
  162.     for file in mod["files"]:
  163.         if not uninstall:
  164.             if not file in filelist:
  165.                 filelist[file] = []
  166.             if not mod['name'] in filelist[file]:
  167.                 filelist[file].append(mod['name'])
  168.         if not apply(mod["files"][file], file):
  169.             print("Fatal error")
  170.             return
  171.         else:
  172.             print("Patching of", file,  "succed!")
  173.             copy(file)
  174.  
  175. def uninstall(mod, oldversion, filelist, modlist):
  176.     _uninstall = loadmoddata(CONFIG_PATH + mod["name"] + " " + oldversion + " uninstall")
  177.     for file in filelist:
  178.         if mod["name"]in filelist[file]:
  179.             filelist[file].remove(mod["name"])
  180.     install(_uninstall, filelist, True)
  181.     del modlist[mod["name"]]
  182.  
  183. def copy(path):
  184.     mkpath(COPY_PATH + path)
  185.     open(COPY_PATH + path, "w").write(open(path, "r").read())
  186.  
  187. def backup(path):
  188.     mkpath(BCKUP_PATH + path)
  189.     try:
  190.         try:
  191.             open(BCKUP_PATH + path, "r")
  192.             print("A backup of", path, "already exist")
  193.         except:
  194.             open(BCKUP_PATH + path, "w").write(open(path, "r").read())
  195.             print("Backup of", path, "suceed!")
  196.         return True
  197.     except:
  198.         print("Impossible to write in", path)
  199.         return False
  200.  
  201. def apply(instruction, path):
  202.  
  203.     if not backup (path):
  204.         return False
  205.     try:
  206.         txt = open(path, "r").read()
  207.     except:
  208.         print("File", path, "don't exist or you havn't access to it.")
  209.         return
  210.  
  211.     for old, new in instruction:
  212.         txt = txt.replace(old, new)
  213.  
  214.     try:
  215.         open(path, "w").write(txt)
  216.     except:
  217.         print("Impossible to write in", path)
  218.         return False
  219.  
  220.     return True
  221.  
  222. def printhelp():
  223.     print("Cossacks 3 (text)Mod manager version 1.2.0\npython c3mm.py [instruction] [options]\n\tInstruction:\n\tinstall + path : install a cossacks3 mod file.\n\tuninstall + path : uninstall a cossacks3 mod file")
  224.  
  225. def loadmoddata(path):
  226.     info, mod = open(path, "r").read().split('\n', 1)
  227.     name, version = info.split(" version ")
  228.  
  229.     data = {}
  230.     data["name"] = name
  231.     data["version"] = version
  232.  
  233.     data["files"] = {}
  234.     for file in mod.split("*PATH*"):
  235.         if file == "":
  236.             continue
  237.         path, instruction = file.split("\n", 1)
  238.         instruction = [(old, new) for old, new in [line.split("*REPLACE*") for line in instruction.split("*END*") if line != ""]]
  239.         data["files"][path] = instruction
  240.  
  241.     return data
  242.  
  243. def mkuninstall(mod):
  244.     data = ""
  245.     data += mod["name"] + " version " + mod["version"] + " cleaner\n"
  246.     for file in mod["files"]:
  247.         data += "*PATH*" + file + "\n"
  248.         for old, new in mod["files"][file]:
  249.             data += new + "*REPLACE*" + old + "*END*"
  250.     open(CONFIG_PATH + mod["name"] + " " + mod["version"] + " uninstall", "w").write(data)
  251.  
  252. def mkpath(path):
  253.     rpath = ""
  254.     for dir in path.split("/")[:-1]:
  255.         rpath += dir + '/'
  256.         try:
  257.             os.mkdir(rpath)
  258.         except:
  259.             pass
  260. if __name__ == "__main__":
  261.     main(sys.argv)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top