~osomon/phatch/extract-all-metadata

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
#!/usr/bin/python

# Phatch - Photo Batch Processor
# Copyright (C) 2009 Nadia Alramli, Stani (www.stani.be)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.
#
# Follows PEP8

import logging
import time
import optparse
import sys
import os

from test_suite import config, utils, phatchtools, report


def clean():
    """Clean generated files from previous run"""
    utils.remove_path(options.output)
    utils.remove_path(config.OUT_ACTIONLISTS_PATH)
    utils.remove_path(config.OUT_DIFF)
    utils.create_path(options.output)
    utils.create_path(config.OUT_ACTIONLISTS_PATH)


def set_logger():
    """Setup logging"""
    logging.basicConfig(
        level=logging.INFO,
        format='%(message)s',
        filename=config.DEFAULT_LOG,
        filemode='w'
    )


if __name__ == '__main__':
    # Option parser
    parser = optparse.OptionParser()
    tags = sorted(phatchtools.get_action_tags().keys() + ['library', 'save'])
    parser.add_option(
        '-t', '--tag',
        default=None,
        choices=tags,
        help='Generate tests by tag',
    )
    actions = sorted(phatchtools.get_actions().keys())
    parser.add_option(
        '-s', '--select',
        action="append",
        choices=actions,
        default=None,
        help='Generate selected actions tests',
    )
    parser.add_option(
        '-a', '--all',
        action='store_true',
        default=False,
        help='Generate all tests',
    )
    parser.add_option(
        '-e', '--extended',
        action='store_true',
        default=False,
        help='Generate extended tests',
    )
    parser.add_option(
        '-c', '--compare',
        default=None,
        help='Comparison folder',
    )
    parser.add_option(
        '-i', '--input',
        default=config.DEFAULT_INPUT,
        help='Image input folder [default: %default]',
    )
    parser.add_option(
        '-o', '--output',
        default=config.DEFAULT_OUTPUT,
        help='Image output folder [default: %default]',
    )
    parser.add_option(
        '--no-execute',
        action='store_true',
        default=False,
        help='Generate actionlists only, don\'t execute',
    )
    parser.add_option(
        '--no-clean',
        action='store_true',
        default=False,
        help='Don\'t remove previously generated files',
    )
    parser.add_option(
        '--clean',
        action='store_true',
        default=False,
        help='Remove previously generated files',
    )
    parser.add_option(
        '--options',
        action='store',
        default='',
        help='Command line options to pass to phatch',
    )
    parser.add_option(
        '--report',
        action='store_true',
        default=False,
        help='Generate an html report',
    )

    options, args = parser.parse_args()
    if options.report:
        report.run()
        sys.exit(0)
    if not options.no_execute and not os.path.exists(options.input):
        msg = 'The input directory "%s" is empty or doesn\'t exist'
        logging.error(
            msg % options.input,
        )
        sys.exit(1)
    choices_function = None
    start_time = time.time()
    if not options.no_clean:
        clean()
        if options.clean:
            # Only clean do nothing else
            sys.exit(0)
    set_logger()
    save_action = phatchtools.get_action('save')
    convert_mode_action = phatchtools.get_action('convert_mode')
    actions_by_tag = phatchtools.get_action_tags()
    all_actions = [
        action
        for name, action in phatchtools.get_actions().iteritems()
        if name not in config.DISABLE_ACTIONS]
    processing_actions = [
        action
        for name, action in phatchtools.get_actions().iteritems()
        if name not in config.DISABLE_ACTIONS
        and name not in actions_by_tag['metadata']]
    metadata_actions = actions_by_tag['metadata'].values()
    if options.tag == 'library':
        phatchtools.generate_library_actionlists(options.output)
    elif options.tag == 'save':
        actionlists = [[convert_mode_action, save_action]]
        phatchtools.generate_actionlists(
            options.output, actionlists, include_file_action=True,
        )
    elif options.tag:
        if options.extended and options.tag != 'metadata':
            actionlists = phatchtools.minimal_actionlists(
                actions_by_tag[options.tag].values(),
                save_action,
                [convert_mode_action],
            )
            choices_function = phatchtools.extended_choices
        else:
            actionlists = phatchtools.minimal_actionlists(
                actions_by_tag[options.tag].values(),
                save_action,
            )
        phatchtools.generate_actionlists(
            options.output,
            actionlists,
            choices_function=choices_function,
        )
    if options.select:
        if options.extended:
            actionlists = phatchtools.minimal_actionlists(
                [phatchtools.get_action(name) for name in options.select],
                save_action,
                [convert_mode_action],
            )
            choices_function = phatchtools.extended_choices
        else:
            actionlists = phatchtools.minimal_actionlists(
                [phatchtools.get_action(name) for name in options.select],
                save_action,
            )
        phatchtools.generate_actionlists(
            options.output,
            actionlists,
            choices_function=choices_function,
        )
    if options.all:
        if options.extended:
            #actionlists = phatchtools.minimal_actionlists(
            #    processing_actions, save_action, [convert_mode_action],
            #)
            #actionlists.extend(
            #    phatchtools.minimal_actionlists(
            #       metadata_actions, save_action),
            #)
            actionlists = phatchtools.minimal_actionlists(
                all_actions, save_action,
            )
            choices_function = phatchtools.extended_choices
        else:
            actionlists = phatchtools.minimal_actionlists(
                all_actions, save_action,
            )
        phatchtools.generate_actionlists(
            options.output,
            actionlists,
            choices_function=choices_function,
        )
        if not options.extended:
            actionlists = [[convert_mode_action, save_action]]
            phatchtools.generate_actionlists(
                options.output, actionlists, include_file_action=True,
            )
        phatchtools.generate_library_actionlists(options.output)
    if not options.no_execute:
        errors = phatchtools.execute_actionlists(
            options.input, options=options.options,
        )
        if errors:
            print 'Number of errors: %s' % len(errors)
            print 'Errors:\n\t%s' % '\n\t'.join(errors)
        else:
            print 'No errors'

    for image in os.listdir(options.output):
        path1 = os.path.join(options.output, image)
        if os.path.exists(path1):
            ok, reason = utils.verify_image_type(path1)
            if not ok:
                logging.info('Format/Ext Mismatch: %s' % reason)
    if options.compare:
        utils.create_path(config.OUT_DIFF)
        new = []
        mismatch = []
        output_files = [
            image
            for image in os.listdir(options.output)]
        for image in output_files:
            path1 = os.path.join(options.compare, image)
            if os.path.exists(path1):
                path2 = os.path.join(options.output, image)
                if not utils.compare(path1, path2):
                    result = utils.analyze(path1, path2)
                    logging.info(
                        'Mismatch: %s\nreason: %s' % (image, result['reason']))
                    if 'diff' in result:
                        result['diff'].save(
                            os.path.join(config.OUT_DIFF, image) + '.png')
                    mismatch.append(image)
            else:
                new.append(image)
        if new:
            logging.info('Number of new images: %s' % len(new))
            logging.info('New:\n\t%s' % '\n\t'.join(new))
        if mismatch:
            logging.info('Number of mismatches: %s' % len(mismatch))
            logging.info('Mismatches:\n\t%s' % '\n\t'.join(mismatch))
        if not (new or mismatch):
            logging.info('No difference')
    print 'The log was saved to: %s' % config.DEFAULT_LOG
    print 'Execution took %.2f seconds' % (time.time() - start_time)