/tools/migrate.py

http://pyaimt.googlecode.com/ · Python · 148 lines · 110 code · 8 blank · 30 comment · 27 complexity · e42dbd6fde06ab2aaeed2bcf7a25b634 MD5 · raw file

  1. #!/usr/bin/env python
  2. #
  3. # Spool Migration Script
  4. #
  5. # This script takes two arguments. The first is either "dump" or "restore".
  6. # The second argument will be a file that your xdb will be dumped to, in the
  7. # case of a "dump", or restored from, in the case of a "restore". The
  8. # spool config used will be what is in config.xml in the root of the
  9. # distribution. This script is expected to be run from this directory.
  10. #
  11. # For example, if you are currently using the xmlfiles xdb backend, you
  12. # would first have a config.xml file that is configured for that. You would
  13. # then type './migrate.py dump mydump'. This will produce a long-ish file
  14. # in XML format that contains all of the data from your spool.
  15. #
  16. # Next, lets say you wanted to switch to the MySQL xdb backend. You would
  17. # first make sure that you have it set up correctly as per the instructions.
  18. # (you would have had to create the tables using db-setup.mysql in this
  19. # directory) Then you would set up your config.xml appropriately and
  20. # run './migrate restore mydump'. This will import the xdb roster into
  21. # your new spool.
  22. #
  23. # WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  24. # A restore -will- write over entries from your current spool.
  25. # Please make sure to make a backup if you wish to do so.
  26. # WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  27. #
  28. # This script accepts a subset of the command line flags that the transport
  29. # itself accepts. Please run it with '-h' to see the available options.
  30. #
  31. transportname = "PyAIMt"
  32. dumpversion = "1.0"
  33. import sys
  34. reload(sys)
  35. sys.setdefaultencoding('utf-8')
  36. del sys.setdefaultencoding
  37. sys.path.append("../src")
  38. import debug
  39. import getopt
  40. import config
  41. import utils
  42. def showhelp():
  43. print "./migrate.py [options] cmd file"
  44. print "options:"
  45. print " -h print this help"
  46. print " -c <file> read configuration from this file"
  47. print " -o <var>=<setting> set config var to setting"
  48. print " -d print debugging output"
  49. print "cmd:";
  50. print " dump dump spool to file"
  51. print " restore restore spool from file"
  52. sys.exit(0)
  53. conffile = "config.xml"
  54. options = {}
  55. opts, args = getopt.getopt(sys.argv[1:], "c:do:h", ["config=", "debug", "option=", "help"])
  56. for o, v in opts:
  57. if o in ("-c", "--config"):
  58. conffile = v
  59. elif o in ("-d", "--debug"):
  60. config.debugOn = True
  61. elif o in ("-o", "--option"):
  62. var, setting = v.split("=", 2)
  63. options[var] = setting
  64. elif o in ("-h", "--help"):
  65. showhelp()
  66. reload(debug)
  67. if len(args) != 2:
  68. showhelp()
  69. import twistfix
  70. twistfix.main()
  71. import xmlconfig
  72. xmlconfig.Import(conffile, options)
  73. from twisted.words.xish.domish import Element
  74. if args[0] == "dump":
  75. import xdb
  76. myxdb = xdb.XDB(config.jid)
  77. out = Element((None, "pydump"))
  78. out["transport"] = transportname
  79. out["version"] = dumpversion
  80. for jid in myxdb.getRegistrationList():
  81. print "Dumping "+jid+"..."
  82. userpass = myxdb.getRegistration(jid)
  83. if not userpass: continue
  84. user = out.addElement("user")
  85. user["jid"] = jid
  86. user["username"] = userpass[0]
  87. user["password"] = userpass[1]
  88. prefs = user.addElement("preferences")
  89. settinglist = myxdb.getSettingList(jid)
  90. if settinglist:
  91. for pref in settinglist:
  92. thispref = settinglist.addElement(pref)
  93. thispref.addContent(settinglist[pref])
  94. listtypes = myxdb.getListTypes(jid)
  95. if listtypes:
  96. for listtype in listtypes:
  97. list = user.addElement("list")
  98. list["type"] = listtype
  99. listentries = myxdb.getList(listtype, jid)
  100. if not listentries: continue
  101. for entry in listentries:
  102. listentry = list.addElement("entry")
  103. listentry["name"] = entry[0]
  104. attrs = entry[1]
  105. for attr in attrs:
  106. entryattr = listentry.addElement(attr)
  107. entryattr.addContent(attrs[attr])
  108. f = open(args[1], "w")
  109. f.write(out.toXml())
  110. f.close()
  111. elif args[0] == "restore":
  112. import xdb
  113. myxdb = xdb.XDB(config.jid)
  114. input = utils.parseFile(args[1])
  115. if input.getAttribute("transport") != transportname:
  116. print "The dump file specified does not appear to be for this transport."
  117. sys.exit(0)
  118. for child in input.elements():
  119. jid = child.getAttribute("jid")
  120. print "Restoring "+jid+"..."
  121. doesexist = myxdb.getRegistration(jid)
  122. if doesexist:
  123. myxdb.removeRegistration(jid)
  124. username = child.getAttribute("username")
  125. password = child.getAttribute("password")
  126. myxdb.setRegistration(jid, username, password)
  127. for child2 in child.elements():
  128. if child2.name == "preferences":
  129. for pref in child2.elements():
  130. myxdb.setSetting(jid, pref, pref.__str__())
  131. elif child2.name == "list":
  132. type = child2.getAttribute("type")
  133. for entry in child2.elements():
  134. name = entry.getAttribute("name")
  135. attrs = {}
  136. for attr in entry.elements():
  137. attrs[attr.name] = attr.__str__()
  138. myxdb.setListEntry(type, jid, name, payload=attrs)
  139. else:
  140. showhelp()