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