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/>.
17 from __future__
import print_function
22 from mediagoblin
import mg_globals
23 from mediagoblin
.db
.models
import MediaEntry
24 from mediagoblin
.gmg_commands
import util
as commands_util
25 from mediagoblin
.submit
.lib
import run_process_media
26 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
27 from mediagoblin
.tools
.pluginapi
import hook_handle
28 from mediagoblin
.processing
import (
29 ProcessorDoesNotExist
, ProcessorNotEligible
,
30 get_entry_and_processing_manager
, get_processing_manager_for_type
,
31 ProcessingManagerDoesNotExist
)
34 def reprocess_parser_setup(subparser
):
35 subparser
.add_argument(
38 help="Don't process eagerly, pass off to celery")
40 subparsers
= subparser
.add_subparsers(dest
="reprocess_subcommand")
45 available_parser
= subparsers
.add_parser(
47 help="Find out what actions are available for this media")
49 available_parser
.add_argument(
51 help="Media id or media type to check")
53 available_parser
.add_argument(
56 help="List argument help for each action available")
58 available_parser
.add_argument(
60 help="The state of media you would like to reprocess")
67 run_parser
= subparsers
.add_parser(
69 help="Run a reprocessing on one or more media")
71 run_parser
.add_argument(
73 help="The media_entry id(s) you wish to reprocess.")
75 run_parser
.add_argument(
77 help="The reprocess command you intend to run")
79 run_parser
.add_argument(
81 nargs
=argparse
.REMAINDER
,
82 help="rest of arguments to the reprocessing tool")
88 thumbs
= subparsers
.add_parser(
90 help='Regenerate thumbs for all processed media')
96 metavar
=('max_width', 'max_height'))
101 subparsers
.add_parser(
103 help='Reprocess all failed media')
108 bulk_run_parser
= subparsers
.add_parser(
110 help='Run reprocessing on a given media type or state')
112 bulk_run_parser
.add_argument(
114 help='The type of media you would like to process')
116 bulk_run_parser
.add_argument(
120 help='The state of the media you would like to process. Defaults to' \
123 bulk_run_parser
.add_argument(
125 help='The reprocess command you intend to run')
127 bulk_run_parser
.add_argument(
129 nargs
=argparse
.REMAINDER
,
130 help='The rest of the arguments to the reprocessing tool')
138 # Get the media type, either by looking up media id, or by specific type
140 media_id
= int(args
.id_or_type
)
141 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
142 media_type
= media_entry
.media_type
144 media_type
= args
.id_or_type
146 manager
= get_processing_manager_for_type(media_type
)
147 except ProcessingManagerDoesNotExist
:
148 entry
= MediaEntry
.query
.filter_by(id=args
.id_or_type
).first()
149 print('No such processing manager for {0}'.format(entry
.media_type
))
152 processors
= manager
.list_all_processors_by_state(args
.state
)
153 elif media_entry
is None:
154 processors
= manager
.list_all_processors()
156 processors
= manager
.list_eligible_processors(media_entry
)
158 print("Available processors:")
159 print("=====================")
163 for processor
in processors
:
164 print(processor
.name
)
165 print("-" * len(processor
.name
))
167 parser
= processor
.generate_parser()
172 for processor
in processors
:
173 if processor
.description
:
174 print(" - %s: %s" % (processor
.name
, processor
.description
))
176 print(" - %s" % processor
.name
)
179 def run(args
, media_id
=None):
181 media_id
= args
.media_id
183 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
185 # TODO: (maybe?) This could probably be handled entirely by the
188 processor_class
= manager
.get_processor(
189 args
.reprocess_command
, media_entry
)
190 except ProcessorDoesNotExist
:
191 print('No such processor "%s" for media with id "%s"' % (
192 args
.reprocess_command
, media_entry
.id))
194 except ProcessorNotEligible
:
195 print('Processor "%s" exists but media "%s" is not eligible' % (
196 args
.reprocess_command
, media_entry
.id))
199 reprocess_parser
= processor_class
.generate_parser()
200 reprocess_args
= reprocess_parser
.parse_args(args
.reprocess_args
)
201 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
204 reprocess_action
=args
.reprocess_command
,
205 reprocess_info
=reprocess_request
)
207 except ProcessingManagerDoesNotExist
:
208 entry
= MediaEntry
.query
.filter_by(id=media_id
).first()
209 print('No such processing manager for {0}'.format(entry
.media_type
))
214 Bulk reprocessing of a given media_type
216 query
= MediaEntry
.query
.filter_by(media_type
=args
.type,
225 Regenerate thumbs for all processed media
227 query
= MediaEntry
.query
.filter_by(state
='processed')
231 media_entry
, manager
= get_entry_and_processing_manager(entry
.id)
233 # TODO: (maybe?) This could probably be handled entirely by the
236 processor_class
= manager
.get_processor(
237 'resize', media_entry
)
238 except ProcessorDoesNotExist
:
239 print('No such processor "%s" for media with id "%s"' % (
240 'resize', media_entry
.id))
242 except ProcessorNotEligible
:
243 print('Processor "%s" exists but media "%s" is not eligible' % (
244 'resize', media_entry
.id))
247 reprocess_parser
= processor_class
.generate_parser()
249 # prepare filetype and size to be passed into reprocess_parser
251 extra_args
= 'thumb --{0} {1} {2}'.format(
252 processor_class
.thumb_size
,
258 reprocess_args
= reprocess_parser
.parse_args(extra_args
.split())
259 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
262 reprocess_action
='resize',
263 reprocess_info
=reprocess_request
)
265 except ProcessingManagerDoesNotExist
:
266 print('No such processing manager for {0}'.format(entry
.media_type
))
271 Reprocess all failed media
273 query
= MediaEntry
.query
.filter_by(state
='failed')
277 media_entry
, manager
= get_entry_and_processing_manager(entry
.id)
280 reprocess_action
='initial')
281 except ProcessingManagerDoesNotExist
:
282 print('No such processing manager for {0}'.format(entry
.media_type
))
286 # Run eagerly unless explicetly set not to
288 os
.environ
['CELERY_ALWAYS_EAGER'] = 'true'
290 commands_util
.setup_app(args
)
292 if args
.reprocess_subcommand
== "run":
295 elif args
.reprocess_subcommand
== "available":
298 elif args
.reprocess_subcommand
== "bulk_run":
301 elif args
.reprocess_subcommand
== "thumbs":
304 elif args
.reprocess_subcommand
== "initial":