1 ###########################################################################
5 # Copyright (c) Kitware Inc.
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0.txt
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 ###########################################################################
23 #! Fetch data from a MIDAS server
27 #! ctkFunctionDownloadData(
28 #! MIDAS_SERVER_URL http://www.insight-journal.org/midas/item/download/
30 #! OUTPUT_DIRECTORY /home/jchris/Projects/Data
35 #! The downloaded file will have the from: midas_item_<ITEMID>.tar
37 #! \ingroup CMakeUtilities
38 function( ctkFunctionDownloadData)
39 ctkMacroParseArguments(MY
40 "MIDAS_SERVER_URL;ITEMID;OUTPUT_DIRECTORY"
46 foreach(arg MIDAS_SERVER_URL ITEMID OUTPUT_DIRECTORY)
47 if(NOT DEFINED MY_${arg})
48 message(FATAL_ERROR "${arg} is mandatory")
52 # Make sure output directory exists
53 if(NOT EXISTS "${MY_OUTPUT_DIRECTORY}")
54 message(FATAL_ERROR "OUTPUT_DIRECTORY '${MY_OUTPUT_DIRECTORY}' doesn't exist !")
57 # Is download required ?
58 set(dest_file ${MY_OUTPUT_DIRECTORY}/midas_item_${MY_ITEMID}.tar)
59 IF (NOT EXISTS ${dest_file} OR MY_FORCE_DOWNLOAD)
60 set(url ${MY_MIDAS_SERVER_URL}/${MY_ITEMID})
61 file(DOWNLOAD ${url} ${dest_file} STATUS status)
62 list(GET status 0 error_code)
63 list(GET status 1 error_msg)
65 message(FATAL_ERROR "error: Failed to download ${url} - ${error_msg}")
67 message(STATUS "info: downloaded '${dest_file}'")
73 # Test - Use cmake -DMACRO_TESTING:BOOL=ON -P ctkFunctionDownloadData.cmake
77 include(ctkMacroParseArguments.cmake)
79 message("Testing ctkFunctionDownloadData ...")
84 set(url http://www.insight-journal.org/midas/item/download/)
85 set(output_dir $ENV{HOME}/Projects/Data)
87 message(STATUS "downloading... [http://www.insight-journal.org/midas/item/download/${itemid}]")
88 ctkFunctionDownloadData(
89 MIDAS_SERVER_URL ${url}
91 OUTPUT_DIRECTORY ${output_dir}
94 set(expected_file ${output_dir}/midas_item_${itemid}.tar)
95 # Make sure the file exists
96 if(NOT EXISTS ${expected_file})
97 message(FATAL_ERROR "File '${expected_file}' doesn't exists")
100 set(extract_destination_dir ${output_dir}/item_${itemid})
103 message(STATUS "creating directory ... [${extract_destination_dir}]")
104 file(MAKE_DIRECTORY ${extract_destination_dir})
108 message(STATUS "extracting... [tar midas_item_${itemid}.tar]")
109 execute_process(COMMAND ${CMAKE_COMMAND} -E tar ${tar_args} ${expected_file}
110 WORKING_DIRECTORY ${extract_destination_dir}
114 message(STATUS "extracting... [error clean up]")
115 file(REMOVE_RECURSE ${extract_destination_dir})
116 message(FATAL_ERROR "error: extract of '${expected_file}' failed")
120 #file(REMOVE ${expected_file})
121 #file(REMOVE_RECURSE ${extract_destination_dir})