aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/reprocess.py
blob: 0c1b03e1fe1e9279c698ad37d95f3f8019052d1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import argparse
import os

from mediagoblin import mg_globals
from mediagoblin.db.models import MediaEntry
from mediagoblin.gmg_commands import util as commands_util
from mediagoblin.submit.lib import run_process_media
from mediagoblin.processing import (
    ProcessorDoesNotExist, ProcessorNotEligible,
    get_entry_and_processing_manager, get_processing_manager_for_type,
    ProcessingManagerDoesNotExist)


def reprocess_parser_setup(subparser):
    subparser.add_argument(
        '--celery',
        action='store_true',
        help="Don't process eagerly, pass off to celery")

    subparsers = subparser.add_subparsers(dest="reprocess_subcommand")

    ###################
    # available command
    ###################
    available_parser = subparsers.add_parser(
        "available",
        help="Find out what actions are available for this media")

    available_parser.add_argument(
        "id_or_type",
        help="Media id or media type to check")

    available_parser.add_argument(
        "--action-help",
        action="store_true",
        help="List argument help for each action available")

    available_parser.add_argument(
        "--state",
        help="The state of media you would like to reprocess")


    #############
    # run command
    #############

    run_parser = subparsers.add_parser(
        "run",
        help="Run a reprocessing on one or more media")

    run_parser.add_argument(
        'media_id',
        help="The media_entry id(s) you wish to reprocess.")

    run_parser.add_argument(
        'reprocess_command',
        help="The reprocess command you intend to run")

    run_parser.add_argument(
        'reprocess_args',
        nargs=argparse.REMAINDER,
        help="rest of arguments to the reprocessing tool")


    ################
    # thumbs command
    ################
    thumbs = subparsers.add_parser(
        'thumbs',
        help='Regenerate thumbs for all processed media')

    thumbs.add_argument(
        '--size',
        nargs=2,
        type=int,
        metavar=('max_width', 'max_height'))

    #################
    # initial command
    #################
    subparsers.add_parser(
        'initial',
        help='Reprocess all failed media')

    ##################
    # bulk_run command
    ##################
    bulk_run_parser = subparsers.add_parser(
        'bulk_run',
        help='Run reprocessing on a given media type or state')

    bulk_run_parser.add_argument(
        'type',
        help='The type of media you would like to process')

    bulk_run_parser.add_argument(
        '--state',
        default='processed',
        nargs='?',
        help='The state of the media you would like to process. Defaults to' \
             " 'processed'")

    bulk_run_parser.add_argument(
        'reprocess_command',
        help='The reprocess command you intend to run')

    bulk_run_parser.add_argument(
        'reprocess_args',
        nargs=argparse.REMAINDER,
        help='The rest of the arguments to the reprocessing tool')

    ###############
    # help command?
    ###############


def available(args):
    # Get the media type, either by looking up media id, or by specific type
    try:
        media_id = int(args.id_or_type)
        media_entry, manager = get_entry_and_processing_manager(media_id)
        media_type = media_entry.media_type
    except ValueError:
        media_type = args.id_or_type
        media_entry = None
        manager = get_processing_manager_for_type(media_type)
    except ProcessingManagerDoesNotExist:
        entry = MediaEntry.query.filter_by(id=args.id_or_type).first()
        print(f'No such processing manager for {entry.media_type}')

    if args.state:
        processors = manager.list_all_processors_by_state(args.state)
    elif media_entry is None:
        processors = manager.list_all_processors()
    else:
        processors = manager.list_eligible_processors(media_entry)

    print("Available processors:")
    print("=====================")
    print("")

    if args.action_help:
        for processor in processors:
            print(processor.name)
            print("-" * len(processor.name))

            parser = processor.generate_parser()
            parser.print_help()
            print("")

    else:
        for processor in processors:
            if processor.description:
                print(f" - {processor.name}: {processor.description}")
            else:
                print(" - %s" % processor.name)


def run(args, media_id=None):
    if not media_id:
        media_id = args.media_id
    try:
        media_entry, manager = get_entry_and_processing_manager(media_id)

        # TODO: (maybe?) This could probably be handled entirely by the
        # processor class...
        try:
            processor_class = manager.get_processor(
                args.reprocess_command, media_entry)
        except ProcessorDoesNotExist:
            print('No such processor "{}" for media with id "{}"'.format(
                args.reprocess_command, media_entry.id))
            return
        except ProcessorNotEligible:
            print('Processor "{}" exists but media "{}" is not eligible'.format(
                args.reprocess_command, media_entry.id))
            return

        reprocess_parser = processor_class.generate_parser()
        reprocess_args = reprocess_parser.parse_args(args.reprocess_args)
        reprocess_request = processor_class.args_to_request(reprocess_args)
        run_process_media(
            media_entry,
            reprocess_action=args.reprocess_command,
            reprocess_info=reprocess_request)

    except ProcessingManagerDoesNotExist:
        entry = MediaEntry.query.filter_by(id=media_id).first()
        print(f'No such processing manager for {entry.media_type}')


def bulk_run(args):
    """
    Bulk reprocessing of a given media_type
    """
    query = MediaEntry.query.filter_by(media_type=args.type,
                                       state=args.state)

    for entry in query:
        run(args, entry.id)


def thumbs(args):
    """
    Regenerate thumbs for all processed media
    """
    query = MediaEntry.query.filter_by(state='processed')

    for entry in query:
        try:
            media_entry, manager = get_entry_and_processing_manager(entry.id)

            # TODO: (maybe?) This could probably be handled entirely by the
            # processor class...
            try:
                processor_class = manager.get_processor(
                    'resize', media_entry)
            except ProcessorDoesNotExist:
                print('No such processor "{}" for media with id "{}"'.format(
                    'resize', media_entry.id))
                return
            except ProcessorNotEligible:
                print('Processor "{}" exists but media "{}" is not eligible'.format(
                    'resize', media_entry.id))
                return

            reprocess_parser = processor_class.generate_parser()

            # prepare filetype and size to be passed into reprocess_parser
            if args.size:
                extra_args = 'thumb --{} {} {}'.format(
                    processor_class.thumb_size,
                    args.size[0],
                    args.size[1])
            else:
                extra_args = 'thumb'

            reprocess_args = reprocess_parser.parse_args(extra_args.split())
            reprocess_request = processor_class.args_to_request(reprocess_args)
            run_process_media(
                media_entry,
                reprocess_action='resize',
                reprocess_info=reprocess_request)

        except ProcessingManagerDoesNotExist:
            print(f'No such processing manager for {entry.media_type}')


def initial(args):
    """
    Reprocess all failed media
    """
    query = MediaEntry.query.filter_by(state='failed')

    for entry in query:
        try:
            media_entry, manager = get_entry_and_processing_manager(entry.id)
            run_process_media(
                media_entry,
                reprocess_action='initial')
        except ProcessingManagerDoesNotExist:
            print(f'No such processing manager for {entry.media_type}')


def reprocess(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    commands_util.setup_app(args)

    if args.reprocess_subcommand == "run":
        run(args)

    elif args.reprocess_subcommand == "available":
        available(args)

    elif args.reprocess_subcommand == "bulk_run":
        bulk_run(args)

    elif args.reprocess_subcommand == "thumbs":
        thumbs(args)

    elif args.reprocess_subcommand == "initial":
        initial(args)