2009-01-01 11:18:16 +00:00
|
|
|
#!/usr/bin/env python
|
2009-07-08 06:55:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2009 Raoul Snyman #
|
2009-11-29 20:30:41 +00:00
|
|
|
# Portions copyright (c) 2008-2009 Tim Bentley, Jonathan Corwin, Michael #
|
2009-11-30 20:29:26 +00:00
|
|
|
# Gorven, Scott Guerrieri, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
|
|
|
|
# Carsten Tinggaard #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
2009-01-01 11:18:16 +00:00
|
|
|
|
2009-06-23 16:25:40 +00:00
|
|
|
import os
|
2009-01-01 11:18:16 +00:00
|
|
|
import logging
|
|
|
|
import time
|
2009-10-18 15:19:57 +00:00
|
|
|
import subprocess
|
|
|
|
import codecs
|
2009-10-22 14:04:11 +00:00
|
|
|
|
2009-10-18 15:19:57 +00:00
|
|
|
if os.name == u'nt':
|
|
|
|
import win32api
|
|
|
|
import win32con
|
|
|
|
from win32com.client import Dispatch
|
2009-01-01 11:18:16 +00:00
|
|
|
|
|
|
|
from openlp.migration.display import *
|
|
|
|
from openlp.migration.migratefiles import *
|
|
|
|
from openlp.migration.migratebibles import *
|
|
|
|
from openlp.migration.migratesongs import *
|
|
|
|
|
2009-10-18 15:19:57 +00:00
|
|
|
###############################################################################
|
|
|
|
# For Windows, requires SQLite ODBC Driver to be installed
|
|
|
|
# (uses sqlite.exe and sqlite3.exe)
|
|
|
|
# http://www.ch-werner.de/sqliteodbc/
|
|
|
|
###############################################################################
|
|
|
|
|
2009-01-01 11:18:16 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
2009-10-18 21:07:57 +00:00
|
|
|
format=u'%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
|
|
|
|
datefmt=u'%m-%d %H:%M',
|
|
|
|
filename=u'openlp-migration.log',
|
|
|
|
filemode=u'w')
|
2009-01-01 11:18:16 +00:00
|
|
|
|
2009-07-08 06:55:08 +00:00
|
|
|
class Migration(object):
|
|
|
|
"""
|
|
|
|
A class to take care of the migration process.
|
|
|
|
"""
|
2009-01-01 11:18:16 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""
|
2009-07-08 06:55:08 +00:00
|
|
|
Initialise the process.
|
2009-01-01 11:18:16 +00:00
|
|
|
"""
|
2009-06-23 16:25:40 +00:00
|
|
|
self.display = Display()
|
2009-09-21 17:56:36 +00:00
|
|
|
self.stime = time.strftime(u'%Y-%m-%d-%H%M%S', time.localtime())
|
2009-06-23 16:25:40 +00:00
|
|
|
self.display.output(u'OpenLp v1.9.0 Migration Utility Started')
|
|
|
|
|
2009-01-01 11:18:16 +00:00
|
|
|
def process(self):
|
2009-07-08 06:55:08 +00:00
|
|
|
"""
|
|
|
|
Perform the conversion.
|
|
|
|
"""
|
2009-06-23 16:25:40 +00:00
|
|
|
#MigrateFiles(self.display).process()
|
2009-01-01 11:18:16 +00:00
|
|
|
MigrateSongs(self.display).process()
|
2009-06-23 16:25:40 +00:00
|
|
|
#MigrateBibles(self.display).process()
|
|
|
|
|
2009-01-01 11:18:16 +00:00
|
|
|
def move_log_file(self):
|
2009-07-08 06:55:08 +00:00
|
|
|
"""
|
|
|
|
Move the log file to a new location.
|
|
|
|
"""
|
2009-10-18 21:07:57 +00:00
|
|
|
fname = u'openlp-migration.log'
|
2009-01-01 11:18:16 +00:00
|
|
|
c = os.path.splitext(fname)
|
2009-06-16 18:21:24 +00:00
|
|
|
b = (c[0]+'-'+ unicode(self.stime) + c[1])
|
2009-10-18 21:07:57 +00:00
|
|
|
self.display.output(u'Logfile ' + b + u' generated')
|
2009-06-16 18:21:24 +00:00
|
|
|
self.display.output(u'Migration Utility Finished ')
|
2009-06-23 16:25:40 +00:00
|
|
|
os.rename(fname, b)
|
|
|
|
|
2009-10-18 15:19:57 +00:00
|
|
|
def convert_file(self, inname, outname):
|
|
|
|
"""
|
|
|
|
Convert a file from another encoding into UTF-8.
|
|
|
|
|
|
|
|
``inname``
|
|
|
|
The name of the file to be opened and converted.
|
|
|
|
|
|
|
|
``outname``
|
|
|
|
The output file name.
|
|
|
|
"""
|
2009-10-18 21:07:57 +00:00
|
|
|
infile = codecs.open(inname, u'r', encoding=u'CP1252')
|
|
|
|
writefile = codecs.open(outname, u'w', encoding=u'utf-8')
|
2009-10-18 15:19:57 +00:00
|
|
|
for line in infile:
|
|
|
|
writefile.write(line)
|
|
|
|
infile.close()
|
|
|
|
writefile.close()
|
|
|
|
|
|
|
|
def convert_sqlite2_to_3(self, olddb, newdb):
|
|
|
|
if os.name == u'nt':
|
2009-10-19 19:36:03 +00:00
|
|
|
# we can't make this a raw unicode string as the \U within it causes much confusion
|
2009-10-18 15:19:57 +00:00
|
|
|
hKey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, u'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\SQLite ODBC Driver')
|
2009-10-18 21:07:57 +00:00
|
|
|
value, type = win32api.RegQueryValueEx (hKey, u'UninstallString')
|
2009-10-18 15:19:57 +00:00
|
|
|
sqlitepath, temp = os.path.split(value)
|
|
|
|
sqliteexe = os.path.join(sqlitepath, u'sqlite.exe')
|
|
|
|
else:
|
|
|
|
sqliteexe = u'sqlite'
|
2009-10-18 21:07:57 +00:00
|
|
|
cmd = u'%s "%s" .dump' % (sqliteexe, olddb)
|
2009-10-18 15:19:57 +00:00
|
|
|
if os.name == u'nt':
|
2009-10-19 19:36:03 +00:00
|
|
|
subprocess.call(cmd, stdout=open(u'sqlite.dmp', u'w'))
|
2009-10-18 15:19:57 +00:00
|
|
|
else:
|
2009-10-19 19:36:03 +00:00
|
|
|
subprocess.call(cmd, stdout=open(u'sqlite.dmp', u'w'), shell=True)
|
2009-10-18 15:19:57 +00:00
|
|
|
self.convert_file(u'sqlite.dmp', u'sqlite3.dmp')
|
|
|
|
if os.name == u'nt':
|
|
|
|
sqlite3exe = os.path.join(sqlitepath, u'sqlite3.exe')
|
|
|
|
else:
|
|
|
|
sqlite3exe = u'sqlite3'
|
|
|
|
if os.path.isfile(newdb):
|
|
|
|
saveddb = newdb + self.stime
|
|
|
|
os.rename(newdb, saveddb)
|
2009-10-18 21:07:57 +00:00
|
|
|
cmd = '%s "%s"' % (sqlite3exe, newdb)
|
2009-10-18 15:19:57 +00:00
|
|
|
if os.name == u'nt':
|
2009-10-18 21:07:57 +00:00
|
|
|
subprocess.call(cmd, stdin=open(u'sqlite3.dmp', u'r'))
|
2009-10-18 15:19:57 +00:00
|
|
|
else:
|
2009-10-18 21:07:57 +00:00
|
|
|
subprocess.call(cmd, stdin=open(u'sqlite3.dmp', u'r'), shell=True)
|
2009-10-18 15:19:57 +00:00
|
|
|
os.remove(u'sqlite.dmp')
|
|
|
|
os.remove(u'sqlite3.dmp')
|
2009-01-01 11:18:16 +00:00
|
|
|
|
2009-10-18 21:07:57 +00:00
|
|
|
if __name__ == u'__main__':
|
2009-01-01 11:18:16 +00:00
|
|
|
mig = Migration()
|
2009-10-18 15:19:57 +00:00
|
|
|
config = PluginConfig(u'Songs')
|
|
|
|
newpath = config.get_data_path()
|
|
|
|
if os.name == u'nt':
|
|
|
|
if not os.path.isdir(newpath):
|
|
|
|
os.makedirs(newpath)
|
|
|
|
ALL_USERS_APPLICATION_DATA = 35
|
2009-10-18 21:07:57 +00:00
|
|
|
shell = Dispatch(u'Shell.Application')
|
2009-10-18 15:19:57 +00:00
|
|
|
folder = shell.Namespace(ALL_USERS_APPLICATION_DATA)
|
|
|
|
folderitem = folder.Self
|
|
|
|
olddb = os.path.join(folderitem.path, u'openlp.org', u'Data', u'songs.olp')
|
|
|
|
else:
|
|
|
|
olddb = os.path.join(newpath, u'songs.olp')
|
|
|
|
newdb = os.path.join(newpath, u'songs.sqlite')
|
|
|
|
mig.convert_sqlite2_to_3(olddb, newdb)
|
2009-01-01 11:18:16 +00:00
|
|
|
mig.process()
|
2009-06-23 16:25:40 +00:00
|
|
|
#mig.move_log_file()
|