ctkFunctionDownloadData.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #! Fetch data from a MIDAS server
  22. #!
  23. #! Usage:
  24. #! \code
  25. #! ctkFunctionDownloadData(
  26. #! MIDAS_SERVER_URL http://www.insight-journal.org/midas/item/download/
  27. #! ITEMID 2461
  28. #! OUTPUT_DIRECTORY /home/jchris/Projects/Data
  29. #! [FORCE_DOWNLOAD]
  30. #! )
  31. #! \endcode
  32. #!
  33. #! The downloaded file will have the from: midas_item_<ITEMID>.tar
  34. #!
  35. #! \ingroup CMakeUtilities
  36. function( ctkFunctionDownloadData)
  37. ctkMacroParseArguments(MY
  38. "MIDAS_SERVER_URL;ITEMID;OUTPUT_DIRECTORY"
  39. "FORCE_DOWNLOAD"
  40. ${ARGN}
  41. )
  42. # Sanity checks
  43. foreach(arg MIDAS_SERVER_URL ITEMID OUTPUT_DIRECTORY)
  44. if(NOT DEFINED MY_${arg})
  45. message(FATAL_ERROR "${arg} is mandatory")
  46. endif()
  47. endforeach()
  48. # Make sure output directory exists
  49. if(NOT EXISTS "${MY_OUTPUT_DIRECTORY}")
  50. message(FATAL_ERROR "OUTPUT_DIRECTORY '${MY_OUTPUT_DIRECTORY}' doesn't exist !")
  51. endif()
  52. # Is download required ?
  53. set(dest_file ${MY_OUTPUT_DIRECTORY}/midas_item_${MY_ITEMID}.tar)
  54. IF (NOT EXISTS ${dest_file} OR MY_FORCE_DOWNLOAD)
  55. set(url ${MY_MIDAS_SERVER_URL}/${MY_ITEMID})
  56. file(DOWNLOAD ${url} ${dest_file} STATUS status)
  57. list(GET status 0 error_code)
  58. list(GET status 1 error_msg)
  59. if(error_code)
  60. message(FATAL_ERROR "error: Failed to download ${url} - ${error_msg}")
  61. endif()
  62. message(STATUS "info: downloaded '${dest_file}'")
  63. endif()
  64. endfunction()
  65. #
  66. # Test - Use cmake -DMACRO_TESTING:BOOL=ON -P ctkFunctionDownloadData.cmake
  67. #
  68. if(FUNCTION_TESTING)
  69. include(ctkMacroParseArguments.cmake)
  70. message("Testing ctkFunctionDownloadData ...")
  71. #
  72. # Test1
  73. #
  74. set(url http://www.insight-journal.org/midas/item/download/)
  75. set(output_dir $ENV{HOME}/Projects/Data)
  76. set(itemid 2461)
  77. message(STATUS "downloading... [http://www.insight-journal.org/midas/item/download/${itemid}]")
  78. ctkFunctionDownloadData(
  79. MIDAS_SERVER_URL ${url}
  80. ITEMID ${itemid}
  81. OUTPUT_DIRECTORY ${output_dir}
  82. )
  83. set(expected_file ${output_dir}/midas_item_${itemid}.tar)
  84. # Make sure the file exists
  85. if(NOT EXISTS ${expected_file})
  86. message(FATAL_ERROR "File '${expected_file}' doesn't exists")
  87. endif()
  88. set(extract_destination_dir ${output_dir}/item_${itemid})
  89. # Create a folder
  90. message(STATUS "creating directory ... [${extract_destination_dir}]")
  91. file(MAKE_DIRECTORY ${extract_destination_dir})
  92. # Extract
  93. set(tar_args xf)
  94. message(STATUS "extracting... [tar midas_item_${itemid}.tar]")
  95. execute_process(COMMAND ${CMAKE_COMMAND} -E tar ${tar_args} ${expected_file}
  96. WORKING_DIRECTORY ${extract_destination_dir}
  97. RESULT_VARIABLE rv)
  98. if(NOT rv EQUAL 0)
  99. message(STATUS "extracting... [error clean up]")
  100. file(REMOVE_RECURSE ${extract_destination_dir})
  101. message(FATAL_ERROR "error: extract of '${expected_file}' failed")
  102. endif()
  103. # Remove archive
  104. #file(REMOVE ${expected_file})
  105. #file(REMOVE_RECURSE ${extract_destination_dir})
  106. endif()