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 subparsers
= subparser
.add_subparsers(dest
="reprocess_subcommand")
36 available_parser
= subparsers
.add_parser(
38 help="Find out what actions are available for this media")
40 available_parser
.add_argument(
42 help="Media id or media type to check")
44 available_parser
.add_argument(
47 help="List argument help for each action available")
50 ############################################
51 # run command (TODO: and bulk_run command??)
52 ############################################
54 run_parser
= subparsers
.add_parser(
56 help="Run a reprocessing on one or more media")
58 run_parser
.add_argument(
60 help="Reprocess media entries in this state"
61 " such as 'failed' or 'processed'")
62 run_parser
.add_argument(
64 help="The type of media to be reprocessed such as 'video' or 'image'")
65 run_parser
.add_argument(
68 help="Regenerate thumbnails for all processed media")
69 run_parser
.add_argument(
72 help="Don't process eagerly, pass off to celery")
74 run_parser
.add_argument(
76 help="The media_entry id(s) you wish to reprocess.")
78 run_parser
.add_argument(
80 help="The reprocess command you intend to run")
82 run_parser
.add_argument(
84 nargs
=argparse
.REMAINDER
,
85 help="rest of arguments to the reprocessing tool")
94 def _set_media_type(args
):
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.
99 If they are trying to process different media types, an Exception will be
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]
107 elif len(args
[0].media_id
) > 1:
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.'))
118 args
[0].type = media_types
[0]
121 def _reprocess_all(args
):
123 This handles reprocessing if no media_id's are given.
126 # If no media type is given, we can either regenerate all thumbnails,
127 # or try to reprocess all failed media
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)')
136 #TODO regenerate all thumbnails
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')
145 #TODO reprocess all failed entries
148 # If here, they didn't set the --type flag and were trying to do
149 # something other the generating thumbnails or initial_processing
151 raise Exception(_('You must set --type when trying to reprocess'
152 ' all media_entries, unless you set --state'
156 _run_reprocessing(args
)
159 def _run_reprocessing(args
):
160 # Are they just asking for the available reprocessing options for the given
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')
167 result
= hook_handle(('reprocess_action', args
[0].type), args
)
169 print _('Sorry there is no available reprocessing for {}'
170 ' entries in the {} state'.format(args
[0].type,
173 # Run media reprocessing
174 return hook_handle(('media_reprocess', args
[0].type), args
)
177 def _set_media_state(args
):
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.
182 If they are trying to process different media states, an Exception will be
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])\
191 elif len(args
[0].media_id
) > 1:
194 for id in args
[0].media_id
:
195 media_states
.append(MediaEntry
.query
.filter_by(id=id).first()
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'
204 args
[0].state
= media_states
[0]
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'
212 # Get the media type, either by looking up media id, or by specific type
214 media_entry
, manager
= get_entry_and_processing_manager(args
.id_or_type
)
215 media_type
= media_entry
.type
217 media_type
= args
.id_or_type
219 manager
= get_processing_manager_for_type(media_type
)
221 if media_entry
is None:
222 processors
= manager
.list_all_processors()
224 processors
= manager
.list_eligible_processors(media_entry
)
226 print "Available processors:"
227 print "====================="
231 for processor
in processors
:
233 print "-" * len(processor
.name
)
235 parser
= processor
.generate_parser()
240 for processor
in processors
:
241 if processor
.description
:
242 print " - %s: %s" % (processor
.name
, processor
.description
)
244 print " - %s" % processor
.name
248 media_entry
, manager
= get_entry_and_processing_manager(args
.media_id
)
250 # TODO: (maybe?) This could probably be handled entirely by the
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)
259 except ProcessorNotEligible
:
260 print 'Processor "%s" exists but media "%s" is not eligible' % (
261 args
.reprocess_command
, media_entry
.id)
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
)
269 reprocess_action
=args
.reprocess_command
,
270 reprocess_info
=reprocess_request
)
274 # Run eagerly unless explicetly set not to
276 os
.environ
['CELERY_ALWAYS_EAGER'] = 'true'
278 commands_util
.setup_app(args
)
280 if args
.reprocess_subcommand
== "run":
283 elif args
.reprocess_subcommand
== "available":