PageRenderTime 77ms CodeModel.GetById 43ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/migrate.py

http://pyaimt.googlecode.com/
Python | 148 lines | 110 code | 8 blank | 30 comment | 38 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
 32transportname	= "PyAIMt"
 33dumpversion	= "1.0"
 34
 35import sys
 36reload(sys)
 37sys.setdefaultencoding('utf-8')
 38del sys.setdefaultencoding
 39sys.path.append("../src")
 40import debug
 41import getopt
 42import config
 43import utils
 44
 45def showhelp():
 46	print "./migrate.py [options] cmd file"
 47	print "options:"
 48	print "   -h                  print this help"
 49	print "   -c <file>           read configuration from this file"
 50	print "   -o <var>=<setting>  set config var to setting"
 51	print "   -d                  print debugging output"
 52	print "cmd:";
 53	print "   dump                dump spool to file"
 54	print "   restore             restore spool from file"
 55	sys.exit(0)
 56
 57conffile = "config.xml"
 58options = {}
 59opts, args = getopt.getopt(sys.argv[1:], "c:do:h", ["config=", "debug", "option=", "help"])
 60for o, v in opts:
 61	if o in ("-c", "--config"):
 62		conffile = v
 63	elif o in ("-d", "--debug"):
 64		config.debugOn = True
 65	elif o in ("-o", "--option"):
 66		var, setting = v.split("=", 2)
 67		options[var] = setting
 68	elif o in ("-h", "--help"):
 69		showhelp()
 70reload(debug)
 71
 72if len(args) != 2:
 73	showhelp()
 74
 75import twistfix
 76twistfix.main()
 77
 78import xmlconfig
 79xmlconfig.Import(conffile, options)
 80from twisted.words.xish.domish import Element
 81
 82if args[0] == "dump":
 83	import xdb
 84	myxdb = xdb.XDB(config.jid)
 85	out = Element((None, "pydump"))
 86	out["transport"] = transportname
 87	out["version"] = dumpversion
 88	for jid in myxdb.getRegistrationList():
 89		print "Dumping "+jid+"..."
 90		userpass = myxdb.getRegistration(jid)
 91		if not userpass: continue
 92		user = out.addElement("user")
 93		user["jid"] = jid
 94		user["username"] = userpass[0]
 95		user["password"] = userpass[1]
 96		prefs = user.addElement("preferences")
 97		settinglist = myxdb.getSettingList(jid)
 98		if settinglist:
 99			for pref in settinglist:
100				thispref = settinglist.addElement(pref)
101				thispref.addContent(settinglist[pref])
102		listtypes = myxdb.getListTypes(jid)
103		if listtypes:
104			for listtype in listtypes:
105				list = user.addElement("list")
106				list["type"] = listtype
107				listentries = myxdb.getList(listtype, jid)
108				if not listentries: continue
109				for entry in listentries:
110					listentry = list.addElement("entry")
111					listentry["name"] = entry[0]
112					attrs = entry[1]
113					for attr in attrs:
114						entryattr = listentry.addElement(attr)
115						entryattr.addContent(attrs[attr])
116	f = open(args[1], "w")
117	f.write(out.toXml())
118	f.close()
119elif args[0] == "restore":
120	import xdb
121	myxdb = xdb.XDB(config.jid)
122	input = utils.parseFile(args[1])
123	if input.getAttribute("transport") != transportname:
124		print "The dump file specified does not appear to be for this transport."
125		sys.exit(0)
126	for child in input.elements():
127		jid = child.getAttribute("jid")
128		print "Restoring "+jid+"..."
129		doesexist = myxdb.getRegistration(jid)
130		if doesexist:
131			myxdb.removeRegistration(jid)
132		username = child.getAttribute("username")
133		password = child.getAttribute("password")
134		myxdb.setRegistration(jid, username, password)
135		for child2 in child.elements():
136			if child2.name == "preferences":
137				for pref in child2.elements():
138					myxdb.setSetting(jid, pref, pref.__str__())
139			elif child2.name == "list":
140				type = child2.getAttribute("type")
141				for entry in child2.elements():
142					name = entry.getAttribute("name")
143					attrs = {}
144					for attr in entry.elements():
145						attrs[attr.name] = attr.__str__()
146					myxdb.setListEntry(type, jid, name, payload=attrs)
147else:
148	showhelp()