#!/usr/bin/python

#
# This source file is part of appleseed.
# Visit http://appleseedhq.net/ for additional information and resources.
#
# This software is released under the MIT license.
#
# Copyright (c) 2014-2016 Francois Beaune, The appleseedhq Organization
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

from __future__ import print_function
import argparse
import os
import shutil


#--------------------------------------------------------------------------------------------------
# Utility functions.
#--------------------------------------------------------------------------------------------------

def safe_mkdir(dir):
    if not os.path.exists(dir):
        os.mkdir(dir)

def walk(directory, recursive):
    if recursive:
        for dirpath, dirnames, filenames in os.walk(directory):
            yield dirpath, dirnames, filenames
    else:
        yield os.walk(directory).next()


#--------------------------------------------------------------------------------------------------
# Update reference images in a given test suite directory.
#--------------------------------------------------------------------------------------------------

def update_ref_images(parent_dir):
    renders_dir = os.path.join(parent_dir, "renders")
    ref_dir = os.path.join(parent_dir, "ref")

    safe_mkdir(ref_dir)

    for filename in os.listdir(renders_dir):
        if os.path.splitext(filename)[1] == ".png":
            src_path = os.path.join(renders_dir, filename)
            dst_path = os.path.join(ref_dir, filename)
            print("  copying {0} to {1}...".format(src_path, dst_path))
            shutil.copyfile(src_path, dst_path)


#--------------------------------------------------------------------------------------------------
# Entry point.
#--------------------------------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(description="update functional test suite reference images.")
    parser.add_argument("-r", "--recursive", action='store_true', dest="recursive",
                        help="scan the specified directory and all its subdirectories")
    parser.add_argument("directory", nargs='?', default=".", help="directory to scan")
    args = parser.parse_args()

    for dirpath, dirnames, filenames in walk(args.directory, args.recursive):
        if "renders" in dirnames:
            update_ref_images(dirpath)

if __name__ == '__main__':
    main()

[((57, 18, 57, 53), 'os.path.join', 'os.path.join', ({(57, 31, 57, 41): 'parent_dir', (57, 43, 57, 52): '"""renders"""'}, {}), "(parent_dir, 'renders')", False, 'import os\n'), ((58, 14, 58, 45), 'os.path.join', 'os.path.join', ({(58, 27, 58, 37): 'parent_dir', (58, 39, 58, 44): '"""ref"""'}, {}), "(parent_dir, 'ref')", False, 'import os\n'), ((62, 20, 62, 43), 'os.listdir', 'os.listdir', ({(62, 31, 62, 42): 'renders_dir'}, {}), '(renders_dir)', False, 'import os\n'), ((75, 13, 75, 98), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((41, 11, 41, 30), 'os.path.exists', 'os.path.exists', ({(41, 26, 41, 29): 'dir'}, {}), '(dir)', False, 'import os\n'), ((42, 8, 42, 21), 'os.mkdir', 'os.mkdir', ({(42, 17, 42, 20): 'dir'}, {}), '(dir)', False, 'import os\n'), ((46, 44, 46, 62), 'os.walk', 'os.walk', ({(46, 52, 46, 61): 'directory'}, {}), '(directory)', False, 'import os\n'), ((64, 23, 64, 58), 'os.path.join', 'os.path.join', ({(64, 36, 64, 47): 'renders_dir', (64, 49, 64, 57): 'filename'}, {}), '(renders_dir, filename)', False, 'import os\n'), ((65, 23, 65, 54), 'os.path.join', 'os.path.join', ({(65, 36, 65, 43): 'ref_dir', (65, 45, 65, 53): 'filename'}, {}), '(ref_dir, filename)', False, 'import os\n'), ((67, 12, 67, 47), 'shutil.copyfile', 'shutil.copyfile', ({(67, 28, 67, 36): 'src_path', (67, 38, 67, 46): 'dst_path'}, {}), '(src_path, dst_path)', False, 'import shutil\n'), ((63, 11, 63, 37), 'os.path.splitext', 'os.path.splitext', ({(63, 28, 63, 36): 'filename'}, {}), '(filename)', False, 'import os\n'), ((49, 14, 49, 32), 'os.walk', 'os.walk', ({(49, 22, 49, 31): 'directory'}, {}), '(directory)', False, 'import os\n')]