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) 2010-2013 Francois Beaune, Jupiter Jazz Limited 10# Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization 11# 12# Permission is hereby granted, free of charge, to any person obtaining a copy 13# of this software and associated documentation files (the "Software"), to deal 14# in the Software without restriction, including without limitation the rights 15# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 16# copies of the Software, and to permit persons to whom the Software is 17# furnished to do so, subject to the following conditions: 18# 19# The above copyright notice and this permission notice shall be included in 20# all copies or substantial portions of the Software. 21# 22# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 28# THE SOFTWARE. 29# 30 31import argparse 32import datetime 33import os 34import subprocess 35 36 37# ------------------------------------------------------------------------------------------------- 38# Constants. 39# ------------------------------------------------------------------------------------------------- 40 41DEFAULT_TOOL_FILENAME = "projecttool.exe" if os.name == "nt" else "projecttool" 42 43 44# ------------------------------------------------------------------------------------------------- 45# Utility functions. 46# ------------------------------------------------------------------------------------------------- 47 48def walk(directory, recursive): 49 if recursive: 50 for dirpath, dirnames, filenames in os.walk(directory): 51 for filename in filenames: 52 yield os.path.join(dirpath, filename) 53 else: 54 dirpath, dirnames, filenames = os.walk(directory).next() 55 for filename in filenames: 56 yield os.path.join(dirpath, filename) 57 58 59# ------------------------------------------------------------------------------------------------- 60# Clean a given project file. 61# ------------------------------------------------------------------------------------------------- 62 63def clean_project_file(filepath, tool_path): 64 print("cleaning {0}...".format(filepath)) 65 subprocess.call([tool_path, "clean", filepath]) 66 67 68# ------------------------------------------------------------------------------------------------- 69# Clean all project files in a given directory (possibly recursively). 70# Returns the number of cleaned project files. 71# ------------------------------------------------------------------------------------------------- 72 73def clean_project_files(tool_path, directory, recursive): 74 cleaned_file_count = 0 75 76 for filepath in walk(directory, recursive): 77 if os.path.splitext(filepath)[1] == ".appleseed": 78 clean_project_file(filepath, tool_path) 79 cleaned_file_count += 1 80 81 return cleaned_file_count 82 83 84# ------------------------------------------------------------------------------------------------- 85# Entry point. 86# ------------------------------------------------------------------------------------------------- 87 88def main(): 89 parser = argparse.ArgumentParser(description="update multiple project files to the latest " 90 "format revision and remove unused entities.") 91 parser.add_argument("-t", "--tool-path", metavar="tool-path", 92 help="set the path to the projecttool binary") 93 parser.add_argument("-r", "--recursive", action='store_true', dest="recursive", 94 help="scan the specified directory and all its subdirectories") 95 parser.add_argument("directory", help="directory to scan") 96 args = parser.parse_args() 97 98 # If no tool path is provided, search for the tool in the same directory as this script. 99 if args.tool_path is None: 100 script_directory = os.path.dirname(os.path.realpath(__file__)) 101 args.tool_path = os.path.join(script_directory, DEFAULT_TOOL_FILENAME) 102 print("setting tool path to {0}.".format(args.tool_path)) 103 104 start_time = datetime.datetime.now() 105 cleaned_file_count = clean_project_files(args.tool_path, args.directory, args.recursive) 106 end_time = datetime.datetime.now() 107 108 print("cleaned {0} project file(s) in {1}.".format(cleaned_file_count, end_time - start_time)) 109 110 111if __name__ == "__main__": 112 main() 113