bd8039b58ff1ebd9ace36b224e414c9eea37e109
[mediagoblin.git] / mediagoblin / gmg_commands / reprocess.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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 import argparse
17 import os
18
19 from mediagoblin import mg_globals
20 from mediagoblin.db.models import MediaEntry
21 from mediagoblin.gmg_commands import util as commands_util
22 from mediagoblin.submit.lib import run_process_media
23 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
24 from mediagoblin.tools.pluginapi import hook_handle
25 from mediagoblin.processing import (
26 ProcessorDoesNotExist, ProcessorNotEligible,
27 get_entry_and_processing_manager, get_processing_manager_for_type)
28
29
30 def reprocess_parser_setup(subparser):
31 subparser.add_argument(
32 '--celery',
33 action='store_true',
34 help="Don't process eagerly, pass off to celery")
35
36 subparsers = subparser.add_subparsers(dest="reprocess_subcommand")
37
38 ###################
39 # available command
40 ###################
41 available_parser = subparsers.add_parser(
42 "available",
43 help="Find out what actions are available for this media")
44
45 available_parser.add_argument(
46 "id_or_type",
47 help="Media id or media type to check")
48
49 available_parser.add_argument(
50 "--action-help",
51 action="store_true",
52 help="List argument help for each action available")
53
54
55 ############################################
56 # run command (TODO: and bulk_run command??)
57 ############################################
58
59 run_parser = subparsers.add_parser(
60 "run",
61 help="Run a reprocessing on one or more media")
62
63 run_parser.add_argument(
64 '--state', '-s',
65 help="Reprocess media entries in this state"
66 " such as 'failed' or 'processed'")
67 run_parser.add_argument(
68 '--type', '-t',
69 help="The type of media to be reprocessed such as 'video' or 'image'")
70 run_parser.add_argument(
71 '--thumbnails',
72 action="store_true",
73 help="Regenerate thumbnails for all processed media")
74
75 run_parser.add_argument(
76 'media_id',
77 help="The media_entry id(s) you wish to reprocess.")
78
79 run_parser.add_argument(
80 'reprocess_command',
81 help="The reprocess command you intend to run")
82
83 run_parser.add_argument(
84 'reprocess_args',
85 nargs=argparse.REMAINDER,
86 help="rest of arguments to the reprocessing tool")
87
88
89 ###############
90 # help command?
91 ###############
92
93
94
95 def _set_media_type(args):
96 """
97 This will verify that all media id's are of the same media_type. If the
98 --type flag is set, it will be replaced by the given media id's type.
99
100 If they are trying to process different media types, an Exception will be
101 raised.
102 """
103 if args[0].media_id:
104 if len(args[0].media_id) == 1:
105 args[0].type = MediaEntry.query.filter_by(id=args[0].media_id[0])\
106 .first().media_type.split('.')[-1]
107
108 elif len(args[0].media_id) > 1:
109 media_types = []
110
111 for id in args[0].media_id:
112 media_types.append(MediaEntry.query.filter_by(id=id).first()
113 .media_type.split('.')[-1])
114 for type in media_types:
115 if media_types[0] != type:
116 raise Exception((u'You cannot reprocess different'
117 ' media_types at the same time.'))
118
119 args[0].type = media_types[0]
120
121
122 def _reprocess_all(args):
123 """
124 This handles reprocessing if no media_id's are given.
125 """
126 if not args[0].type:
127 # If no media type is given, we can either regenerate all thumbnails,
128 # or try to reprocess all failed media
129
130 if args[0].thumbnails:
131 if args[0].available:
132 print _('Available options for regenerating all processed'
133 ' media thumbnails: \n'
134 '\t --size: max_width max_height'
135 ' (defaults to config specs)')
136 else:
137 #TODO regenerate all thumbnails
138 pass
139
140 # Reprocess all failed media
141 elif args[0].state == 'failed':
142 if args[0].available:
143 print _('\n Available reprocess actions for all failed'
144 ' media_entries: \n \t --initial_processing')
145 else:
146 #TODO reprocess all failed entries
147 pass
148
149 # If here, they didn't set the --type flag and were trying to do
150 # something other the generating thumbnails or initial_processing
151 else:
152 raise Exception(_('You must set --type when trying to reprocess'
153 ' all media_entries, unless you set --state'
154 ' to "failed".'))
155
156 else:
157 _run_reprocessing(args)
158
159
160 def _run_reprocessing(args):
161 # Are they just asking for the available reprocessing options for the given
162 # media?
163 if args[0].available:
164 if args[0].state == 'failed':
165 print _('\n Available reprocess actions for all failed'
166 ' media_entries: \n \t --initial_processing')
167 else:
168 result = hook_handle(('reprocess_action', args[0].type), args)
169 if not result:
170 print _('Sorry there is no available reprocessing for {}'
171 ' entries in the {} state'.format(args[0].type,
172 args[0].state))
173 else:
174 # Run media reprocessing
175 return hook_handle(('media_reprocess', args[0].type), args)
176
177
178 def _set_media_state(args):
179 """
180 This will verify that all media id's are in the same state. If the
181 --state flag is set, it will be replaced by the given media id's state.
182
183 If they are trying to process different media states, an Exception will be
184 raised.
185 """
186 if args[0].media_id:
187 # Only check if we are given media_ids
188 if len(args[0].media_id) == 1:
189 args[0].state = MediaEntry.query.filter_by(id=args[0].media_id[0])\
190 .first().state
191
192 elif len(args[0].media_id) > 1:
193 media_states = []
194
195 for id in args[0].media_id:
196 media_states.append(MediaEntry.query.filter_by(id=id).first()
197 .state)
198
199 # Make sure that all media are in the same state
200 for state in media_states:
201 if state != media_states[0]:
202 raise Exception(_('You can only reprocess media that is in'
203 ' the same state.'))
204
205 args[0].state = media_states[0]
206
207 # If no state was set, then we will default to the processed state
208 if not args[0].state:
209 args[0].state = 'processed'
210
211
212 def available(args):
213 # Get the media type, either by looking up media id, or by specific type
214 try:
215 media_id = int(args.id_or_type)
216 media_entry, manager = get_entry_and_processing_manager(media_id)
217 media_type = media_entry.media_type
218 except ValueError:
219 media_type = args.id_or_type
220 media_entry = None
221 manager = get_processing_manager_for_type(media_type)
222
223 if media_entry is None:
224 processors = manager.list_all_processors()
225 else:
226 processors = manager.list_eligible_processors(media_entry)
227
228 print "Available processors:"
229 print "====================="
230 print ""
231
232 if args.action_help:
233 for processor in processors:
234 print processor.name
235 print "-" * len(processor.name)
236
237 parser = processor.generate_parser()
238 parser.print_help()
239 print ""
240
241 else:
242 for processor in processors:
243 if processor.description:
244 print " - %s: %s" % (processor.name, processor.description)
245 else:
246 print " - %s" % processor.name
247
248
249 def run(args):
250 media_entry, manager = get_entry_and_processing_manager(args.media_id)
251
252 # TODO: (maybe?) This could probably be handled entirely by the
253 # processor class...
254 try:
255 processor_class = manager.get_processor(
256 args.reprocess_command, media_entry)
257 except ProcessorDoesNotExist:
258 print 'No such processor "%s" for media with id "%s"' % (
259 args.reprocess_command, media_entry.id)
260 return
261 except ProcessorNotEligible:
262 print 'Processor "%s" exists but media "%s" is not eligible' % (
263 args.reprocess_command, media_entry.id)
264 return
265
266 reprocess_parser = processor_class.generate_parser()
267 reprocess_args = reprocess_parser.parse_args(args.reprocess_args)
268 reprocess_request = processor_class.args_to_request(reprocess_args)
269 run_process_media(
270 media_entry,
271 reprocess_action=args.reprocess_command,
272 reprocess_info=reprocess_request)
273
274
275 def reprocess(args):
276 # Run eagerly unless explicetly set not to
277 if not args.celery:
278 os.environ['CELERY_ALWAYS_EAGER'] = 'true'
279
280 commands_util.setup_app(args)
281
282 if args.reprocess_subcommand == "run":
283 run(args)
284
285 elif args.reprocess_subcommand == "available":
286 available(args)