ctkFunctionDownloadData.cmake 3.6 KB

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