Refresh wpt tests up to d686014
[WebKit-https.git] / LayoutTests / imported / w3c / web-platform-tests / service-workers / tools / blink-import.py
1 import os
2 import re
3 import shutil
4 import glob
5 import tempfile
6 import sys
7 from collections import defaultdict
8
9 here = os.path.abspath(os.path.split(__file__)[0])
10
11 def get_extra_files(chromium_root):
12     return [(os.path.join(chromium_root, "LayoutTests", "http", "tests", "resources", "testharness-helpers.js"),
13              os.path.join("resources", "testharness-helpers.js"))]
14
15 resources_re = re.compile("/?(?:\.\./)*resources/(testharness(?:report)?)\.js")
16
17 def resources_path(line, depth):
18     return False, resources_re.sub(r"/resources/\1.js", line)
19
20 php_re = re.compile("\.php")
21
22 def python_to_php(line, depth):
23     return False, php_re.sub(".py", line)
24
25 abs_testharness_helpers_re = re.compile("([\"'])/resources/testharness-helpers.js")
26 testharness_helpers_re = re.compile("\.\./((?:\.\./)*)resources/testharness-helpers.js")
27
28 def testharness_helpers(line, depth):
29     if abs_testharness_helpers_re.findall(line):
30         return False, abs_testharness_helpers_re.sub(r"\1%sresources/testharness-helpers.js" % ("../" * (depth - 1)), line)
31     return False, testharness_helpers_re.sub(r"\1resources/testharness-helpers.js", line)
32
33 serviceworker_path_re = re.compile("/serviceworker/")
34 def service_worker_path(line, depth):
35     return False, serviceworker_path_re.sub("/service-workers/", line)
36
37 localhost_re = re.compile("localhost")
38 alt_host_re = re.compile("127\.0\.0\.1")
39 port_http_re = re.compile("8000")
40 port_https_re = re.compile("8000")
41
42
43 def server_names(line, depth):
44     line, count_0 = localhost_re.subn("{{host}}", line)
45     line, count_1 = alt_host_re.subn("{{domains[www]}}", line)
46     line, count_2 = port_http_re.subn("{{ports[http][0]}}", line)
47     line, count_3 = port_https_re.subn("{{ports[https][0]}}", line)
48
49     count = count_0 + count_1 + count_2 + count_3
50
51     return bool(count), line
52
53
54 def source_paths(chromium_root):
55     for dirpath, dirnames, filenames in os.walk(chromium_root):
56         if "chromium" in dirnames:
57             dirnames.remove("chromium")
58         for filename in filenames:
59             if filename.endswith("-expected.txt") or filename.endswith(".php"):
60                 continue
61             yield os.path.relpath(os.path.join(dirpath, filename), chromium_root)
62
63
64 def do_subs(path, line):
65     depth = len(os.path.split(os.path.sep))
66     subs = [resources_path, python_to_php, testharness_helpers, service_worker_path, server_names]
67     file_is_template = False
68     for sub in subs:
69         added_template, line = sub(line, depth)
70         if added_template:
71             file_is_template = True
72     return file_is_template, line
73
74 def get_head(git):
75     return git("rev-parse", "HEAD")
76
77 def get_changes(git, path, old, new):
78     data = git("diff", "--name-status", "-z", "--no-renames", "%s..%s" % (old, new), "--", path)
79     items = data.split("\0")
80     rv = defaultdict(list)
81     for status, path in items:
82         rv[status].append(path)
83
84     return rv
85
86 def copy(src_path, out_dir, rel_path):
87     dest = os.path.normpath(os.path.join(out_dir, rel_path))
88     dest_dir = os.path.split(dest)[0]
89     if not os.path.exists(dest_dir):
90         os.makedirs(dest_dir)
91     shutil.copy2(src_path, dest)
92
93 def copy_local_files(local_files, out_root, tmp_dir):
94     for path in local_files:
95         rel_path = os.path.relpath(path, out_root)
96         copy(path, tmp_dir, rel_path)
97
98 def copy_extra_files(chromium_root, tmp_dir):
99     for in_path, rel_path in get_extra_files(chromium_root):
100         copy(in_path, tmp_dir, rel_path)
101
102 def sub_changed_filenames(filename_changes, f):
103     rv = []
104     for line in f:
105         for in_name, out_name in filename_changes.iteritems():
106             line = line.replace(in_name, out_name)
107         rv.append(line)
108     return "".join(rv)
109
110 testharness_re = re.compile("<script[^>]*src=[\"']?/resources/testharness.js[\"' ][^>]*>")
111
112 def is_top_level_test(path, data):
113     if os.path.splitext(path)[1] != ".html":
114         return False
115     for line in data:
116         if testharness_re.findall(line):
117             return True
118     return False
119
120 def add_suffix(path, suffix):
121     root, ext = os.path.splitext(path)
122     return root + ".%s" % suffix + ext
123
124 def main():
125     if "--cache-tests" in sys.argv:
126         sw_path = os.path.join("LayoutTests", "http", "tests", "cachestorage")
127         out_root = os.path.abspath(os.path.join(here, "..", "cache-storage"))
128     elif "--sw-tests" in sys.argv:
129         sw_path = os.path.join("LayoutTests", "http", "tests", "serviceworkers")
130         out_root = os.path.abspath(os.path.join(here, "..", "service-worker"))
131     else:
132         raise ValueError("Must supply either --cache-tests or --sw-tests")
133
134     chromium_root = os.path.abspath(sys.argv[1])
135
136     work_path = tempfile.mkdtemp()
137
138     test_path = os.path.join(chromium_root, sw_path)
139
140     local_files = glob.glob(os.path.normpath(os.path.join(here, "..", "resources", "*.py")))
141
142     if not os.path.exists(out_root):
143         os.mkdir(out_root)
144
145     copy_local_files(local_files, out_root, work_path)
146     copy_extra_files(chromium_root, work_path)
147
148     path_changes = {}
149
150     for path in source_paths(test_path):
151         out_path = os.path.join(work_path, path)
152         out_dir = os.path.dirname(out_path)
153         if not os.path.exists(out_dir):
154             os.makedirs(out_dir)
155         with open(os.path.join(test_path, path), "r") as in_f:
156             data = []
157             sub = False
158             for line in in_f:
159                 sub_flag, output_line = do_subs(path, line)
160                 data.append(output_line)
161                 if sub_flag:
162                     sub = True
163             is_test = is_top_level_test(out_path, data)
164
165         initial_path = out_path
166
167         if is_test:
168             path_1 = add_suffix(out_path, "https")
169         else:
170             path_1 = out_path
171
172         if sub:
173             path_2 = add_suffix(out_path, "sub")
174         else:
175             path_2 = path_1
176
177         if path_2 != initial_path:
178             path_changes[initial_path] = path_2
179
180         with open(path_2, "w") as out_f:
181             out_f.write("".join(data))
182
183     filename_changes = {}
184
185     for k, v in path_changes.iteritems():
186         if os.path.basename(k) in filename_changes:
187             print "Got duplicate name:" + os.path.basename(k)
188         filename_changes[os.path.basename(k)] = os.path.basename(v)
189
190     for path in source_paths(work_path):
191         full_path = os.path.join(work_path, path)
192         with open(full_path, "r") as f:
193             data = sub_changed_filenames(filename_changes, f)
194         with open(full_path, "w") as f:
195             f.write(data)
196
197     for dirpath, dirnames, filenames in os.walk(work_path):
198         for filename in filenames:
199             in_path = os.path.join(dirpath, filename)
200             rel_path = os.path.relpath(in_path, work_path)
201             copy(in_path, out_root, rel_path)
202
203 if __name__ == "__main__":
204     main()