1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
)
30 def reprocess_parser_setup(subparser
):
31 subparser
.add_argument(
34 help="Don't process eagerly, pass off to celery")
36 subparsers
= subparser
.add_subparsers(dest
="reprocess_subcommand")
41 available_parser
= subparsers
.add_parser(
43 help="Find out what actions are available for this media")
45 available_parser
.add_argument(
47 help="Media id or media type to check")
49 available_parser
.add_argument(
52 help="List argument help for each action available")
54 available_parser
.add_argument(
56 help="The state of media you would like to reprocess")
63 run_parser
= subparsers
.add_parser(
65 help="Run a reprocessing on one or more media")
67 run_parser
.add_argument(
69 help="The media_entry id(s) you wish to reprocess.")
71 run_parser
.add_argument(
73 help="The reprocess command you intend to run")
75 run_parser
.add_argument(
77 nargs
=argparse
.REMAINDER
,
78 help="rest of arguments to the reprocessing tool")
84 thumbs
= subparsers
.add_parser(
86 help='Regenerate thumbs for all processed media')
92 metavar
=('max_width', 'max_height'))
97 subparsers
.add_parser(
99 help='Reprocess all failed media')
104 bulk_run_parser
= subparsers
.add_parser(
106 help='Run reprocessing on a given media type or state')
108 bulk_run_parser
.add_argument(
110 help='The type of media you would like to process')
112 bulk_run_parser
.add_argument(
115 help='The state of the media you would like to process. Defaults to' \
118 bulk_run_parser
.add_argument(
120 nargs
=argparse
.REMAINDER
,
121 help='The rest of the arguments to the reprocessing tool')
129 def _set_media_type(args
):
131 This will verify that all media id's are of the same media_type. If the
132 --type flag is set, it will be replaced by the given media id's type.
134 If they are trying to process different media types, an Exception will be
138 if len(args
[0].media_id
) == 1:
139 args
[0].type = MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
140 .first().media_type
.split('.')[-1]
142 elif len(args
[0].media_id
) > 1:
145 for id in args
[0].media_id
:
146 media_types
.append(MediaEntry
.query
.filter_by(id=id).first()
147 .media_type
.split('.')[-1])
148 for type in media_types
:
149 if media_types
[0] != type:
150 raise Exception((u
'You cannot reprocess different'
151 ' media_types at the same time.'))
153 args
[0].type = media_types
[0]
156 def _reprocess_all(args
):
158 This handles reprocessing if no media_id's are given.
161 # If no media type is given, we can either regenerate all thumbnails,
162 # or try to reprocess all failed media
164 if args
[0].thumbnails
:
165 if args
[0].available
:
166 print _('Available options for regenerating all processed'
167 ' media thumbnails: \n'
168 '\t --size: max_width max_height'
169 ' (defaults to config specs)')
171 #TODO regenerate all thumbnails
174 # Reprocess all failed media
175 elif args
[0].state
== 'failed':
176 if args
[0].available
:
177 print _('\n Available reprocess actions for all failed'
178 ' media_entries: \n \t --initial_processing')
180 #TODO reprocess all failed entries
183 # If here, they didn't set the --type flag and were trying to do
184 # something other the generating thumbnails or initial_processing
186 raise Exception(_('You must set --type when trying to reprocess'
187 ' all media_entries, unless you set --state'
191 _run_reprocessing(args
)
194 def _run_reprocessing(args
):
195 # Are they just asking for the available reprocessing options for the given
197 if args
[0].available
:
198 if args
[0].state
== 'failed':
199 print _('\n Available reprocess actions for all failed'
200 ' media_entries: \n \t --initial_processing')
202 result
= hook_handle(('reprocess_action', args
[0].type), args
)
204 print _('Sorry there is no available reprocessing for {}'
205 ' entries in the {} state'.format(args
[0].type,
208 # Run media reprocessing
209 return hook_handle(('media_reprocess', args
[0].type), args
)
212 def _set_media_state(args
):
214 This will verify that all media id's are in the same state. If the
215 --state flag is set, it will be replaced by the given media id's state.
217 If they are trying to process different media states, an Exception will be
221 # Only check if we are given media_ids
222 if len(args
[0].media_id
) == 1:
223 args
[0].state
= MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
226 elif len(args
[0].media_id
) > 1:
229 for id in args
[0].media_id
:
230 media_states
.append(MediaEntry
.query
.filter_by(id=id).first()
233 # Make sure that all media are in the same state
234 for state
in media_states
:
235 if state
!= media_states
[0]:
236 raise Exception(_('You can only reprocess media that is in'
239 args
[0].state
= media_states
[0]
241 # If no state was set, then we will default to the processed state
242 if not args
[0].state
:
243 args
[0].state
= 'processed'
247 # Get the media type, either by looking up media id, or by specific type
249 media_id
= int(args
.id_or_type
)
250 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
251 media_type
= media_entry
.media_type
253 media_type
= args
.id_or_type
255 manager
= get_processing_manager_for_type(media_type
)
258 processors
= manager
.list_all_processors_by_state(args
.state
)
259 elif media_entry
is None:
260 processors
= manager
.list_all_processors()
262 processors
= manager
.list_eligible_processors(media_entry
)
264 print "Available processors:"
265 print "====================="
269 for processor
in processors
:
271 print "-" * len(processor
.name
)
273 parser
= processor
.generate_parser()
278 for processor
in processors
:
279 if processor
.description
:
280 print " - %s: %s" % (processor
.name
, processor
.description
)
282 print " - %s" % processor
.name
286 media_entry
, manager
= get_entry_and_processing_manager(args
.media_id
)
288 # TODO: (maybe?) This could probably be handled entirely by the
291 processor_class
= manager
.get_processor(
292 args
.reprocess_command
, media_entry
)
293 except ProcessorDoesNotExist
:
294 print 'No such processor "%s" for media with id "%s"' % (
295 args
.reprocess_command
, media_entry
.id)
297 except ProcessorNotEligible
:
298 print 'Processor "%s" exists but media "%s" is not eligible' % (
299 args
.reprocess_command
, media_entry
.id)
302 reprocess_parser
= processor_class
.generate_parser()
303 reprocess_args
= reprocess_parser
.parse_args(args
.reprocess_args
)
304 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
307 reprocess_action
=args
.reprocess_command
,
308 reprocess_info
=reprocess_request
)
315 #TODO regenerate thumbs for all processed media
320 #TODO initial processing on all failed media
325 # Run eagerly unless explicetly set not to
327 os
.environ
['CELERY_ALWAYS_EAGER'] = 'true'
329 commands_util
.setup_app(args
)
331 if args
.reprocess_subcommand
== "run":
334 elif args
.reprocess_subcommand
== "available":
337 elif args
.reprocess_subcommand
== "bulk_run":
340 elif args
.reprocess_subcommand
== "thumbs":
343 elif args
.reprocess_subcommand
== "initial":