1#!/usr/bin/env python
2# Copyright (c) 2011 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Finds .png expectations without .checksums or .checksum expectations
31without .pngs."""
32
33import optparse
34import os
35
36from webkitpy.common.checkout import scm
37
38def parse_args():
39    """Returns a 2-tuple (options, paths) where options contains the
40    parsed arguments and paths is a list of directories to search."""
41    option_list = [
42        optparse.make_option("-p", "--missing-pngs", dest="missing_pngs",
43            action="store_true", default=False, help="Only list missing pngs."),
44        optparse.make_option("-c", "--missing-checksums",
45            dest="missing_checksums", action="store_true", default=False,
46            help="Only list missing checksums."),
47    ]
48
49    option_parser = optparse.OptionParser(option_list=option_list,
50                                          description=__doc__)
51    options, paths = option_parser.parse_args()
52    if not options.missing_pngs and not options.missing_checksums:
53        option_parser.error(
54            "Please specify either --missing-pngs or --missing-checksums.")
55
56    if not paths:
57        paths = [os.path.join(scm.find_checkout_root(), 'LayoutTests')]
58    return options, paths
59
60def find_mismatched_results(dirpath, filenames, options):
61    """Prints mismatched results to stdout.
62    dirpath is the directory we are searching
63    filenames is a list of the filenames in dirpath.
64    options is the parsed arguments."""
65    checksum_files = set()
66    png_files = set()
67
68    for filename in filenames:
69        name, extension = os.path.splitext(filename)
70        if not name.endswith("-expected"):
71            continue
72        if extension == ".checksum":
73            checksum_files.add(name)
74        elif extension == ".png":
75            png_files.add(name)
76
77    if options.missing_pngs:
78        missing_pngs = checksum_files.difference(png_files)
79        for missing_png in missing_pngs:
80            print os.path.normpath(os.path.join(dirpath, missing_png)
81                ) + '.checksum'
82
83    if options.missing_checksums:
84        missing_checksums = png_files.difference(checksum_files)
85        for missing_checksum in missing_checksums:
86            print os.path.normpath(os.path.join(dirpath, missing_checksum)
87                ) + '.png'
88
89def main():
90    options, paths = parse_args()
91
92    for path in paths:
93        for dirpath, dirnames, filenames in os.walk(path):
94            find_mismatched_results(dirpath, filenames, options)
95if __name__ == "__main__":
96    main()
97