summaryrefslogtreecommitdiff
path: root/solenv/bin/pack_images.py
blob: 5d368658961f7a517d5d89f58312580cfdd1ffa1 (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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# -*- coding: utf-8 -*-
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
""" Pack images into archives. """

from __future__ import with_statement

import argparse
from collections import OrderedDict
import contextlib
import logging
import os
import shutil
import sys
import tempfile
import zipfile


logging.basicConfig(format='%(message)s')
LOGGER = logging.getLogger()


def main(args):
    """ Main function. """

    tmp_output_file = "%s.%d.tmp" % (args.output_file, os.getpid())

    # Sanity checks
    if not os.path.exists(args.imagelist_file):
        LOGGER.error("imagelist_file '%s' doesn't exists", args.imagelist_file)
        sys.exit(2)

    if args.links_file is not None and not os.path.exists(args.links_file):
        LOGGER.error("link_file '%s' doesn't exists", args.links_file)
        sys.exit(2)

    out_path = os.path.dirname(args.output_file)
    for path in (out_path, args.global_path, args.module_path):
        if not os.path.exists(path):
            LOGGER.error("Path '%s' doesn't exists", path)
            sys.exit(2)
        if not os.access(path, os.X_OK):
            LOGGER.error("Unable to search path %s", path)
            sys.exit(2)

    if not os.access(out_path, os.W_OK):
        LOGGER.error("Unable to write into path %s", out_path)

    custom_paths = []
    for path in args.custom_path:
        if not os.path.exists(path):
            LOGGER.warning("Skipping non-existing path: %s", path)
            continue
        elif not os.access(path, os.X_OK):
            LOGGER.error("Unable to search path %s", path)
            sys.exit(2)

        custom_paths.append(path)

    imagelist_filenames = get_imagelist_filenames(args.imagelist_file)
    global_image_list, module_image_list = parse_image_list(imagelist_filenames)
    custom_image_list = find_custom(custom_paths)

    links = {}
    if args.links_file is not None:
        read_links(links, args.links_file)
    else:
        read_links(links, os.path.join(ARGS.global_path, "links.txt"))
        for path in custom_paths:
            read_links(links, os.path.join(path, "links.txt"))
    check_links(links)

    zip_list = create_zip_list(global_image_list, module_image_list, custom_image_list,
                               args.global_path, args.module_path)
    remove_links_from_zip_list(zip_list, links)

    if check_rebuild(args.output_file, imagelist_filenames, custom_paths, zip_list, args.links_file):
        tmp_dir = copy_images(zip_list)
        create_zip_archive(zip_list, links, tmp_dir, tmp_output_file, args.sort_file)

        replace_zip_file(tmp_output_file, args.output_file)
        try:
            LOGGER.info("Remove temporary directory %s", tmp_dir)
            shutil.rmtree(tmp_dir)
        except Exception as e:
            LOGGER.error("Unable to remove temporary directory %s", tmp_dir)
            sys.exit(2)
    else:
        LOGGER.info("No rebuild needed. %s is up to date.", args.output_file)


def check_rebuild(zip_file, imagelist_filenames, custom_paths, zip_list, links_file):
    """ Check if a rebuild is needed.

    :type zip_file: str
    :param zip_file: File to check against (use st_mtime).

    :type imagelist_filenames: dict
    :param imagelist_filenames: List of imagelist filename.

    :type custom_paths: list
    :param custom_paths: List of paths to use with links.txt files.

    :type zip_list: dict
    :param zip_list: List of filenames to create the zip archive.

    :type links_file: str
    :param links_file: filename to read the links from

    :rtype: bool
    :return: True if rebuild is needed and False if not.
    """

    if not os.path.exists(zip_file):
        return True

    zip_file_stat = os.stat(zip_file)

    def compare_modtime(filenames):
        """ Check if modification time of zip archive is older the provided
        list of files.

        :type filenames: dict
        :param filenames: List of filenames to check against.

        :rtype: bool
        :return: True if zip archive is older and False if not.
        """
        for filename, path in filenames.items():
            filename = os.path.join(path, filename) if filename else path
            if os.path.exists(filename) and zip_file_stat.st_mtime < os.stat(filename).st_mtime:
                return True
        return False

    if compare_modtime(imagelist_filenames):
        return True
    if compare_modtime(zip_list):
        return True
    if links_file is not None:
        if zip_file_stat.st_mtime < os.stat(links_file).st_mtime:
            return True
    else:
        for path in custom_paths:
            link_file = os.path.join(path, 'links.txt')
            if os.path.exists(link_file):
                if zip_file_stat.st_mtime < os.stat(link_file).st_mtime:
                    return True

    return False


def replace_zip_file(src, dst):
    """ Replace the old archive file with the newly created one.

    :type src: str
    :param src: Source file name.

    :type dst: str
    :param dst: Destination file name.
    """
    LOGGER.info("Replace old zip archive with new archive")
    if os.path.exists(dst):
        try:
            os.unlink(dst)
        except OSError as e:
            if os.path.exists(src):
                os.unlink(src)

            LOGGER.error("Unable to unlink old archive '%s'", dst)
            LOGGER.error(str(e))
            sys.exit(1)

    try:
        LOGGER.info("Copy archive '%s' to '%s'", src, dst)
        shutil.move(src, dst)
    except (shutil.SameFileError, OSError) as e:
        os.unlink(src)
        LOGGER.error("Cannot copy file '%s' to %s: %s", src, dst, str(e))
        sys.exit(1)


def optimize_zip_layout(zip_list, sort_file=None):
    """ Optimize the zip layout by ordering the list of filename alphabetically
    or with provided sort file (can be partly).

    :type zip_list: dict
    :param zip_list: List of files to include in the zip archive.

    :type sort_file: str
    :param sort_file: Path/Filename to file with sort order.

    :rtype: OrderedDict
    :return: Dict with right sort order.
    """
    if sort_file is None:
        LOGGER.info("No sort file provided")
        LOGGER.info("Sorting entries alphabetically")

        return OrderedDict(sorted(zip_list.items(), key=lambda t: t[0]))

    LOGGER.info("Sort entries from file '%s'", sort_file)
    sorted_zip_list = OrderedDict()
    try:
        fh = open(sort_file)
    except IOError:
        LOGGER.error("Cannot open file %s", sort_file)
        sys.exit(1)
    else:
        with fh:
            for line in fh:
                line = line.strip()
                if line == '' or line.startswith('#'):
                    continue

                if line in zip_list:
                    sorted_zip_list[line] = ''
                else:
                    LOGGER.warning("Unknown file '%s'", line)

    for key in sorted(zip_list.keys()):
        if key not in sorted_zip_list:
            sorted_zip_list[key] = ''

    return sorted_zip_list


def create_zip_archive(zip_list, links, tmp_dir, tmp_zip_file, sort_file=None):
    """ Create the zip archive.

    :type zip_list: dict
    :param zip_list: All filenames to be included in the archive.

    :type links: dict
    :param links: All filenames to create links.txt file.

    :type tmp_dir: str
    :param tmp_dir: Path to temporary saved files.

    :type tmp_zip_file: str
    :param tmp_zip_file: Filename/Path of temporary zip archive.

    :type sort_file: str|None
    :param sort_file: Optional filename with sort order to apply.
    """
    LOGGER.info("Creating image archive")

    old_pwd = os.getcwd()
    os.chdir(tmp_dir)

    ordered_zip_list = optimize_zip_layout(zip_list, sort_file)

    with contextlib.closing(zipfile.ZipFile(tmp_zip_file, 'w')) as tmp_zip:
        if links.keys():
            LOGGER.info("Add file 'links.txt' to zip archive")
            create_links_file(tmp_dir, links)
            tmp_zip.write('links.txt', compress_type=zipfile.ZIP_DEFLATED)

        for link in ordered_zip_list:
            LOGGER.info("Add file '%s' from path '%s' to zip archive", link, tmp_dir)
            try:
                if (link.endswith(".svg")):
                    tmp_zip.write(link, compress_type=zipfile.ZIP_DEFLATED)
                else:
                    tmp_zip.write(link)
            except OSError:
                LOGGER.warning("Unable to add file '%s' to zip archive", link)

    os.chdir(old_pwd)


def create_links_file(path, links):
    """ Create file links.txt. Contains all links.

    :type path: str
    :param path: Path where to create the file.

    :type links: dict
    :param links: Dict with links (source -> target).
    """
    LOGGER.info("Create file links.txt")

    try:
        filename = os.path.join(path, 'links.txt')
        fh = open(filename, 'w')
    except IOError:
        LOGGER.error("Cannot open file %s", filename)
        sys.exit(1)
    else:
        with fh:
            for key in sorted(links.keys()):
                fh.write("%s %s\n" % (key, links[key]))


def copy_images(zip_list):
    """ Create a temporary directory and copy images to that directory.

    :type zip_list: dict
    :param zip_list: Dict with all files.

    :rtype: str
    :return: Path of the temporary directory.
    """
    LOGGER.info("Copy image files to temporary directory")

    tmp_dir = tempfile.mkdtemp()
    for key, value in zip_list.items():
        path = os.path.join(value, key)
        out_path = os.path.join(tmp_dir, key)

        LOGGER.debug("Copying '%s' to '%s'", path, out_path)
        if os.path.exists(path):
            dirname = os.path.dirname(out_path)
            if not os.path.exists(dirname):
                os.makedirs(dirname)

            try:
                shutil.copyfile(path, out_path)
            except (shutil.SameFileError, OSError) as e:
                LOGGER.error("Cannot add file '%s' to image dir: %s", path, str(e))
                sys.exit(1)

    LOGGER.debug("Temporary directory '%s'", tmp_dir)
    return tmp_dir


def remove_links_from_zip_list(zip_list, links):
    """ Remove any files from zip list that are linked.

    :type zip_list: dict
    :param zip_list: Files to include in the zip archive.

    :type links: dict
    :param links: Images which are linked.
    """
    LOGGER.info("Remove linked files from zip list")

    for link in links.keys():
        if link in zip_list:
            del zip_list[link]

    LOGGER.debug("Cleaned zip list:\n%s", "\n".join(zip_list))


def create_zip_list(global_image_list, module_image_list, custom_image_list, global_path, module_path):
    """ Create list of images for the zip archive.

    :type global_image_list: dict
    :param global_image_list: Global images.

    :type module_image_list: dict
    :param module_image_list: Module images.

    :type custom_image_list: dict
    :param custom_image_list: Custom images.

    :type global_path: str
    :param global_path: Global path (use when no custom path is available).

    :type module_path: str
    :param module_path: Module path (use when no custom path is available).

    :rtype: dict
    :return: List of images to include in zip archive.
    """
    LOGGER.info("Assemble image list")

    zip_list = {}
    duplicates = []

    for key in global_image_list.keys():
        if key in module_image_list:
            duplicates.append(key)
            continue

        if key in custom_image_list:
            zip_list[key] = custom_image_list[key]
            continue

        zip_list[key] = global_path

    for key in module_image_list.keys():
        if key in custom_image_list:
            zip_list[key] = custom_image_list[key]
            continue

        zip_list[key] = module_path

    if duplicates:
        LOGGER.warning("Found duplicate entries in 'global' and 'module' list")
        LOGGER.warning("\n".join(duplicates))

    LOGGER.debug("Assembled image list:\n%s", "\n".join(zip_list))
    return zip_list


def check_links(links):
    """ Check that no link points to another link.

    :type links: dict
    :param links: Links to icons
    """

    stop = False

    for link, target in links.items():
        if target in links.keys():
            LOGGER.error("Link %s -> %s -> %s", link, target, links[target])
            stop = True

    if stop:
        LOGGER.error("Some icons in links.txt were found to link to other linked icons")
        sys.exit(1)


def read_links(links, filename):
    """ Read links from file.

    :type links: dict
    :param links: Hash to store all links

    :type filename: str
    :param filename: filename to read the links from
    """

    LOGGER.info("Read links from file '%s'", filename)
    if not os.path.isfile(filename):
        LOGGER.info("No file to read")
        return

    try:
        fh = open(filename)
    except IOError:
        LOGGER.error("Cannot open file %s", filename)
        sys.exit(1)
    else:
        with fh:
            for line in fh:
                line = line.strip()
                if line == '' or line.startswith('#'):
                    continue

                tmp = line.split(' ')
                if len(tmp) == 2:
                    links[tmp[0]] = tmp[1]
                else:
                    LOGGER.error("Malformed links line '%s' in file %s", line, filename)
                    sys.exit(1)

    LOGGER.debug("Read links:\n%s", "\n".join(links))


def find_custom(custom_paths):
    """ Find all custom images.

    :type custom_paths: list
    :param custom_paths: List of custom paths to search within.

    :rtype: dict
    :return: List of all custom images.
    """
    LOGGER.info("Find all images in custom paths")

    custom_image_list = {}
    for path in custom_paths:
        # find all png files under the path including subdirs
        custom_files = [val for sublist in [
            [os.path.join(i[0], j) for j in i[2]
             if j.endswith('.png') and os.path.isfile(os.path.join(i[0], j))]
            for i in os.walk(path)] for val in sublist]

        for filename in custom_files:
            if filename.startswith(path):
                key = filename.replace(os.path.join(path, ''), '')
                if key not in custom_image_list:
                    custom_image_list[key] = path

    LOGGER.debug("Custom image list:\n%s", "\n".join(custom_image_list.keys()))
    return custom_image_list


def parse_image_list(imagelist_filenames):
    """ Parse and extract filename from the imagelist files.

    :type imagelist_filenames: list
    :param imagelist_filenames: List of imagelist files.

    :rtype: tuple
    :return: Tuple with dicts containing the global or module image list.
    """

    global_image_list = {}
    module_image_list = {}
    for imagelist_filename in imagelist_filenames.keys():
        LOGGER.info("Parsing '%s'", imagelist_filename)

        try:
            fh = open(imagelist_filename)
        except IOError as e:
            LOGGER.error("Cannot open imagelist file %s", imagelist_filename)
            sys.exit(2)
        else:
            line_count = 0
            with fh:
                for line in fh:
                    line_count += 1
                    line = line.strip()
                    if line == '' or line.startswith('#'):
                        continue
                    # clean up backslashes and double slashes
                    line = line.replace('\\', '/')
                    line = line.replace('//', '/')

                    if line.startswith('%GLOBALRES%'):
                        key = "res/%s" % line.replace('%GLOBALRES%/', '')
                        global_image_list[key] = True
                        if key.endswith('.png'):
                            global_image_list[key[:-4] + '.svg'] = True
                        continue

                    if line.startswith('%MODULE%'):
                        key = line.replace('%MODULE%/', '')
                        module_image_list[key] = True
                        if key.endswith('.png'):
                            module_image_list[key[:-4] + '.svg'] = True
                        continue

                    LOGGER.error("Cannot parse line %s:%d", imagelist_filename, line_count)
                    sys.exit(1)

    LOGGER.debug("Global image list:\n%s", "\n".join(global_image_list))
    LOGGER.debug("Module image list:\n%s", "\n".join(module_image_list))
    return global_image_list, module_image_list


def get_imagelist_filenames(filename):
    """ Extract a list of imagelist filenames.

    :type filename: str
    :param filename: Name of file from extracting the list.

    :rtype: dict
    :return: List of imagelist filenames.
    """
    LOGGER.info("Extract the imagelist filenames")

    imagelist_filenames = {}
    try:
        fh = open(filename)
    except IOError:
        LOGGER.error("Cannot open imagelist file %s", filename)
        sys.exit(1)
    else:
        with fh:
            for line in fh:
                line = line.strip()
                if not line or line == '':
                    continue

                for line_split in line.split(' '):
                    line_split.strip()
                    imagelist_filenames[line_split] = ''

    LOGGER.debug("Extracted imagelist filenames:\n%s", "\n".join(imagelist_filenames.keys()))
    return imagelist_filenames


if __name__ == '__main__':
    parser = argparse.ArgumentParser("Pack images into archives")
    parser.add_argument('-o', '--output-file', dest='output_file',
                        action='store', required=True,
                        help='path to output archive')
    parser.add_argument('-l', '--imagelist-file', dest='imagelist_file',
                        action='store', required=True,
                        help='file containing list of image list file')
    parser.add_argument('-L', '--links-file', dest='links_file',
                        action='store', required=False,
                        help='file containing linked images')
    parser.add_argument('-s', '--sort-file', dest='sort_file',
                        action='store', required=True, default=None,
                        help='image sort order file')
    parser.add_argument('-c', '--custom-path', dest='custom_path',
                        action='append', required=True,
                        help='path to custom path directory')
    parser.add_argument('-g', '--global-path', dest='global_path',
                        action='store', required=True,
                        help='path to global images directory')
    parser.add_argument('-m', '--module-path', dest='module_path',
                        action='store', required=True,
                        help='path to module images directory')
    parser.add_argument('-v', '--verbose', dest='verbose',
                        action='count', default=0,
                        help='set the verbosity (can be used multiple times)')

    ARGS = parser.parse_args()
    LOGGER.setLevel(logging.ERROR - (10 * ARGS.verbose if ARGS.verbose <= 3 else 3))

    main(ARGS)
    sys.exit(0)

# vim: set shiftwidth=4 softtabstop=4 expandtab: