• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2
3# Copyright 2021 The Tint Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Collect all .wgsl files under a given directory and copy them to a given
18# corpus directory, flattening their file names by replacing path
19# separators with underscores. If the output directory already exists, it
20# will be deleted and re-created. Files ending with ".expected.spvasm" are
21# skipped.
22#
23# The intended use of this script is to generate a corpus of WGSL shaders
24# for fuzzing.
25#
26# Usage:
27#    generate_wgsl_corpus.py <input_dir> <corpus_dir>
28
29
30import os
31import pathlib
32import shutil
33import sys
34
35
36def list_wgsl_files(root_search_dir):
37    for root, folders, files in os.walk(root_search_dir):
38        for filename in folders + files:
39            if pathlib.Path(filename).suffix == '.wgsl':
40                yield os.path.join(root, filename)
41
42
43def main():
44    if len(sys.argv) != 3:
45        print("Usage: " + sys.argv[0] + " <input dir> <output dir>")
46        return 1
47    input_dir: str = os.path.abspath(sys.argv[1].rstrip(os.sep))
48    corpus_dir: str = os.path.abspath(sys.argv[2])
49    if os.path.exists(corpus_dir):
50        shutil.rmtree(corpus_dir)
51    os.makedirs(corpus_dir)
52    for in_file in list_wgsl_files(input_dir):
53        if in_file.endswith(".expected.wgsl"):
54            continue
55        out_file = in_file[len(input_dir) + 1:].replace(os.sep, '_')
56        shutil.copy(in_file, corpus_dir + os.sep + out_file)
57
58
59if __name__ == "__main__":
60    sys.exit(main())
61