Guest User

c3mm.py

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

×