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")
55 ############################################
56 # run command (TODO: and bulk_run command??)
57 ############################################
59 run_parser
= subparsers
.add_parser(
61 help="Run a reprocessing on one or more media")
63 run_parser
.add_argument(
65 help="Reprocess media entries in this state"
66 " such as 'failed' or 'processed'")
67 run_parser
.add_argument(
69 help="The type of media to be reprocessed such as 'video' or 'image'")
70 run_parser
.add_argument(
73 help="Regenerate thumbnails for all processed media")
75 run_parser
.add_argument(
77 help="The media_entry id(s) you wish to reprocess.")
79 run_parser
.add_argument(
81 help="The reprocess command you intend to run")
83 run_parser
.add_argument(
85 nargs
=argparse
.REMAINDER
,
86 help="rest of arguments to the reprocessing tool")
95 def _set_media_type(args
):
97 This will verify that all media id's are of the same media_type. If the
98 --type flag is set, it will be replaced by the given media id's type.
100 If they are trying to process different media types, an Exception will be
104 if len(args
[0].media_id
) == 1:
105 args
[0].type = MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
106 .first().media_type
.split('.')[-1]
108 elif len(args
[0].media_id
) > 1:
111 for id in args
[0].media_id
:
112 media_types
.append(MediaEntry
.query
.filter_by(id=id).first()
113 .media_type
.split('.')[-1])
114 for type in media_types
:
115 if media_types
[0] != type:
116 raise Exception((u
'You cannot reprocess different'
117 ' media_types at the same time.'))
119 args
[0].type = media_types
[0]
122 def _reprocess_all(args
):
124 This handles reprocessing if no media_id's are given.
127 # If no media type is given, we can either regenerate all thumbnails,
128 # or try to reprocess all failed media
130 if args
[0].thumbnails
:
131 if args
[0].available
:
132 print _('Available options for regenerating all processed'
133 ' media thumbnails: \n'
134 '\t --size: max_width max_height'
135 ' (defaults to config specs)')
137 #TODO regenerate all thumbnails
140 # Reprocess all failed media
141 elif args
[0].state
== 'failed':
142 if args
[0].available
:
143 print _('\n Available reprocess actions for all failed'
144 ' media_entries: \n \t --initial_processing')
146 #TODO reprocess all failed entries
149 # If here, they didn't set the --type flag and were trying to do
150 # something other the generating thumbnails or initial_processing
152 raise Exception(_('You must set --type when trying to reprocess'
153 ' all media_entries, unless you set --state'
157 _run_reprocessing(args
)
160 def _run_reprocessing(args
):
161 # Are they just asking for the available reprocessing options for the given
163 if args
[0].available
:
164 if args
[0].state
== 'failed':
165 print _('\n Available reprocess actions for all failed'
166 ' media_entries: \n \t --initial_processing')
168 result
= hook_handle(('reprocess_action', args
[0].type), args
)
170 print _('Sorry there is no available reprocessing for {}'
171 ' entries in the {} state'.format(args
[0].type,
174 # Run media reprocessing
175 return hook_handle(('media_reprocess', args
[0].type), args
)
178 def _set_media_state(args
):
180 This will verify that all media id's are in the same state. If the
181 --state flag is set, it will be replaced by the given media id's state.
183 If they are trying to process different media states, an Exception will be
187 # Only check if we are given media_ids
188 if len(args
[0].media_id
) == 1:
189 args
[0].state
= MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
192 elif len(args
[0].media_id
) > 1:
195 for id in args
[0].media_id
:
196 media_states
.append(MediaEntry
.query
.filter_by(id=id).first()
199 # Make sure that all media are in the same state
200 for state
in media_states
:
201 if state
!= media_states
[0]:
202 raise Exception(_('You can only reprocess media that is in'
205 args
[0].state
= media_states
[0]
207 # If no state was set, then we will default to the processed state
208 if not args
[0].state
:
209 args
[0].state
= 'processed'
213 # Get the media type, either by looking up media id, or by specific type
215 media_id
= int(args
.id_or_type
)
216 media_entry
, manager
= get_entry_and_processing_manager(media_id
)
217 media_type
= media_entry
.media_type
219 media_type
= args
.id_or_type
221 manager
= get_processing_manager_for_type(media_type
)
223 if media_entry
is None:
224 processors
= manager
.list_all_processors()
226 processors
= manager
.list_eligible_processors(media_entry
)
228 print "Available processors:"
229 print "====================="
233 for processor
in processors
:
235 print "-" * len(processor
.name
)
237 parser
= processor
.generate_parser()
242 for processor
in processors
:
243 if processor
.description
:
244 print " - %s: %s" % (processor
.name
, processor
.description
)
246 print " - %s" % processor
.name
250 media_entry
, manager
= get_entry_and_processing_manager(args
.media_id
)
252 # TODO: (maybe?) This could probably be handled entirely by the
255 processor_class
= manager
.get_processor(
256 args
.reprocess_command
, media_entry
)
257 except ProcessorDoesNotExist
:
258 print 'No such processor "%s" for media with id "%s"' % (
259 args
.reprocess_command
, media_entry
.id)
261 except ProcessorNotEligible
:
262 print 'Processor "%s" exists but media "%s" is not eligible' % (
263 args
.reprocess_command
, media_entry
.id)
266 reprocess_parser
= processor_class
.generate_parser()
267 reprocess_args
= reprocess_parser
.parse_args(args
.reprocess_args
)
268 reprocess_request
= processor_class
.args_to_request(reprocess_args
)
271 reprocess_action
=args
.reprocess_command
,
272 reprocess_info
=reprocess_request
)
276 # Run eagerly unless explicetly set not to
278 os
.environ
['CELERY_ALWAYS_EAGER'] = 'true'
280 commands_util
.setup_app(args
)
282 if args
.reprocess_subcommand
== "run":
285 elif args
.reprocess_subcommand
== "available":