REGRESSION (r236481): Move soft-linking of LocalAuthentication.framework out of Local...
[WebKit-https.git] / Tools / Scripts / check-for-duplicated-platform-test-results
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2017 Igalia S.L.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Library General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU Library General Public License
16 # along with this library; see the file COPYING.LIB.  If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
19
20 import hashlib
21 import optparse
22 import os
23 from webkitpy.common.host import Host
24
25 host = Host()
26 host.initialize_scm()
27 checkout_root = host.scm().checkout_root
28 layout_tests_directory = os.path.join(checkout_root, 'LayoutTests')
29 platform_directory = os.path.join(layout_tests_directory, 'platform')
30
31
32 def check_duplicate(platform, baseline_search_path, platform_test_result):
33     def sha1(path):
34         with file(path, 'rb') as f:
35             return hashlib.sha1(f.read()).hexdigest()
36
37     prefix_len = len(os.path.join(platform_directory, platform)) + 1
38     test_result_filename = platform_test_result[prefix_len:]
39     platform_test_result_size = None
40     platform_test_result_sha1 = None
41
42     baseline_found = False
43     for baseline_path in baseline_search_path:
44         baseline_test_result = os.path.join(baseline_path, test_result_filename)
45         if not os.path.isfile(baseline_test_result):
46             continue
47
48         baseline_found = True
49         if platform_test_result_size is None:
50             platform_test_result_size = os.path.getsize(platform_test_result)
51         baseline_test_result_size = os.path.getsize(baseline_test_result)
52         if platform_test_result_size != baseline_test_result_size:
53             return False
54
55         if platform_test_result_sha1 is None:
56             platform_test_result_sha1 = sha1(platform_test_result)
57         baseline_test_result_sha1 = sha1(baseline_test_result)
58         if platform_test_result_sha1 != baseline_test_result_sha1:
59             return False
60
61     return baseline_found
62
63
64 def platform_list(platform):
65     if platform == 'all':
66         return os.listdir(platform_directory)
67     if os.path.isdir(os.path.join(platform_directory, platform)):
68         return [platform]
69     return []
70
71
72 option_parser = optparse.OptionParser(usage='usage: %prog [options]')
73 option_parser.add_option('-p', '--platform',
74                          action='store', dest='platform', default='all',
75                          help='Platform to check for duplicated results. By the default all platform are checked')
76 option_parser.add_option('-n', '--no-delete',
77                          action='store_true', dest='no_delete',
78                          help='Do not delete the duplicated results found from the repository, list them instead')
79 options, args = option_parser.parse_args()
80
81 total = 0
82 baseline_search_paths_checked = set()
83 for platform in platform_list(options.platform):
84     try:
85         port = host.port_factory.get(platform)
86         baseline_search_path = tuple([p for p in port.baseline_search_path() if os.path.isdir(p)] + [layout_tests_directory])
87     except:
88         continue
89
90     if baseline_search_path in baseline_search_paths_checked:
91         continue
92     baseline_search_paths_checked.add(baseline_search_path)
93
94     duplicates = []
95     for root, dirs, files in os.walk(baseline_search_path[0]):
96         for file_name in files:
97             platform_test_result = os.path.join(root, file_name)
98             if check_duplicate(baseline_search_path[0], baseline_search_path[1:], platform_test_result):
99                 duplicates.append(platform_test_result)
100
101     if not duplicates:
102         continue
103     if options.no_delete:
104         print "\n".join(duplicates)
105     else:
106         host.scm().delete_list(duplicates)
107
108     duplicates_len = len(duplicates)
109     total += duplicates_len
110     print '%d found in %s -> generic' % (duplicates_len, ' -> '.join([os.path.basename(p) for p in baseline_search_path[:-1]]))
111
112 if total:
113     if not options.no_delete:
114         print '%d files have been removed from the repository, check the status and commit if everything is correct' % (total)
115 else:
116     print 'No duplicated results found'
117