gPodder 3.0.0 "397/D" released
[gpodder.git] / bin / gpodder-migrate2tres
blob41f140d6c5c11206f611862f7d70625d87206b0d
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
5 # gPodder - A media aggregator and podcast client
6 # Copyright (c) 2005-2011 Thomas Perl and the gPodder Team
8 # gPodder is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # gPodder is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 # gpodder-migrate2tres - Migrate data from gPodder 2.x to gPodder 3
24 # by Thomas Perl <thp@gpodder.org>; 2011-04-28
27 import sys
28 import os
29 import re
30 import ConfigParser
31 import shutil
33 gpodder_script = sys.argv[0]
34 if os.path.islink(gpodder_script):
35 gpodder_script = os.readlink(gpodder_script)
36 gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
37 prefix = os.path.abspath(os.path.normpath(gpodder_dir))
39 src_dir = os.path.join(prefix, 'src')
40 data_dir = os.path.join(prefix, 'data')
42 if os.path.exists(src_dir) and os.path.exists(data_dir) and \
43 not prefix.startswith('/usr'):
44 # Run gPodder from local source folder (not installed)
45 sys.path.insert(0, src_dir)
47 import gpodder
49 # Platform detection (i.e. Maemo 5, etc..)
50 gpodder.detect_platform()
53 from gpodder import schema
54 from gpodder import util
56 old_database = os.path.expanduser('~/.config/gpodder/database.sqlite')
57 new_database = gpodder.database_file
59 old_config = os.path.expanduser('~/.config/gpodder/gpodder.conf')
60 new_config = gpodder.config_file
62 if not os.path.exists(old_database):
63 print >>sys.stderr, """
64 Turns out that you never ran gPodder 2.
65 Can't find this required file:
67 %(old_database)s
68 """ % locals()
69 sys.exit(1)
71 old_downloads = None
73 if os.path.exists(old_config):
74 parser = ConfigParser.RawConfigParser()
75 parser.read(old_config)
76 try:
77 old_downloads = parser.get('gpodder-conf-1', 'download_dir')
78 except ConfigParser.NoSectionError:
79 # The file is empty / section (gpodder-conf-1) not found
80 pass
81 except ConfigParser.NoOptionError:
82 # The section is available, but the key (download_dir) is not
83 pass
85 if old_downloads is None:
86 # The user has no configuration. This usually happens when
87 # only the CLI version of gPodder is used. In this case, the
88 # download directory is most likely the default (bug 1434)
89 old_downloads = os.path.expanduser('~/gpodder-downloads')
91 new_downloads = gpodder.downloads
93 if not os.path.exists(old_downloads):
94 print >>sys.stderr, """
95 Old download directory does not exist. Creating empty one.
96 """
97 os.makedirs(old_downloads)
99 if any(os.path.exists(x) for x in (new_database, new_downloads)):
100 print >>sys.stderr, """
101 Existing gPodder 3 user data found.
102 To continue, please remove:
104 %(new_database)s
105 %(new_downloads)s
106 """ % locals()
107 sys.exit(1)
109 print >>sys.stderr, """
110 Would carry out the following actions:
112 Move downloads from %(old_downloads)s
113 to %(new_downloads)s
115 Convert database from %(old_database)s
116 to %(new_database)s
118 """ % locals()
120 result = raw_input('Continue? (Y/n) ')
122 if result in 'Yy':
123 util.make_directory(gpodder.home)
124 schema.convert_gpodder2_db(old_database, new_database)
125 if not os.path.exists(new_database):
126 print >>sys.stderr, 'Could not convert database.'
127 sys.exit(1)
129 shutil.move(old_downloads, new_downloads)
130 if not os.path.exists(new_downloads):
131 print >>sys.stderr, 'Could not move downloads.'
132 sys.exit(1)
134 print 'Done. Have fun with gPodder 3!'