CTK 0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
Loading...
Searching...
No Matches
ctkFunctionDownloadData.cmake
Go to the documentation of this file.
1###########################################################################
2#
3# Library: CTK
4#
5# Copyright (c) Kitware Inc.
6#
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
10#
11# http://www.apache.org/licenses/LICENSE-2.0.txt
12#
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.
18#
19###########################################################################
20
21
22#!
23#! Fetch data from a MIDAS server
24#!
25#! Usage:
26#! \code
27#! ctkFunctionDownloadData(
28#! MIDAS_SERVER_URL http://www.insight-journal.org/midas/item/download/
29#! ITEMID 2461
30#! OUTPUT_DIRECTORY /home/jchris/Projects/Data
31#! [FORCE_DOWNLOAD]
32#! )
33#! \endcode
34#!
35#! The downloaded file will have the from: midas_item_<ITEMID>.tar
36#!
37#! \ingroup CMakeUtilities
38function( ctkFunctionDownloadData)
39 ctkMacroParseArguments(MY
40 "MIDAS_SERVER_URL;ITEMID;OUTPUT_DIRECTORY"
41 "FORCE_DOWNLOAD"
42 ${ARGN}
43 )
44
45 # Sanity checks
46 foreach(arg MIDAS_SERVER_URL ITEMID OUTPUT_DIRECTORY)
47 if(NOT DEFINED MY_${arg})
48 message(FATAL_ERROR "${arg} is mandatory")
49 endif()
50 endforeach()
51
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 !")
55 endif()
56
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)
64 if(error_code)
65 message(FATAL_ERROR "error: Failed to download ${url} - ${error_msg}")
66 endif()
67 message(STATUS "info: downloaded '${dest_file}'")
68 endif()
69
70endfunction()
71
72#
73# Test - Use cmake -DMACRO_TESTING:BOOL=ON -P ctkFunctionDownloadData.cmake
74#
75if(FUNCTION_TESTING)
76
77 include(ctkMacroParseArguments.cmake)
78
79 message("Testing ctkFunctionDownloadData ...")
80
81 #
82 # Test1
83 #
84 set(url http://www.insight-journal.org/midas/item/download/)
85 set(output_dir $ENV{HOME}/Projects/Data)
86 set(itemid 2461)
87 message(STATUS "downloading... [http://www.insight-journal.org/midas/item/download/${itemid}]")
88 ctkFunctionDownloadData(
89 MIDAS_SERVER_URL ${url}
90 ITEMID ${itemid}
91 OUTPUT_DIRECTORY ${output_dir}
92 )
93
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")
98 endif()
99
100 set(extract_destination_dir ${output_dir}/item_${itemid})
101
102 # Create a folder
103 message(STATUS "creating directory ... [${extract_destination_dir}]")
104 file(MAKE_DIRECTORY ${extract_destination_dir})
105
106 # Extract
107 set(tar_args xf)
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}
111 RESULT_VARIABLE rv)
112
113 if(NOT rv EQUAL 0)
114 message(STATUS "extracting... [error clean up]")
115 file(REMOVE_RECURSE ${extract_destination_dir})
116 message(FATAL_ERROR "error: extract of '${expected_file}' failed")
117 endif()
118
119 # Remove archive
120 #file(REMOVE ${expected_file})
121 #file(REMOVE_RECURSE ${extract_destination_dir})
122endif()