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
,
28 ProcessingManagerDoesNotExist
)
31 def reprocess_parser_setup(subparser
):
32 subparser
.add_argument(
35 help="Don't process eagerly, pass off to celery")
37 subparsers
= subparser
.add_subparsers(dest
="reprocess_subcommand")
42 available_parser
= subparsers
.add_parser(
44 help="Find out what actions are available for this media")
46 available_parser
.add_argument(
48 help="Media id or media type to check")
50 available_parser
.add_argument(
53 help="List argument help for each action available")
55 available_parser
.add_argument(
57 help="The state of media you would like to reprocess")
64 run_parser
= subparsers
.add_parser(
66 help="Run a reprocessing on one or more media")
68 run_parser
.add_argument(
70 help="The media_entry id(s) you wish to reprocess.")
72 run_parser
.add_argument(
74 help="The reprocess command you intend to run")
76 run_parser
.add_argument(
78 nargs
=argparse
.REMAINDER
,
79 help="rest of arguments to the reprocessing tool")
85 thumbs
= subparsers
.add_parser(
87 help='Regenerate thumbs for all processed media')
93 metavar
=('max_width', 'max_height'))
98 subparsers
.add_parser(
100 help='Reprocess all failed media')
105 bulk_run_parser
= subparsers
.add_parser(
107 help='Run reprocessing on a given media type or state')
109 bulk_run_parser
.add_argument(
111 help='The type of media you would like to process')
113 bulk_run_parser
.add_argument(
117 help='The state of the media you would like to process. Defaults to' \
120 bulk_run_parser
.add_argument(
122 help='The reprocess command you intend to run')
124 bulk_run_parser
.add_argument(
126 nargs
=argparse
.REMAINDER
,
127 help='The rest of the arguments to the reprocessing tool')
135 # Get the media type, either by looking up media id, or by specific type
137 media_id
= int(args
.id_or_type
)
138 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
139 media_type
= media_entry
.media_type
141 media_type
= args
.id_or_type
143 manager
= get_processing_manager_for_type(media_type
)
144 except ProcessingManagerDoesNotExist
:
145 entry
= MediaEntry
.query
.filter_by(id=args
.id_or_type
).first()
146 print 'No such processing manager for {0}'.format(entry
.media_type
)
149 processors
= manager
.list_all_processors_by_state(args
.state
)
150 elif media_entry
is None:
151 processors
= manager
.list_all_processors()
153 processors
= manager
.list_eligible_processors(media_entry
)
155 print "Available processors:"
156 print "====================="
160 for processor
in processors
:
162 print "-" * len(processor
.name
)
164 parser
= processor
.generate_parser()
169 for processor
in processors
:
170 if processor
.description
:
171 print " - %s: %s" % (processor
.name
, processor
.description
)
173 print " - %s" % processor
.name
176 def run(args
, media_id
=None):
178 media_id
= args
.media_id
180 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
182 # TODO: (maybe?) This could probably be handled entirely by the
185 processor_class
= manager
.get_processor(
186 args
.reprocess_command
, media_entry
)
187 except ProcessorDoesNotExist
:
188 print 'No such processor "%s" for media with id "%s"' % (
189 args
.reprocess_command
, media_entry
.id)
191 except ProcessorNotEligible
:
192 print 'Processor "%s" exists but media "%s" is not eligible' % (
193 args
.reprocess_command
, media_entry
.id)
196 reprocess_parser
= processor_class
.generate_parser()
197 reprocess_args
= reprocess_parser
.parse_args(args
.reprocess_args
)
198 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
201 reprocess_action
=args
.reprocess_command
,
202 reprocess_info
=reprocess_request
)
204 except ProcessingManagerDoesNotExist
:
205 entry
= MediaEntry
.query
.filter_by(id=media_id
).first()
206 print 'No such processing manager for {0}'.format(entry
.media_type
)
211 Bulk reprocessing of a given media_type
213 query
= MediaEntry
.query
.filter_by(media_type
=args
.type,
222 Regenerate thumbs for all processed media
224 query
= MediaEntry
.query
.filter_by(state
='processed')
228 media_entry
, manager
= get_entry_and_processing_manager(entry
.id)
230 # TODO: (maybe?) This could probably be handled entirely by the
233 processor_class
= manager
.get_processor(
234 'resize', media_entry
)
235 except ProcessorDoesNotExist
:
236 print 'No such processor "%s" for media with id "%s"' % (
237 'resize', media_entry
.id)
239 except ProcessorNotEligible
:
240 print 'Processor "%s" exists but media "%s" is not eligible' % (
241 'resize', media_entry
.id)
244 reprocess_parser
= processor_class
.generate_parser()
246 # prepare filetype and size to be passed into reprocess_parser
248 extra_args
= 'thumb --{0} {1} {2}'.format(
249 processor_class
.thumb_size
,
255 reprocess_args
= reprocess_parser
.parse_args(extra_args
.split())
256 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
259 reprocess_action
='resize',
260 reprocess_info
=reprocess_request
)
262 except ProcessingManagerDoesNotExist
:
263 print 'No such processing manager for {0}'.format(entry
.media_type
)
268 Reprocess all failed media
270 query
= MediaEntry
.query
.filter_by(state
='failed')
274 media_entry
, manager
= get_entry_and_processing_manager(entry
.id)
277 reprocess_action
='initial')
278 except ProcessingManagerDoesNotExist
:
279 print 'No such processing manager for {0}'.format(entry
.media_type
)
283 # Run eagerly unless explicetly set not to
285 os
.environ
['CELERY_ALWAYS_EAGER'] = 'true'
287 commands_util
.setup_app(args
)
289 if args
.reprocess_subcommand
== "run":
292 elif args
.reprocess_subcommand
== "available":
295 elif args
.reprocess_subcommand
== "bulk_run":
298 elif args
.reprocess_subcommand
== "thumbs":
301 elif args
.reprocess_subcommand
== "initial":