Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin into f298_create...
[mediagoblin.git] / mediagoblin / gmg_commands / import_export.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from mediagoblin.gmg_commands import util as commands_util
18 from mediagoblin import mg_globals
19 from mediagoblin.db import util as db_util
20 from mediagoblin.db.open import setup_connection_and_db_from_config
21 from mediagoblin.init.config import read_mediagoblin_config
22 from mediagoblin import util as mg_util
23
24 import shlex
25 import tarfile
26 import subprocess
27 import os.path
28
29 def import_export_parse_setup(subparser):
30 # TODO: Add default
31 subparser.add_argument(
32 'tar_file')
33 subparser.add_argument(
34 '-cf', '--conf_file', default='mediagoblin.ini',
35 help='Config file used to set up environment')
36 subparser.add_argument(
37 '--mongodump_cache', default='/tmp/mediagoblin/mongodump',
38 help='mongodump cache directory')
39 subparser.add_argument(
40 '--mongodump_path', default='mongodump',
41 help='mongodump binary')
42 subparser.add_argument(
43 '--mongorestore_path', default='mongorestore',
44 help='mongorestore binary')
45 subparser.add_argument(
46 '--extract_path', default='/tmp/mediagoblin/import',
47 help='the directory to which the tarball should be extracted temporarily')
48 subparser.add_argument(
49 '--media_cache_path', default='/tmp/mediagoblin/mediaentries',
50 help='')
51
52 def _export_database(db, args):
53 print "\n== Exporting database ==\n"
54
55 command = '{mongodump_path} -d {database} -o {mongodump_cache}'.format(
56 mongodump_path=args.mongodump_path,
57 database=db.name,
58 mongodump_cache=args.mongodump_cache)
59
60 p = subprocess.Popen(
61 shlex.split(command))
62
63 p.wait()
64
65 print "\n== Database exported ==\n"
66
67 def _export_media(db, args):
68 for entry in db.media_entries.find():
69 storage = mg_util.import_component(
70 'mediagoblin.storage:BasicFileStorage')()
71 print(storage.get_file(entry['media_files']['medium']))
72 print(entry)
73
74 def _import_database(db, args):
75 command = '{mongorestore_path} -d {database} -o {mongodump_cache}'.format(
76 mongorestore_path=args.mongorestore_path,
77 database=db.name,
78 mongodump_cache=args.mongodump_cache)
79
80 def env_import(args):
81 config, validation_result = read_mediagoblin_config(args.conf_file)
82 connection, db = setup_connection_and_db_from_config(
83 config['mediagoblin'], use_pymongo=True)
84
85 tf = tarfile.open(
86 args.tar_file,
87 mode='r|gz')
88
89 tf.extractall(args.extract_path)
90
91 def env_export(args):
92 config, validation_result = read_mediagoblin_config(args.conf_file)
93 connection, db = setup_connection_and_db_from_config(
94 config['mediagoblin'], use_pymongo=True)
95
96 if os.path.exists(args.tar_file):
97 overwrite = raw_input(
98 'The output file already exists. '
99 'Are you **SURE** you want to overwrite it? '
100 '(yes/no)> ')
101 if not overwrite == 'yes':
102 print "Aborting."
103 return
104
105 _export_media(db, args)
106
107 tf = tarfile.open(
108 args.tar_file,
109 mode='w|gz')
110
111
112 _export_database(db, args)
113
114 tf.add(args.mongodump_cache)
115