Advertisement
Guest User

c3mm.py

a guest
Oct 1st, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. # coding: utf8
  2. # Cossacks 3 mod manager V 1.0
  3. #require python 3.5.x download link https://www.python.org/downloads/
  4. import sys
  5.  
  6. def main(argv):
  7.     if len (argv) == 1:
  8.         printhelp()
  9.         return
  10.  
  11.     if argv[1] == "install":
  12.         if len(argv) < 2:
  13.             mod = input("Please type name of mod file: ")
  14.         else:
  15.             mod = argv[2]
  16.  
  17.         mod = open(mod, "r").read().split("*PATH*")
  18.         for _mod in mod:
  19.             if _mod == "":
  20.                 continue
  21.             path, instruction = _mod.split("\n", 1)
  22.             if not backup(path):
  23.                 print("Fatal error")
  24.                 return
  25.             else:
  26.                 print("Backup of", path,  "succed!")
  27.  
  28.             if not apply(instruction, path):
  29.                 print("Fatal error")
  30.                 return
  31.             else:
  32.                 print("Patching of", path,  "succed!")
  33.  
  34. def backup(path):
  35.     try:
  36.         open(path + ".backup", "w").write(open(path, "r").read())
  37.         return True
  38.     except:
  39.         print("Impossible to write in", path)
  40.         return False
  41.  
  42. def apply(instruction, path):
  43.     try:
  44.         txt = open(path, "r").read()
  45.     except:
  46.         print("File", path, "don't exist or you havn't access to it.")
  47.         return False
  48.  
  49.     try:
  50.         mod = [(old, new) for old, new in [_instruction.split("*REPLACE*") for _instruction in instruction.split("*END*") if _instruction != '']]
  51.     except:
  52.         print("Bad format file for", mod)
  53.         return False
  54.    
  55.     for old, new in mod:
  56.         txt = txt.replace(old, new)
  57.  
  58.     try:
  59.         open(path, "w").write(txt)
  60.     except:
  61.         print("Impossible to write in", path)
  62.         return False
  63.  
  64.     return True
  65.  
  66. def printhelp():
  67.     print("python c3mm.py [instruction] [options]\n\t Instruction:\n\tinstall + path : install a cossacks3 mod files.")
  68.  
  69. if __name__ == "__main__":
  70.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement