fix unittest on python 3.5
[gpodder.git] / bin / gpodder-migrate2tres
blobe300e1511d39a605e1c484b8dae0f559869bb1ec
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
5 # gPodder - A media aggregator and podcast client
6 # Copyright (c) 2005-2018 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 configparser
28 import os
29 import re
30 import shutil
31 import sys
33 gpodder_script = sys.argv[0]
34 gpodder_script = os.path.realpath(gpodder_script)
35 gpodder_dir = os.path.join(os.path.dirname(gpodder_script), '..')
36 prefix = os.path.abspath(os.path.normpath(gpodder_dir))
38 src_dir = os.path.join(prefix, 'src')
40 if os.path.exists(os.path.join(src_dir, 'gpodder', '__init__.py')):
41     # Run gPodder from local source folder (not installed)
42     sys.path.insert(0, src_dir)
44 import gpodder  # isort:skip
46 gpodder.prefix = prefix
48 from gpodder import schema, util  # isort:skip
50 old_database = os.path.expanduser('~/.config/gpodder/database.sqlite')
51 new_database = gpodder.database_file
53 old_config = os.path.expanduser('~/.config/gpodder/gpodder.conf')
54 new_config = gpodder.config_file
56 if not os.path.exists(old_database):
57     print("""
58     Turns out that you never ran gPodder 2.
59     Can't find this required file:
61        %(old_database)s
62     """ % locals(), file=sys.stderr)
63     sys.exit(1)
65 old_downloads = None
67 if os.path.exists(old_config):
68     parser = configparser.RawConfigParser()
69     parser.read(old_config)
70     try:
71         old_downloads = parser.get('gpodder-conf-1', 'download_dir')
72     except configparser.NoSectionError:
73         # The file is empty / section (gpodder-conf-1) not found
74         pass
75     except configparser.NoOptionError:
76         # The section is available, but the key (download_dir) is not
77         pass
79 if old_downloads is None:
80     # The user has no configuration. This usually happens when
81     # only the CLI version of gPodder is used. In this case, the
82     # download directory is most likely the default (bug 1434)
83     old_downloads = os.path.expanduser('~/gpodder-downloads')
85 new_downloads = gpodder.downloads
87 if not os.path.exists(old_downloads):
88     print("""
89     Old download directory does not exist. Creating empty one.
90     """, file=sys.stderr)
91     os.makedirs(old_downloads)
93 if any(os.path.exists(x) for x in (new_database, new_downloads)):
94     print("""
95     Existing gPodder 3 user data found.
96     To continue, please remove:
98        %(new_database)s
99        %(new_downloads)s
100     """ % locals(), file=sys.stderr)
101     sys.exit(1)
103 print("""
104   Would carry out the following actions:
106       Move downloads from %(old_downloads)s
107                        to %(new_downloads)s
109       Convert database from %(old_database)s
110                          to %(new_database)s
112 """ % locals(), file=sys.stderr)
114 result = input('Continue? (Y/n) ')
116 if result in 'Yy':
117     util.make_directory(gpodder.home)
118     schema.convert_gpodder2_db(old_database, new_database)
119     if not os.path.exists(new_database):
120         print('Could not convert database.', file=sys.stderr)
121         sys.exit(1)
123     shutil.move(old_downloads, new_downloads)
124     if not os.path.exists(new_downloads):
125         print('Could not move downloads.', file=sys.stderr)
126         sys.exit(1)
128     print('Done. Have fun with gPodder 3!')