# (C) Copyright 2016-2021 Xilinx, Inc.
# All Rights Reserved.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License. 


import xml.etree.ElementTree as ET
import os, sys, re


def print_usage_exit() :
    print "Usage: copy_project_files.py file_type project_file target_dir";
    exit();

if (len(sys.argv) != 4) :
    print_usage_exit();

file_type    = sys.argv[1];    
project_file = sys.argv[2];    
target_dir   = sys.argv[3];    


if not os.path.isdir(target_dir) :
    print "[ERROR] Target directory \""+target_dir+"\" does not exist.";
    exit();


xml_tree = ET.parse(project_file);

xml_root = xml_tree.getroot();

if file_type == "src" : 
    xml_file_set = xml_root.find("source_file_set");
elif file_type == "aux" :    
    xml_file_set = xml_root.find("aux_file_set");
else: 
    print "[ERROR] File type \""+file_type+"\" is not recognized.";
    exit();

for xml_file in xml_file_set.findall("file") :
    os.system("cp " + xml_file.text + " " + target_dir);




