~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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python

# Phatch - Photo Batch Processor
# Copyright (C) 2007-2009  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/

# Linux packagers: please patch lib/linux/path.py, not here!

import time
time_start = time.time()

import sys

NO_WINDOWS = '''
Sorry, the use of 'setup.py install' is not supported yet for Windows.
(You can however use setup.py with py2exe.)
If you want to use Phatch, just run 'python app.py' from the source folder.
Please read the instructions at http://photobatch.wikidot.com/install#toc8
'''

import glob
import os
import shutil
import subprocess
import zipfile

from distutils.core import setup

import phatch  # fix sys.path

from core import info
from lib.context import get_context

CONTEXT = get_context('phatch', base_source=None, sys_prefix='',
    user=False)

if CONTEXT['context'] == ('windows', 'package'):
    # Do not allow setup.py install as we don't know where to install
    # the data, doc and locale path.
    sys.exit(NO_WINDOWS)

#centralised info generates README and AUTHORS
#Temporarily execute the following statement if these files needs update.
#info.write_readme_credits()

write = sys.stdout.write
error = sys.stderr.write


def zip_folder(start_dir, archive):
    z = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
    for dirpath, dirnames, filenames in os.walk(start_dir):
        for filename in filenames:
            full = os.path.join(dirpath, filename)
            arc = full.replace(start_dir, '')
            print('Zipping: %s' % arc)
            z.write(full, arc)
    z.close()


def files(*args):
    return glob.glob(os.path.join(*args))


def doc(path=''):
    return os.path.join(CONTEXT['app_doc_path'], path)


def data(path='', *paths):
    return os.path.join(CONTEXT['app_data_path'], path, *paths)


# manifest
# http://pythonhaven.wordpress.com/2009/10/07/
# how-to-get-xp-styles-on-wxpython-apps-made-with-py2exe/
manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<assemblyIdentity
    version="0.64.1.0"
    processorArchitecture="x86"
    name="Controls"
    type="win32"
/>
<description>Phatch Photo Batch Processor</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="X86"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>
"""

classifiers = [
    'Development Status :: 4 - Beta',
    'Environment :: Console',
    'Environment :: MacOS X',
    'Environment :: Win32 (MS Windows)',
    'Environment :: X11 Applications',
    'Environment :: X11 Applications :: Gnome',
    'Environment :: X11 Applications :: GTK',
    'Intended Audience :: Developers',
    'Intended Audience :: End Users/Desktop',
    'License :: OSI Approved :: GNU General Public License (GPL)',
    'Natural Language :: Dutch',
    'Natural Language :: English',
    'Operating System :: MacOS :: MacOS X',
    'Operating System :: Microsoft :: Windows',
    'Operating System :: OS Independent',
    'Operating System :: POSIX',
    'Operating System :: POSIX :: Linux',
    'Programming Language :: Python',
    'Topic :: Artistic Software',
    'Topic :: Multimedia :: Graphics',
    'Topic :: Multimedia :: Graphics :: Graphics Conversion',
]


packages = [
    'phatch',
    'phatch.actions',
    'phatch.console',
    'phatch.core',
    'phatch.lib', 'phatch.lib.pyWx',
    'phatch.other', 'phatch.other.pyWx',
    'phatch.pyWx', 'phatch.pyWx.wxGlade',
]
if not CONTEXT['distribute'] == 'frozen':
    packages += ['phatch.other.pil_1_1_6']

#Create an array with all the locale filenames
i18n_files = []
for filepath in files('locale', '*', 'LC_MESSAGES', 'phatch.mo'):
    targetpath = os.path.dirname(os.path.join(
        CONTEXT['app_locale_path'],
        filepath,
    ))
    i18n_files.append((targetpath, [filepath]))

#doc
doc_files = [
    (doc(),
        files('doc', 'build', 'html', '*.html')),
    (doc('_static'),
        files('doc', 'build', 'html', '_static', '*')),
    (doc('_sources'),
        files('doc', 'build', 'html', 'sources', '*.txt')),
]


#data
data_files = [
    (data(),
        [os.path.join('data', 'geek.txt')],
    ),  # eg geek.txt
    (data('actionlists'),
        files('data', 'actionlists', '*.phatch'),
    ),
    (data('blender'),
        files('data', 'blender', '*.blend') + \
        files('data', 'blender', '*.py'),
    ),
    (data('fonts'),
        files('data', 'fonts', '*.ttf'),
    ),
    (data('gui', 'preview'),
        [os.path.join('data', 'gui', 'preview', 'user.png')],
    ),
    (data('gui', 'preview', 'actionlists'),
        files('data', 'gui', 'preview', 'actionlists', '*.png'),
    ),
    (data('gui', 'preview', 'blender', 'object'),
        files('data', 'gui', 'preview', 'blender', 'object', '*.jpg'),
    ),
    (data('gui', 'preview', 'perspective'),
        files('data', 'gui', 'preview', 'perspective', '*.png'),
    ),
    # use 48px for notifications, ...
    # TODO Nadia: check if this doesn't look pixelated on Mac Dock
    (data('images', 'icons'),
        files('setup', 'images', 'icons', '48x48', '*.png'),
    ),
    (data('images', 'highlights'),
        files('data', 'images', 'highlights', '*.png'),
    ),
    (data('images', 'masks'),
        files('data', 'images', 'masks', '*.png') + \
        files('data', 'images', 'masks', '*.jpg')
    ),
    (data('images', 'watermarks'),
        files('data', 'images', 'watermarks', '*.png'),
    ),
]


# rotation previews of blender objects
blender_objects = ('book', 'box', 'can', 'cd', 'lcd', 'sphere')
for ob in blender_objects:
    data_files.append((
        data('gui', 'preview', 'blender', 'rotation', ob),
            files('data', 'gui', 'preview', 'blender', 'rotation', ob,
                '*.jpg')))


# os specific files

extra_options = {}

if CONTEXT['windows']:

    packages += ['phatch.lib.windows', 'phatch.pyWx.windows']
    os_files = [
        # phatch specific windows icon
        (data('images', 'icons'), [
            'setup\\images\\icons\\256x256\\phatch.ico',
            'setup\\images\\icons\\256x256\\phatch-inspector.ico',
        ]),
    ]
    if os.path.exists('..\\bin'):
        bin_files = [
            ('bin', glob.glob('..\\bin\\*.*')),
            ('bin\\blender',
                glob.glob(os.path.abspath('..\\bin\\blender\\*'))),
            ('bin\\imagemagick',
                glob.glob(os.path.abspath('..\\bin\\imagemagick\\*'))),
        ]
        os_files += bin_files
    actions = [
        'actions.%s' % os.path.splitext(name)[0]
        for name in os.listdir('phatch\\actions') if name.endswith('.py')
    ]
    extra_options['windows'] = [
        {
            'script': 'phatch/app.py',
            'dest_base': 'phatch',
            'icon_resources': [
                (0, 'setup\\images\\icons\\256x256\\phatch.ico'),
                (1, 'setup\\images\\icons\\256x256\\phatch-inspector.ico'),
                (2, 'setup\\images\\icons\\256x256\\phatch-actionlist.ico'),
            ],
            'other_resources': [(24, 1, manifest)],
        }
    ]

    if CONTEXT['distribute'] == 'frozen':

        import py2exe
        from lib import imtools, system, openImage
        system.set_bin_paths([
            os.path.dirname(files[0])
            for path, files in bin_files
            if files
        ])
        openImage.register()

        exe_dir = os.path.join('..', 'exe')
        inno_dir = os.path.join(exe_dir, 'innosetup')
        py2exe_dir = os.path.join(exe_dir, 'py2exe')

        # clean up previous build
        for d in (inno_dir, py2exe_dir):
            if os.path.exists(d):
                shutil.rmtree(d)
        system.ensure_path(exe_dir)

        # copy innosetup files
        phatch_iss = os.path.join(exe_dir, 'phatch.iss')
        shutil.copy2('setup\\windows\\phatch.iss', phatch_iss)
        shutil.copytree('setup\\windows\\innosetup', inno_dir)

        # copy dlls
        dll_path = '..\\dll'
        if os.path.exists(dll_path):
            shutil.copytree(dll_path, py2exe_dir)

        # py2exe options
        extra_options['options'] = {
            'py2exe': {
                'skip_archive': True,
                'dist_dir': py2exe_dir,
                #'optimize': 2, does not seem to work
                'includes': ['pyWx.images'] + actions,
                'excludes': [
                    #'ctypes', needed for sysArgvUnicode.fix()
                    'difflib',
                    'doctest',
                    'cookielib',
                    'email',
                    'ftplib',
                    '_imagingtk',
                    #'inspect', do NOT exclude
                    'pdb',
                    'pyreadline',
                    'pywin',
                    'pywin.debugger',
                    'pywin.debugger.dbgcon',
                    'pywin.dialogs',
                    'pywin.dialogs.list',
                    'readline',
                    '_ssl',
                    'Tkconstants',
                    'Tkinter',
                    'tcl',
                    'unittest',
                    #'win32ui',  # needed for recentFiles.py
                ],
                'dll_excludes': [
                    '_imagingtk.pyd',
                    'pythoncom25.dll',
                    'tcl85.dll', 'tk85.dll',
                ],
            },
        }

elif CONTEXT['apple']:
    # FIXME: What about python setup.py install?
    import py2app
    plist = {
        'CFBundleDocumentTypes': [{
        'CFBundleTypeExtensions': ['phatch'],
        'CFBundleTypeName': 'Phatch Action List',
        'CFBundleTypeRole': 'Viewer',
        'CFBundleTypeIconFile': 'phatch.icns',
         }],
        'CFBundleGetInfoString': 'Phatch',
    }
    actions = [
        'actions.%s' % os.path.splitext(name)[0]
        for name in os.listdir('phatch/actions') if name.endswith('.py')
    ]
    extra_options['app'] = ['phatch/app.py']
    extra_options['options'] = {
        'py2app': {
             'argv_emulation': True,
             'iconfile': 'setup/images/icons/scalable/phatch.icns',
             'plist': plist,
             'includes': actions,
         }
    }
    os_files = [
        #man page
        #('/usr/share/man/man1', ['setup/linux/phatch.1']),
        (data('actions'), files('phatch', 'actions', '*.py')),
        (data('bin'), files('bin', '*.*')),
    ]
else:
    #linux
    packages += ['phatch.lib.linux', 'phatch.pyWx.linux']
    os_files = [
        #man page
        ('share/man/man1',
            ['setup/linux/phatch.1']),
        # desktop shortcut
        ('share/applications',
            files('setup', 'linux', '*.desktop')),
        # mime type
        ('share/mime/packages',
            ['setup/linux/phatch.xml']),
    ]
    # apps icons
    icon_sizes = [
        '%dx%d' % (x, x)
        for x in (16, 24, 32, 48, 64, 96, 128, 192, 256)
    ]
    icons = \
        [
            ('share/icons/hicolor/%s/apps' % x,
                files('setup', 'images', 'icons', x, '*.png'))
            for x in icon_sizes
        ] + \
        [
            ('share/icons/hicolor/scalable/apps',
                files('setup', 'images', 'icons', 'scalable', 'phatch*.svg')),
            ('share/pixmaps',
                files('setup', 'images', 'icons', '256x256', 'phatch*.png')),
        ]
    # mime icons
    icon_sizes = [
        '%dx%d' % (x, x)
        for x in (24, 48, 64, 128, 256)
    ]
    icons += \
        [
            ('share/icons/hicolor/%s/mimetypes' % x,
                files('setup', 'images', 'icons', x, 'x-*.png'))
            for x in icon_sizes
        ] + \
        [
            ('share/icons/hicolor/scalable/mimetypes',
                files('setup', 'images', 'icons', 'scalable', 'x-*.svg')),
            ('share/pixmaps',
                files('setup', 'images', 'icons', '256x256', 'x-*.png')),
        ]
    os_files.extend(icons)


# setup options
setup_options = {
    'packages': packages,
    'scripts': ['bin/phatch'],
    'data_files': i18n_files + doc_files + data_files + os_files,
    'classifiers': classifiers,
}
setup_options.update(info.SETUP)
setup_options.update(extra_options)


if __name__ == '__main__':
    #run the setup
    dist = setup(**setup_options)

    # disabled: distro maintainers do the right thing
    # non distro users, who don't use packages at all,
    # might need to enable it
    if CONTEXT['linux'] and 0:
        # Update the mime types
        ROOT = os.geteuid() == 0
        if ROOT and dist != None:

            #update the mimetypes database -> associate.phatch
            try:
                subprocess.call(["update-mime-database",
                    os.path.join(sys.prefix, "share/mime/")])
                write('Updating the mime types database.\n')
            except:
                error('Failed to update the mime types database.\n')

            #update the .desktop file database -> application menu
            try:
                subprocess.call(["update-desktop-database"])
                write('Updating the .desktop file database.\n')
            except:
                error('Failed to update the .desktop file database.\n')

    elif CONTEXT['context'] == ('windows', 'frozen'):
        installer_dir = os.path.join(exe_dir, 'installer')
        for path in ('build', installer_dir):
            if os.path.isdir(path):
                shutil.rmtree(path)
        # Zip the py2exe folder
        system.ensure_path(installer_dir)
        zip_folder(
            py2exe_dir,
            os.path.join(installer_dir, 'phatch-win.zip'),
        )
        # do NOT use ISCC.exe or on Windows 2000 we get this error:
        # Error 193: %1 is not a valid Win32 application
        # http://jrsoftware.innosetup.free-usenet.eu/
        # Error-193-1-is-not-a-valid-Win32-application_T50622284_S2
        subprocess.call(['Compil32.exe', '/cc', phatch_iss])
        # Test if the executables launch
        subprocess.Popen([os.path.join(py2exe_dir, 'phatch.exe')])
        subprocess.Popen([os.path.join(exe_dir, 'installer', 'Setup.exe')])

    if CONTEXT['distribute'] == 'package':
        write("\nInstallation finished! "
            "You can now run Phatch by typing 'phatch' \n"
            "or through your applications menu.\n")

    write('Done in %.2fs.' % (time.time() - time_start))