1#!/usr/local/bin/python3.8
2
3#
4# This source file is part of appleseed.
5# Visit https://appleseedhq.net/ for additional information and resources.
6#
7# This software is released under the MIT license.
8#
9# Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10#
11# Permission is hereby granted, free of charge, to any person obtaining a copy
12# of this software and associated documentation files (the "Software"), to deal
13# in the Software without restriction, including without limitation the rights
14# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15# copies of the Software, and to permit persons to whom the Software is
16# furnished to do so, subject to the following conditions:
17#
18# The above copyright notice and this permission notice shall be included in
19# all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27# THE SOFTWARE.
28#
29
30from __future__ import print_function
31import argparse
32import os
33import shutil
34
35
36# -------------------------------------------------------------------------------------------------
37# Utility functions.
38# -------------------------------------------------------------------------------------------------
39
40def safe_mkdir(dir):
41    if not os.path.exists(dir):
42        os.mkdir(dir)
43
44
45def walk(directory, recursive):
46    if recursive:
47        for dirpath, dirnames, filenames in os.walk(directory):
48            yield dirpath, dirnames, filenames
49    else:
50        yield os.walk(directory).next()
51
52
53# -------------------------------------------------------------------------------------------------
54# Update reference images in a given test suite directory.
55# -------------------------------------------------------------------------------------------------
56
57def update_ref_images(parent_dir):
58    renders_dir = os.path.join(parent_dir, "renders")
59    ref_dir = os.path.join(parent_dir, "ref")
60
61    safe_mkdir(ref_dir)
62
63    for filename in os.listdir(renders_dir):
64        if os.path.splitext(filename)[1] == ".png":
65            src_path = os.path.join(renders_dir, filename)
66            dst_path = os.path.join(ref_dir, filename)
67            print("  copying {0} to {1}...".format(src_path, dst_path))
68            shutil.copyfile(src_path, dst_path)
69
70
71# -------------------------------------------------------------------------------------------------
72# Entry point.
73# -------------------------------------------------------------------------------------------------
74
75def main():
76    parser = argparse.ArgumentParser(description="update functional test suite reference images.")
77    parser.add_argument("-r", "--recursive", action='store_true', dest="recursive",
78                        help="scan the specified directory and all its subdirectories")
79    parser.add_argument("directory", nargs='?', default=".", help="directory to scan")
80    args = parser.parse_args()
81
82    for dirpath, dirnames, filenames in walk(args.directory, args.recursive):
83        if "renders" in dirnames:
84            update_ref_images(dirpath)
85
86
87if __name__ == "__main__":
88    main()
89