Advertisement
Guest User

DaO: install mods (OSX)

a guest
Aug 23rd, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. #/usr/bin/python
  2.  
  3. USAGE = "python install.py 'Skip the Fade.dazip' '/Users/username/Documents/BioWare/Dragon Age/'"
  4. import os
  5. import sys
  6.  
  7. ADDIN_LOCATION = 'Contents/Addins'
  8. PACKAGE_LOCATION = 'Contents/packages'
  9.  
  10. DAO_ADDIN = 'ADDINS'
  11. DAO_PKG = 'packages/core/data'
  12. DAO_ADDIN_XML = lambda base: '{}/Settings/AddIns.xml'.format(base)
  13.  
  14. if sys.argv[1] == '-h':
  15.   print("USAGE (example): {}".format(USAGE))
  16.   sys.exit(0)
  17.  
  18. def fmtname(cmd):
  19.   # escape spaces before system call
  20.   cmd = cmd.replace(' ', '\ ')
  21.   return cmd
  22.  
  23. def unformat(f):
  24.   return f.replace('\ ', ' ')
  25.  
  26. mod = fmtname(sys.argv[1])
  27. daroot = fmtname(sys.argv[2])  # ~/Documents/BioWare/Dragon\ Age
  28. mod_fld = mod.strip('.dazip')
  29. mod_zip = mod.strip('dazip') + 'da.zip'
  30.  
  31. os.system('cp {} {}'.format(mod,mod_zip))
  32. os.system('unzip {} -d {}'.format(mod_zip, mod_fld))
  33.  
  34. # pull in the lines of the DAO addins
  35. da_addins_lines = open(DAO_ADDIN_XML(sys.argv[2])).readlines()
  36.  
  37. # pull in the lines of the mod xml
  38. try:
  39.   mod_manifest_lines = open('{}/manifest.xml'.format(unformat(mod_fld))).readlines()
  40. except IOError:
  41.   mod_manifest_lines = open('{}/Manifest.xml'.format(unformat(mod_fld))).readlines()
  42.  
  43. # check if this is already installed
  44. uid_line = filter(lambda line: 'UID=' in line, mod_manifest_lines)[0]
  45. line_fields = filter(lambda t: '=' in t, uid_line.strip().lstrip().split())
  46. uid = dict(map(lambda s: s.split('='), line_fields))['UID']
  47.  
  48. if any(['UID={}'.format(uid) in line for line in da_addins_lines]):
  49.   raise ValueError('{} already installed'.format(mod))
  50.  
  51.  
  52. # insert the mod lines into the correct location in the addins xml
  53. # (after the last addinitem)
  54.  
  55. last_addin_index = filter(lambda a: '</AddInItem>' in a[1], enumerate(da_addins_lines))[-1][0]
  56.  
  57. # get the mod lines to insert
  58. mod_begin_index = filter(lambda a: '<AddInItem' in a[1], enumerate(mod_manifest_lines))[0][0]
  59. mod_end_index = filter(lambda a: '</AddInItem>' in a[1], enumerate(mod_manifest_lines))[-1][0]
  60.  
  61. new_addins = da_addins_lines[0:last_addin_index + 1]
  62. new_addins.extend(mod_manifest_lines[mod_begin_index:mod_end_index + 1])
  63. new_addins.extend(da_addins_lines[last_addin_index + 1:])
  64.  
  65. # make a backup of the DAO manifest
  66. ctr = 1
  67. backup = '{}.{}-{}'.format(DAO_ADDIN_XML(daroot), 'backup', ctr)
  68. while os.path.exists(unformat(backup)):
  69.   ctr += 1
  70.   backup = '{}.{}-{}'.format(DAO_ADDIN_XML(daroot), 'backup', ctr)
  71. os.system('cp {}/Settings/AddIns.xml {}'.format(daroot, backup))
  72.  
  73. # move the addin files to the proper location
  74. os.system('mv {}/{}/* {}{}/'.format(mod_fld, ADDIN_LOCATION, daroot, DAO_ADDIN))
  75.  
  76. # check to see if there are package files
  77. mod_packages = '{}/{}'.format(mod_fld, PACKAGE_LOCATION)
  78. if os.path.exists(unformat(mod_packages)):
  79.   os.system('mv {}/core/data/* {}{}'.format(mod_packages, daroot, DAO_PKG))
  80.  
  81. # now replace addins.xml
  82. addins_out = open(unformat(DAO_ADDIN_XML(daroot)), 'w')
  83. addins_out.write(''.join(new_addins))
  84. addins_out.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement