]> Creatis software - cpPlugins.git/commitdiff
...
authorLeonardo Flórez-Valencia <florez-l@javeriana.edu.co>
Tue, 11 Oct 2016 22:19:36 +0000 (17:19 -0500)
committerLeonardo Flórez-Valencia <florez-l@javeriana.edu.co>
Tue, 11 Oct 2016 22:19:36 +0000 (17:19 -0500)
dependencies/dependencies.py

index cc1d9172e1048c51b73e4e180ea678261b788293..c2323802ff14747d671a470d2ff015c11426bdf7 100644 (file)
-import os, platform, readline, glob, multiprocessing
+import errno, glob, os, math, mimetypes, multiprocessing
+import platform, readline, tarfile
 from distutils import spawn
 
+## ------------------------
+## -- Create directories --
+## ------------------------
+
+def create_dir( path ):
+    try:
+        os.makedirs(path)
+    except OSError as exception:
+        if exception.errno != errno.EEXIST:
+            raise
+    # yrt
+# fed
+        
 ## ------------------------
 ## -- Filename completer --
 ## ------------------------
 
-def complete( text, state ):
+def filename_completer( text, state ):
     return ( glob.glob( text + '*' ) + [ None ] )[ state ]
 # fed
 
-## -----------------------
-## -- File decompressor --
-## -----------------------
+def get_filepath( message ):
+    readline.set_completer_delims( ' \t\n;' )
+    readline.parse_and_bind( "tab: complete" )
+    readline.set_completer( filename_completer )
+    return os.path.abspath( raw_input( message ) )
+# fed
 
-def decompress( filename, outdir ):
-    real_ext = None
-    for ext in [ ".zip", ".tar", ".tar.gz", ".tar.bz2" ]:
-        if filename[ len( filename ) - len( ext ) : ] == ext:
-            real_ext = ext
-        # fi
+## ----------------
+## -- Find paths --
+## ----------------
+
+def find_paths( locs, progs ):
+    paths = []
+    for prog in progs:
+        for loc in locs:
+            path = spawn.find_executable( loc + "/" + prog )
+            if path <> None:
+                paths.append( path )
+        # rof
     # rof
-    command = None
-    if real_ext == ".zip":
-        command = "unzip " + filename + " -d " + outdir
-    elif real_ext == ".tar":
-        command = "tar xf " + filename + " -C " + outdir + " --strip-components=1"
-    elif real_ext == ".tar.gz":
-        command = "tar xzf " + filename + " -C " + outdir + " --strip-components=1"
-    elif real_ext == ".tar.bz2":
-        command = "tar xjf " + filename + " -C " + outdir + " --strip-components=1"
-    # fi
-    if command <> None:
-        print( "\tExtracting " + filename + "..." )
-        res = os.system( "mkdir -p " + outdir + " && " + command )
-        print( "\t\tdone." )
-        if res == 0:
-            return True
-        else:
-            return False
-    else:
-        return False
+    return paths
+# def
+
+## ------------------------------
+## -- Extract compressed files --
+## ------------------------------
+
+def extract( src ):
+    tar_gz_type = ( "application/x-tar", "gzip" )
+    src_type = mimetypes.guess_type( src )
+    print( "Extracting " + src + ": " )
+    if src_type == tar_gz_type:
+        src_dir = os.path.splitext( src )[ 0 ]
+        src_file = tarfile.open( src, "r|gz" )
+        src_file.extractall( src_dir )
+        src_file.close( )
+    #elif src_type == tar_bz2_type:
+    #elif src_type == zip_type:
+    #else:
     # fi
-# fed
+    print( " done!" )
+    return src_dir
+
+## -------------------
+## -- Choose config --
+## -------------------
 
-## -------------------------
-## -- Find necessary data --
-## -------------------------
-
-system_name = platform.system( )
-process_count = multiprocessing.cpu_count( ) / 2
-qmake_exec = spawn.find_executable( "qmake-qt4" )
-cmake_exec = spawn.find_executable( "cmake" )
-readline.set_completer_delims( ' \t\n;' )
-readline.parse_and_bind( "tab: complete" )
-readline.set_completer( complete )
-install_prefix = os.path.abspath( raw_input( "Install prefix: " ) )
-
-## --------------------------------------------
-## -- Ask if new versions should be compiled --
-## --------------------------------------------
-
-build_qmake = "y"
-if qmake_exec <> None:
-    build_qmake = raw_input( "Qt4 was found in \"" + qmake_exec + "\", do you want to install a new Qt4 [y/n]? " )
+def choose_config( paths, name ):
+    print( "Please choose one configuration for " + name + ":" )
+    for i in range( len( paths ) ):
+        print( "\t" + str( i ) + ": " + paths[ i ] )
+    # rof
+    print( "\t" + str( len( paths ) + 0 ) + ": Give another installation." )
+    print( "\t" + str( len( paths ) + 1 ) + ": Build from scratch." )
+    opt = -1
+    while opt == -1:
+        opt = int( raw_input( "\t---> " ) )
+        if opt < 0 or opt > len( paths ) + 1:
+            qmake_opt = -1
+        # fi
+    # elihw
+    return opt
+# def
+
+## --------------------
+## -- Some variables --
+## --------------------
+
+home_dir = os.environ[ "HOME" ]
+process_count = str( multiprocessing.cpu_count( ) / 2 )
+
+## -----------------------------------------------
+## -- qmake and cmake locations and executables --
+## -----------------------------------------------
+
+locations = []
+qmake_progs = []
+cmake_progs = []
+if platform.system( ) == "Linux":
+    locations = [
+        "/usr/bin",
+        "/usr/local/bin",
+        home_dir + "/bin",
+        home_dir + "/local/bin"
+        ]
+    qmake_progs = [ "qmake", "qmake-qt4" ]
+    cmake_progs = [ "cmake" ]
+# fi
+qmake_paths = find_paths( locations, qmake_progs )
+cmake_paths = find_paths( locations, cmake_progs )
+
+qmake_opt = choose_config( qmake_paths, "Qt4" )
+qmake_src = None
+if qmake_opt == len( qmake_paths ):
+    qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
+    while not os.path.isfile( qmake_exec ):
+        qmake_exec = get_filepath( "Choose your own Qt4 installation: " )
+    # elihw
+elif qmake_opt == len( qmake_paths ) + 1:
+    qmake_src = get_filepath( "Choose your own Qt4 source file: " )
+    while not os.path.isfile( qmake_src ):
+        qmake_src = get_filepath( "Choose your own Qt4 source file: " )
+    # elihw
+else:
+    qmake_exec = qmake_paths[ qmake_opt ]
 # fi
-build_qmake.lower( )
 
-build_cmake = "y"
-if cmake_exec <> None:
-    build_cmake = raw_input( "CMake was found in \"" + cmake_exec + "\", do you want to install a new CMake [y/n]? " )
+cmake_opt = choose_config( cmake_paths, "CMake" )
+cmake_src = None
+if cmake_opt == len( cmake_paths ):
+    cmake_exec = get_filepath( "Choose your own CMake installation: " )
+    while not os.path.isfile( cmake_exec ):
+        cmake_exec = get_filepath( "Choose your own CMake installation: " )
+    # elihw
+elif cmake_opt == len( cmake_paths ) + 1:
+    cmake_src = get_filepath( "Choose your own CMake source file: " )
+    while not os.path.isfile( cmake_src ):
+        cmake_src = get_filepath( "Choose your own CMake source file: " )
+    # elihw
+else:
+    cmake_exec = cmake_paths[ cmake_opt ]
+# fi
+
+## -----------------------
+## -- ITK and VTK files --
+## -----------------------
+
+itk_src = get_filepath( "Choose your own ITK source file: " )
+while not os.path.isfile( itk_src ):
+    itk_src = get_filepath( "Choose your own ITK source file: " )
+# elihw
+itk_extra = raw_input( "Type an extra configurations for ITK:" )
+
+vtk_src = get_filepath( "Choose your own VTK source file: " )
+while not os.path.isfile( vtk_src ):
+    vtk_src = get_filepath( "Choose your own VTK source file: " )
+# elihw
+vtk_extra = raw_input( "Type an extra configurations for VTK:" )
+
+## ------------------------
+## -- More configuration --
+## ------------------------
+
+install_prefix = get_filepath( "Choose your installation prefix: " )
+build_type = raw_input( "Type your build type [(r)elease/(d)ebug]: " )
+build_type.lower( )
+qmake_full = "n"
+if qmake_src <> None:
+    qmake_full = raw_input( "Would you like a full Qt4 install [y/n]? " )
+    qmake_full.lower( )
+# fi
+qmake_build_type = "release"
+kitware_build_type = "MinSizeRel"
+if build_type[ 0 ] == "d":
+    qmake_build_type = "debug"
+    kitware_build_type = "Debug"
 # fi
-build_cmake.lower( )
 
 ## -----------------
 ## -- Compile Qt4 --
 ## -----------------
 
-if build_qmake[ 0 ] == "y":
-    print( "Compiling Qt4..." )
-    readline.set_completer_delims( ' \t\n;' )
-    readline.parse_and_bind( "tab: complete" )
-    readline.set_completer( complete )
-    qt4_file = os.path.abspath(
-        raw_input( "\tCompressed file containing Qt4: " )
-        )
-    full = raw_input( "\tWould you like to perform a full build [y/n]? " )
-    full.lower( )
-    build_type = raw_input( "\tBuild type [release/debug]? " )
-    build_type.lower( )
-    if not decompress( qt4_file, "./qt4_source" ):
-        print( "Unable to decompress Qt4." )
-        exit( 1 )
-    # fi
-    qt4_source = os.path.abspath( "./qt4_source" )
-    os.system( "mkdir -p ./qt4_build" )
-    qt4_build = os.path.abspath( "./qt4_build" )
-
-    config_command = qt4_source + "/configure"
-    config_command += " -prefix " + install_prefix
-    config_command += " -" + build_type
-    config_command += " -opensource -shared -fast -no-webkit -optimized-qmake -confirm-license"
-    if full[ 0 ] == "n":
-        config_command += " -no-phonon -no-phonon-backend -no-openvg -nomake demos -nomake examples"
-    # fi
-    ## MACOS: -no-framework
+if qmake_src <> None:
+    qmake_src_dir = extract( qmake_src )
+    qmake_bin_dir = qmake_src_dir + "-build"
+    min_size = 999999
+    qmake_configure = ""
+    for root, dirs, files in os.walk( qmake_src_dir ):
+        if "configure" in files:
+            path = os.path.join( root, "configure" )
+            if min_size > len( path ):
+                min_size = len( path )
+                qmake_configure = path
+            # fi
+        # fi
+    # rof
+    create_dir( qmake_bin_dir )
 
-    ## Build and install
-    res = os.system( "cd " + qt4_build + " && " + config_command )
-    if res <> 0:
-        print( "Error configuring Qt4." )
-        exit( 1 )
+    # particular_options=-no-framework
+    particular_options = ""
+
+    command  = "cd " + qmake_bin_dir + " && "
+    command += qmake_configure
+    command += " -prefix " + install_prefix
+    command += " -" + qmake_build_type
+    command += " -opensource -shared -fast -no-webkit -optimized-qmake"
+    command += " -confirm-license"
+    if qmake_full == "y":
+        command += " -no-phonon -no-phonon-backend -no-openvg"
+        command += " -nomake demos -nomake examples"
     # fi
-    res = os.system( "cd " + qt4_build + " && make -j" + str( process_count ) )
-    if res <> 0:
-        print( "Error compiling Qt4." )
-        exit( 1 )
+    command += particular_options
+    command += " && make -j" + process_count
+    command += " && make -j install"
+    os.system( command )
+    qmake_exec = find_paths( [ install_prefix ], [ "qmake" ] )[ 0 ]
+# fi
+
+## -------------------
+## -- Compile CMake --
+## -------------------
+
+if cmake_src <> None:
+    cmake_src_dir = extract( cmake_src )
+    cmake_bin_dir = cmake_src_dir + "-build"
+    min_size = 999999
+    cmake_configure = ""
+    for root, dirs, files in os.walk( cmake_src_dir ):
+        if "bootstrap" in files:
+            path = os.path.join( root, "bootstrap" )
+            if min_size > len( path ):
+                min_size = len( path )
+                cmake_configure = path
+            # fi
+        # fi
+    # rof
+    create_dir( cmake_bin_dir )
+
+    command  = "cd " + cmake_bin_dir + " && "
+    command += cmake_configure
+    command += " --prefix " + install_prefix
+    command += " --qt-gui --qt-qmake=" + qmake_exec
+    command += " && make -j" + process_count
+    command += " && make -j install"
+    os.system( command )
+    cmake_exec = find_paths( [ install_prefix ], [ "cmake" ] )[ 0 ]
+# fi
+
+## -----------------
+## -- Compile ITK --
+## -----------------
+
+itk_src_dir = extract( itk_src )
+itk_bin_dir = itk_src_dir + "-build"
+min_size = 999999
+itk_configure = ""
+for root, dirs, files in os.walk( itk_src_dir ):
+    if "CMakeLists.txt" in files:
+        path = os.path.join( root, "CMakeLists.txt" )
+        if min_size > len( path ):
+            min_size = len( path )
+            itk_configure = path
+        # fi
     # fi
-    res = os.system( "cd " + qt4_build + " && make -j install" )
-    if res <> 0:
-        print( "Error installing Qt4." )
-        exit( 1 )
+# rof
+create_dir( itk_bin_dir )
+
+command  = "cd " + itk_bin_dir + " && "
+command += cmake_exec
+command += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
+command += " -DBUILD_DOCUMENTATION:BOOL=OFF"
+command += " -DBUILD_EXAMPLES:BOOL=OFF"
+command += " -DBUILD_SHARED_LIBS:BOOL=ON"
+command += " -DBUILD_TESTING:BOOL=OFF"
+command += " -DModule_ITKReview:BOOL=ON"
+command += " -DModule_ITKVtkGlue:BOOL=OFF"
+command += " -DModule_ParabolicMorphology:BOOL=ON"
+command += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build_type
+command += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
+command += " " + itk_src_dir
+command += " && make -j" + process_count
+command += " && make -j install"
+os.system( command )
+
+## -----------------
+## -- Compile VTK --
+## -----------------
+
+vtk_src_dir = extract( vtk_src )
+vtk_bin_dir = vtk_src_dir + "-build"
+min_size = 999999
+vtk_configure = ""
+for root, dirs, files in os.walk( vtk_src_dir ):
+    if "CMakeLists.txt" in files:
+        path = os.path.join( root, "CMakeLists.txt" )
+        if min_size > len( path ):
+            min_size = len( path )
+            vtk_configure = path
+        # fi
     # fi
-# fi
+# rof
+create_dir( vtk_bin_dir )
+
+command  = "cd " + vtk_bin_dir + " && "
+command += cmake_exec
+command += " -DCMAKE_CXX_FLAGS:STRING=-std=c++11"
+command += " -DBUILD_DOCUMENTATION:BOOL=OFF"
+command += " -DBUILD_EXAMPLES:BOOL=OFF"
+command += " -DBUILD_SHARED_LIBS:BOOL=ON"
+command += " -DBUILD_TESTING:BOOL=OFF"
+command += " -DModule_vtkGUISupportQt:BOOL=ON"
+command += " -DModule_vtkGUISupportQtOpenGL:BOOL=ON"
+command += " -DModule_vtkGUISupportQtSQL:BOOL=OFF"
+command += " -DModule_vtkGUISupportQtWebkit:BOOL=OFF"
+command += " -DQT_QMAKE_EXECUTABLE:PATH=" + qmake_exec
+command += " -DCMAKE_BUILD_TYPE:STRING=" + kitware_build_type
+command += " -DCMAKE_INSTALL_PREFIX:PATH=" + install_prefix
+command += " " + vtk_src_dir
+command += " && make -j" + process_count
+command += " && make -j install"
+os.system( command )
 
 ## eof - $RCSfile$