Guest User

c3mm.py

a guest
Oct 1st, 2016
171
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf8
  2. # Cossacks Mod manager version 1.1.1
  3. # Require python 3.5 to work: https://python.org/dowload
  4. import sys
  5.  
  6. def olderversionthan(a, b):
  7.     a = a.split('.')
  8.     b = b.split('.')
  9.  
  10.     for _a, _b in zip(a, b):
  11.         if _a < _b:
  12.             return True
  13.         elif _a > _b:
  14.             return False
  15.     return False
  16.  
  17. def loadmodlist():
  18.     data = {}
  19.     for name, version in [line.split(" version ") for line in open("c3mm.ini", "r").read().split("\n") if line != ""]:
  20.         data[name] = version
  21.     return data
  22.  
  23. def savemodlist(modlist):
  24.     data = ""
  25.     for key in modlist:
  26.         data += key + " version " + modlist[key] + "\n"
  27.  
  28.     open("c3mm.ini", "w").write(data)
  29.  
  30. def main(argv):
  31.  
  32.     modlist = loadmodlist()
  33.  
  34.     if len (argv) == 1:
  35.         printhelp()
  36.         return
  37.  
  38.     if argv[1] == "install":
  39.         if len(argv) < 2:
  40.             mod = input("Please type name of mod file: ")
  41.         else:
  42.             mod = argv[2]
  43.  
  44.         mod = loadmoddata(mod)
  45.         if not mod["name"] in modlist:
  46.             modlist[mod["name"]] = mod["version"]
  47.         elif modlist[mod["name"]] == mod["version"]:
  48.             print(mod["name"], "version", mod["version"], "already installed!")
  49.             exit()
  50.         elif olderversionthan(modlist[mod["name"]], mod["version"]):
  51.             print("Unistall", mod["name"], "version", modlist[mod["name"]])
  52.             uninstall(mod, modlist[mod["name"]])   
  53.             modlist[mod["name"]] = mod["version"]
  54.         elif olderversionthan(mod["version"], modlist[mod["name"]]) :
  55.             print(mod["name"], "version", modlist[mod["name"]], " is already installed. Are you sure to install an older version?", "(" + mod["version"] + ")")
  56.             choice = input("yes/no:")
  57.             if choice == "yes":
  58.                 print("Unistall", mod["name"], "version", modlist[mod["name"]])
  59.                 uninstall(mod, modlist[mod["name"]])   
  60.                 modlist[mod["name"]] = mod["version"]
  61.             else:
  62.                 return
  63.  
  64.         print("Install", mod["name"], "version", mod["version"])
  65.         install(mod)
  66.  
  67.         mkuninstall(mod)
  68.  
  69.     elif argv[1] == "uninstall":
  70.         if len(argv) < 2:
  71.             mod = input("Please type name of mod file: ")
  72.         else:
  73.             mod = argv[2]
  74.  
  75.         mod = loadmoddata(mod)
  76.  
  77.         if not mod["name"] in modlist or modlist[mod["name"]] == mod["version"]:
  78.             print(mod["name"], "version", mod["version"], "doesn't exist, impossible to uninstall it.")
  79.             exit()
  80.  
  81.         print("Unistall", mod["name"], "version", modlist[mod["name"]])
  82.         uninstall(mod, modlist[mod["name"]])
  83.  
  84.         del modlist[mod["name"]]
  85.  
  86.     elif argv[1] == "help":
  87.         printhelp ()
  88.  
  89.     else:
  90.         print("Unknow command", argv[1])
  91.         printhelp()
  92.  
  93.     savemodlist(modlist)
  94.  
  95. def install(mod):
  96.     for file in mod["files"]:
  97.         if not apply(mod["files"][file], file):
  98.             print("Fatal error")
  99.             return
  100.         else:
  101.             print("Patching of", file,  "succed!")
  102.  
  103. def uninstall(mod, oldversion):
  104.     unistall = loadmoddata(mod["name"] + " " + oldversion + " uninstall")
  105.     install(unistall)
  106.  
  107. def backup(path):
  108.     try:
  109.         try:
  110.             open(path + ".backup", "r")
  111.             print("A backup of", path, "already exist")
  112.         except:
  113.             open(path + ".backup", "w").write(open(path, "r").read())
  114.             print("Backup of", path, "suceed!")
  115.         return True
  116.     except:
  117.         print("Impossible to write in", path)
  118.         return False
  119.  
  120. def apply(instruction, path):
  121.  
  122.     if not backup (path):
  123.         return False
  124.     try:
  125.         txt = open(path, "r").read()
  126.     except:
  127.         print("File", path, "don't exist or you havn't access to it.")
  128.         return
  129.  
  130.     for old, new in instruction:
  131.         txt = txt.replace(old, new)
  132.  
  133.     try:
  134.         open(path, "w").write(txt)
  135.     except:
  136.         print("Impossible to write in", path)
  137.         return False
  138.  
  139.     return True
  140.  
  141. def printhelp():
  142.     print("python c3mm.py [instruction] [options]\n\t Instruction:\n\tinstall + path : install a cossacks3 mod file.\n\tuninstall + path : uninstall a cossacks3 mod file")
  143.  
  144. def loadmoddata(path):
  145.     info, mod = open(path, "r").read().split('\n', 1)
  146.     name, version = info.split(" version ")
  147.  
  148.     data = {}
  149.     data["name"] = name
  150.     data["version"] = version
  151.  
  152.     data["files"] = {}
  153.     for file in mod.split("*PATH*"):
  154.         if file == "":
  155.             continue
  156.         path, instruction = file.split("\n", 1)
  157.         instruction = [(old, new) for old, new in [line.split("*REPLACE*") for line in instruction.split("*END*") if line != ""]]
  158.         data["files"][path] = instruction
  159.  
  160.     return data
  161.  
  162. def mkuninstall(mod):
  163.     data = ""
  164.     data += mod["name"] + " version " + mod["version"] + " cleaner\n"
  165.     for file in mod["files"]:
  166.         data += "*PATH*" + file + "\n"
  167.         for old, new in mod["files"][file]:
  168.             data += new + "*REPLACE*" + old + "*END*"
  169.     open(mod["name"] + " " + mod["version"] + " uninstall", "w").write(data)
  170.  
  171. if __name__ == "__main__":
  172.     main(sys.argv)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×