Exemplo n.º 1
0
def main(argv):
  otbDir = op.abspath(argv[1])
  targetModule = argv[2]
  targetGroup = "TBD"
  if targetModule.count('/') == 1:
    pos = targetModule.find('/')
    targetGroup = targetModule[0:pos]
    targetModule = targetModule[pos+1:]
  if targetModule == "":
    print "Wrong module name, check input argument : "+argv[2]
  if targetGroup == "":
    print "Wrong group name, check input argument : "+argv[2]

  print "Target module: "+targetGroup+"/"+targetModule

  srcFiles = []
  for item in argv[3:]:
    src = item
    if not src.startswith("Modules/"):
      src = "Modules/"+src
    if op.isfile(op.join(otbDir,src)):
      srcFiles.append(src.strip('./'))
    else:
      print "Input source file not found, will be skipped : "+src
  
  # First, analyse current OTB tree, retrieve :
  #  - module list
  #  - group vs module association
  modulesRoot = op.join(otbDir,"Modules")
  
  [depList, optDepList, testDepList] = sourceAPI.parseOTBModuleCmake(modulesRoot)
  #fullDepList = manifestParser.buildFullDep(depList)
  #oldCleanDepList = sourceAPI.cleanDepList(depList,fullDepList)
  
  [groups,moduleList,sourceList,testList] = sourceAPI.parseModuleRoot(modulesRoot)
  
  # DEBUG
  #manifestParser.printDepList(depList)
  #print str(moduleList)
  
  
  # Second, operate the move
  if targetModule in moduleList:
    targetGroup = manifestParser.getGroup(targetModule,groups)
  else:
    if targetGroup == 'TBD':
      print "Error : group name must be specified for new modules (use group/module syntax)"
      return 1
  
  destinationPrefix = op.join(targetGroup,targetModule)
  for srcFile in srcFiles:
    cleanFile = srcFile.strip('./')
    words = cleanFile.split('/')
    srcMod = words[2]
    srcGrp = words[1]
    targetFile = cleanFile.replace(srcGrp+'/'+srcMod,destinationPrefix,1)
    targetPath = op.join(otbDir,op.dirname(targetFile))
    if not op.isdir(targetPath):
      os.makedirs(targetPath)
    shutil.move(op.join(otbDir,cleanFile),targetPath)
  
  # Compute new modules dependencies
  [newGroups,newModuleList,newSourceList,newTestList] = sourceAPI.parseModuleRoot(modulesRoot)
  newDepList = sourceAPI.buildModularDep(otbDir,newModuleList,newSourceList)
  newTestDepList = sourceAPI.buildModularDep(otbDir,newTestList,newSourceList)
  
  # compute full dependencies
  newFullDepList = manifestParser.buildFullDep(newDepList)
  
  # detect cyclic dependencies
  cyclicDependentModules = []
  for mod in newFullDepList.keys():
    if mod in newFullDepList[mod]:
      if not mod in cyclicDependentModules:
        cyclicDependentModules.append(mod)
  if len(cyclicDependentModules) > 0:
    print "Check for cyclic dependency : Failed"
    for m in cyclicDependentModules:
      cycle = sourceAPI.getCyclicDep(m,newDepList)
      print "Cycle for module "+m+": "+str(cycle)
      if len(cycle) > 0:
        a = m
        for c in cycle:
          b = c
          print a+" -> "+b+": "
          for dep in newDepList[a][b]:
            print "\t"+dep["from"]+" -> "+dep["to"]
          a = c
    
    #manifestParser.printDepList(newDepList,cyclicDependentModules)
    return 1
  else:
    print "Check for cyclic dependency : Passed"
  
  # fix srcList and test declaration
  os.chdir(otbDir)
  for srcFile in srcFiles:
    cleanFile = srcFile.strip('./')
    words = cleanFile.split('/')
    srcMod = words[2]
    srcGrp = words[1]
    srcSub = words[3]
    targetFile = cleanFile.replace(srcGrp+"/"+srcMod,destinationPrefix,1)
    # call hg rename -A
    call(["hg","rename","-A",cleanFile,targetFile])
    #  - for input files in 'src' : adapt OTBModule_SRC
    if srcSub == "src" and len(words) == 5:
      # remove entry in previous module
      removed = [op.basename(srcFile)]
      added = []
      cmakelistPath = op.join(modulesRoot,srcGrp,srcMod,srcSub,"CMakeLists.txt")
      # Check that there are some source file remaining in src dir
      source_files = filter(os.path.isfile, glob.glob(op.join(modulesRoot,srcGrp,srcMod,srcSub)+'/*.c*'))
      if len(source_files) >0 :
        sourceAPI.updateSourceList(cmakelistPath,"OTB"+srcMod+"_SRC",added,removed)
      else:
        # There are no more sources here, the whole src module can be removed
        print "Removing "+srcSub+" dir in "+srcGrp+"/"+srcMod+", since it does not contain source files anymore"
        call(["hg","remove",op.join(modulesRoot,srcGrp,srcMod,srcSub)+"/*"])
      # add entry in target module
      removed = []
      added = [op.basename(srcFile)]
      cmakelistPath = op.join(modulesRoot,destinationPrefix,srcSub,"CMakeLists.txt")
      if not op.exists(cmakelistPath):
        sourceAPI.initializeSrcCMakeLists(cmakelistPath,targetModule)
        call(["hg","add",cmakelistPath.replace(otbDir,".")])
      sourceAPI.updateSourceList(cmakelistPath,"OTB"+targetModule+"_SRC",added,removed)

    #  -for input files in 'app'
    if srcSub == "app" and len(words) == 5:
      cmakelistPath = op.join(modulesRoot,srcGrp,srcMod,srcSub,"CMakeLists.txt")
      nextCmakelistPath = op.join(modulesRoot,destinationPrefix,srcSub,"CMakeLists.txt")
      if not op.exists(nextCmakelistPath):
        sourceAPI.initializeAppCMakeLists(nextCmakelistPath,targetModule)
        call(["hg","add",nextCmakelistPath.replace(otbDir,".")])
      # move the 'otb_create_application' bloc
      appName = sourceAPI.moveAppBloc(cmakelistPath,nextCmakelistPath,op.basename(srcFile))
      
      source_files = filter(os.path.isfile, glob.glob(op.join(modulesRoot,srcGrp,srcMod,srcSub)+'/*.c*'))
      if len(source_files) == 0 :
        # There are no more sources here, the whole src module can be removed
        print "Removing "+srcSub+" dir in "+srcGrp+"/"+srcMod+", since it does not contain source files anymore"
        call(["hg","remove",op.join(modulesRoot,srcGrp,srcMod,srcSub)+"/*"])
      
      # move tests
      testCmakelistPath = op.join(modulesRoot,srcGrp,srcMod,"test","CMakeLists.txt")
      nextTestCmakelistPath = op.join(modulesRoot,destinationPrefix,"test","CMakeLists.txt")
      appTestBlocs = {}
      if op.exists(testCmakelistPath):
        appTestBlocs = sourceAPI.extractAppTestBlocs(testCmakelistPath,appName)
      
      if len(appTestBlocs) > 0:
        if not op.exists(nextTestCmakelistPath):
          if not op.exists(op.dirname(nextTestCmakelistPath)):
            os.makedirs(op.dirname(nextTestCmakelistPath))
          fd = open(nextTestCmakelistPath,'w')
          fd.write("otb_module_test()\n")
          fd.write("\n")
          fd.close()
          call(["hg","add",nextTestCmakelistPath.replace(otbDir,".")])
        
        fd = open(nextTestCmakelistPath,'a')
        fd.write("#----------- "+appName+" TESTS ----------------\n")
        for tname in appTestBlocs:
          fd.writelines(appTestBlocs[tname])
          fd.write("\n")
        fd.close()
    
    if srcSub == "test" and len(words) == 5:
      # analyse test
      res = createTestManifest.parseTestCxx(op.join(otbDir,targetFile))
      
      if res["hasMain"]:
        print "Test with main ("+targetFile+") : not handled for now"
      else:
        # remove entry in previous module source list
        removed = [op.basename(srcFile)]
        added = []
        oldCmakelistPath = op.join(modulesRoot,op.join(srcGrp,op.join(srcMod,"test/CMakeLists.txt")))
        oldTestDriverPath = op.join(modulesRoot,op.join(srcGrp,op.join(srcMod,"test/otb"+srcMod+"TestDriver.cxx")))
        sourceAPI.updateSourceList(oldCmakelistPath,"OTB"+srcMod+"Tests",added,removed)
        
        # add entry in target module source list
        removed = []
        added = [op.basename(srcFile)]
        cmakelistPath = op.join(modulesRoot,op.join(destinationPrefix,"test/CMakeLists.txt"))
        testDriverPath = op.join(modulesRoot,op.join(destinationPrefix,"test/otb"+targetModule+"TestDriver.cxx"))
        if not op.exists(cmakelistPath):
          # there was no test before : initialize CMakeLists.txt
          sourceAPI.initializeTestCMakeLists(cmakelistPath,targetModule)
          call(["hg","add",cmakelistPath.replace(otbDir,".")])
        if not op.exists(testDriverPath):
          # there was no test before : initialize test driver
          sourceAPI.initializeTestDriver(testDriverPath)
          call(["hg","add",testDriverPath.replace(otbDir,".")])
        sourceAPI.updateSourceList(cmakelistPath,"OTB"+targetModule+"Tests",added,removed)
        
        #  - move test declaration
        testCode = dispatchTests.findTestFromExe(oldCmakelistPath,"otb"+srcMod+"TestDriver","",res["testFunctions"],'otb_')
        
        # get set_test_properties()
        testProp = dispatchTests.findTestProperties(oldCmakelistPath,testCode.keys())
        
        print "Found "+str(len(testCode))+" tests to move"

        sourceAPI.moveTestCode(oldCmakelistPath,cmakelistPath,testCode,testProp)

  for mod in newModuleList:
    curGroup = manifestParser.getGroup(mod,newGroups)
    
    # fix the otb-module.cmake
    cmake_module_path = op.join(modulesRoot,op.join(curGroup,op.join(mod,"otb-module.cmake")))
    cmakel_module_path = op.join(modulesRoot,op.join(curGroup,op.join(mod,"CMakeLists.txt")))
    if (not op.exists(cmake_module_path)) and (mod == targetModule):
      # initialize new otb-module.cmake if new module
      sourceAPI.initializeOTBModuleCmake(cmake_module_path,mod)
      with_lib = False
      if op.exists(op.join(curGroup,op.join(mod,"src"))):
        with_lib = True
      sourceAPI.initializeOTBModuleCMakeLists(cmakel_module_path,mod,with_lib)
      call(["hg","add",cmakel_module_path.replace(otbDir,".")])
      
      call(["hg","add",cmake_module_path.replace(otbDir,".")])
    
    #  - fix the target_link_libraries
    src_dir = op.join(modulesRoot,curGroup,mod,"src")

    sub_src_CMakeList = op.join(modulesRoot,op.join(curGroup,op.join(mod,"src/CMakeLists.txt")))

  # Update the module dependencies
  print "Updating module dependencies ..."
  count = updateModuleDependencies.update(otbDir)
  print str(count)+" modules were updated"
  
  # Update the module test driver
  print "Updating test drivers ..."
  count = updateTestDrivers.update(otbDir)
  print str(count)+" files updated"

  # Update the group declaration in doxygen
  print "Updating doxygen group declaration ..."
  updateDoxyGroup.update(otbDir)

  # TODO : hg commit by the user
  print "\nTo commit those changes, run: hg commit -m \"ENH: Automatic move of files to module "+targetGroup+"/"+targetModule+"\"\n"
  print "It is advised to run updateModuleDependencies.py script beforehand.\n"
def update(otbDir,dry_run = False,verbose = False):
  
  modulesRoot = op.join(otbDir,"Modules")
  
  # Modules in this list will only be updated with additional
  # dependencies, nod dependencies will be removed
  blacklist_for_removal = ["CommandLine", "TestKernel", "ApplicationEngine", "OSSIMAdapters"]

  # Parse otb-module.cmake to get declared dependencies
  [depList, optDepList, testDepList] = sourceAPI.parseOTBModuleCmake(modulesRoot)
  
  # Parse source code to get all elements
  [groups,moduleList,sourceList,testList] = sourceAPI.parseModuleRoot(modulesRoot)
  actualDepList = sourceAPI.buildModularDep(otbDir,moduleList,sourceList)
  actualTestDepList = sourceAPI.buildModularDep(otbDir,testList,sourceList)

  changes_required = 0
  no_changes_required = 0

  for module in moduleList.keys():
  
    # Find group
    group = ""
    
    for grp in groups.keys():
      if module in groups[grp]:
        group = grp

    if len(group) == 0:
      print bcolors.FAIL+ "Error: module "+module+" not found in any group"+bcolor.ENDC
      continue
      

    fancy_module = bcolors.UNDERLINE + group+"/"+module+ bcolors.ENDC

    # Do not process third parties
    if module in groups['ThirdParty']:
      if verbose:
        print fancy_module +" - Ignoring third party module"
    else:
      current_deps = set([])
      if module in depList.keys():
        current_deps = set(depList[module].keys())
      current_actualdeps = set(actualDepList[module].keys())

      to_add = current_actualdeps - current_deps
      to_remove = current_deps - current_actualdeps

      # Handling optional dependencies
      current_opt_deps = set([])
      if module in optDepList.keys():
        for opt in optDepList[module].keys():
          if opt in to_add:
            if verbose:
              print fancy_module +" - Ignoring optional dependency: "+opt
            to_add.remove(opt)

        current_opt_deps = set(optDepList[module].keys())

      opt_to_remove = current_opt_deps - current_actualdeps

      # Handling test dependencies
      current_test_deps = set([])
      if module in testDepList.keys():
          current_test_deps = set(testDepList[module].keys())
      current_actual_test_deps = set(actualTestDepList[module].keys())

      # Do not self link
      if module in current_actual_test_deps:
        current_actual_test_deps.remove(module)

      for dep in current_actualdeps:
        if dep in current_actual_test_deps:
          current_actual_test_deps.remove(dep)
      
      test_deps_to_add = current_actual_test_deps - current_test_deps
      test_deps_to_remove = current_test_deps - current_actual_test_deps

      if group == "Applications":
        if verbose:
          print fancy_module+" - Module in Applications group, TestKernel and CommandLine modules will not be removed from tests dependencies"
        if "TestKernel" in test_deps_to_remove : test_deps_to_remove.remove("TestKernel")
        if "CommandLine" in test_deps_to_remove : test_deps_to_remove.remove("CommandLine")
    
      if module in blacklist_for_removal:
        if verbose:
          print fancy_module+" - Module is black-listed, only additions will be made to dependencies"
        to_remove.clear()
        opt_to_remove.clear()
        test_deps_to_remove.clear()

      if len(to_add) == 0 and len(to_remove) == 0 and len(opt_to_remove) == 0 and len(test_deps_to_add) == 0 and len(test_deps_to_remove) == 0:
        if verbose:
          print fancy_module+ bcolors.OKGREEN + " - No changes required"+ bcolors.ENDC
        no_changes_required+=1
      else:
        changes_required+=1
        if len(to_add) > 0:
          print fancy_module+bcolors.OKBLUE +" - Additional dependencies required: "+str(list(to_add))+ bcolors.ENDC
          if verbose:
            for dep_to_add in to_add:
              for dep_include in actualDepList[module][dep_to_add]:
                print fancy_module+" - needs "+dep_to_add+" because "+dep_include["from"]+" includes "+dep_include["to"]
        if len(to_remove) > 0:
          print fancy_module+bcolors.OKRED + " - Dependencies to remove: "+str(list(to_remove))+ bcolors.ENDC
       
        if len(opt_to_remove) > 0:
          print fancy_module+bcolors.OKRED + " - Optional dependencies to remove: "+str(list(opt_to_remove))+ bcolors.ENDC

        if len(test_deps_to_add):
           print fancy_module+bcolors.OKBLUE+" - Additional test dependencies required: "+str(list(test_deps_to_add))+ bcolors.ENDC
           if verbose:
             for dep_to_add in test_deps_to_add:
               for dep_include in actualTestDepList[module][dep_to_add]:
                 print fancy_module+" - needs "+dep_to_add+" because "+dep_include["from"]+" includes "+dep_include["to"]
        if len(test_deps_to_remove):
           print fancy_module+bcolors.OKRED + " - Test dependencies to remove: "+str(list(test_deps_to_remove))+ bcolors.ENDC

        final_dep = (current_deps | to_add) - to_remove
        final_opt_dep = current_opt_deps - opt_to_remove
        final_test_dep = (current_test_deps | test_deps_to_add) - test_deps_to_remove
        
        if not dry_run:
          cmake_mod_file_path=op.join(otbDir,"Modules",group,module,"otb-module.cmake")      
          print fancy_module+" - Patching file "+cmake_mod_file_path
          sourceAPI.updateModuleDependencies(cmake_mod_file_path,sorted(final_dep),sorted(final_opt_dep), sorted(final_test_dep))
          
          if len(to_add)>0:
              sub_src_CMakeList = op.join(modulesRoot,op.join(otbDir,"Modules",group,module,"src/CMakeLists.txt"))
              if op.isfile(sub_src_CMakeList):
                  print fancy_module+" - Patching file "+sub_src_CMakeList
                  sourceAPI.setTargetLinkLibs(sub_src_CMakeList,"OTB"+module,sorted(to_add))
      if not dry_run and module not in ['SWIG'] and module not in groups["ThirdParty"] and sourceAPI.UpdateLibraryExport(group,module,otbDir):
              print fancy_module+" - Patching main CMakeLists.txt to fix library export"

  return changes_required
Exemplo n.º 3
0
def main(argv):
    otbDir = op.abspath(argv[1])
    targetModule = argv[2]
    targetGroup = "TBD"
    if targetModule.count('/') == 1:
        pos = targetModule.find('/')
        targetGroup = targetModule[0:pos]
        targetModule = targetModule[pos + 1:]
    if targetModule == "":
        print "Wrong module name, check input argument : " + argv[2]
    if targetGroup == "":
        print "Wrong group name, check input argument : " + argv[2]

    print "Target module: " + targetGroup + "/" + targetModule

    srcFiles = []
    for item in argv[3:]:
        src = item
        if not src.startswith("Modules/"):
            src = "Modules/" + src
        if op.isfile(op.join(otbDir, src)):
            srcFiles.append(src.strip('./'))
        else:
            print "Input source file not found, will be skipped : " + src

    # First, analyse current OTB tree, retrieve :
    #  - module list
    #  - group vs module association
    modulesRoot = op.join(otbDir, "Modules")

    [depList, optDepList,
     testDepList] = sourceAPI.parseOTBModuleCmake(modulesRoot)
    #fullDepList = manifestParser.buildFullDep(depList)
    #oldCleanDepList = sourceAPI.cleanDepList(depList,fullDepList)

    [groups, moduleList, sourceList,
     testList] = sourceAPI.parseModuleRoot(modulesRoot)

    # DEBUG
    #manifestParser.printDepList(depList)
    #print str(moduleList)

    # Second, operate the move
    if targetModule in moduleList:
        targetGroup = manifestParser.getGroup(targetModule, groups)
    else:
        if targetGroup == 'TBD':
            print "Error : group name must be specified for new modules (use group/module syntax)"
            return 1

    destinationPrefix = op.join(targetGroup, targetModule)
    for srcFile in srcFiles:
        cleanFile = srcFile.strip('./')
        words = cleanFile.split('/')
        srcMod = words[2]
        srcGrp = words[1]
        targetFile = cleanFile.replace(srcGrp + '/' + srcMod,
                                       destinationPrefix, 1)
        targetPath = op.join(otbDir, op.dirname(targetFile))
        if not op.isdir(targetPath):
            os.makedirs(targetPath)
        shutil.move(op.join(otbDir, cleanFile), targetPath)

    # Compute new modules dependencies
    [newGroups, newModuleList, newSourceList,
     newTestList] = sourceAPI.parseModuleRoot(modulesRoot)
    newDepList = sourceAPI.buildModularDep(otbDir, newModuleList,
                                           newSourceList)
    newTestDepList = sourceAPI.buildModularDep(otbDir, newTestList,
                                               newSourceList)

    # compute full dependencies
    newFullDepList = manifestParser.buildFullDep(newDepList)

    # detect cyclic dependencies
    cyclicDependentModules = []
    for mod in newFullDepList.keys():
        if mod in newFullDepList[mod]:
            if not mod in cyclicDependentModules:
                cyclicDependentModules.append(mod)
    if len(cyclicDependentModules) > 0:
        print "Check for cyclic dependency : Failed"
        for m in cyclicDependentModules:
            cycle = sourceAPI.getCyclicDep(m, newDepList)
            print "Cycle for module " + m + ": " + str(cycle)
            if len(cycle) > 0:
                a = m
                for c in cycle:
                    b = c
                    print a + " -> " + b + ": "
                    for dep in newDepList[a][b]:
                        print "\t" + dep["from"] + " -> " + dep["to"]
                    a = c

        #manifestParser.printDepList(newDepList,cyclicDependentModules)
        return 1
    else:
        print "Check for cyclic dependency : Passed"

    # fix srcList and test declaration
    os.chdir(otbDir)
    for srcFile in srcFiles:
        cleanFile = srcFile.strip('./')
        words = cleanFile.split('/')
        srcMod = words[2]
        srcGrp = words[1]
        srcSub = words[3]
        targetFile = cleanFile.replace(srcGrp + "/" + srcMod,
                                       destinationPrefix, 1)
        # call hg rename -A
        call(["hg", "rename", "-A", cleanFile, targetFile])
        #  - for input files in 'src' : adapt OTBModule_SRC
        if srcSub == "src" and len(words) == 5:
            # remove entry in previous module
            removed = [op.basename(srcFile)]
            added = []
            cmakelistPath = op.join(modulesRoot, srcGrp, srcMod, srcSub,
                                    "CMakeLists.txt")
            # Check that there are some source file remaining in src dir
            source_files = filter(
                os.path.isfile,
                glob.glob(
                    op.join(modulesRoot, srcGrp, srcMod, srcSub) + '/*.c*'))
            if len(source_files) > 0:
                sourceAPI.updateSourceList(cmakelistPath,
                                           "OTB" + srcMod + "_SRC", added,
                                           removed)
            else:
                # There are no more sources here, the whole src module can be removed
                print "Removing " + srcSub + " dir in " + srcGrp + "/" + srcMod + ", since it does not contain source files anymore"
                call([
                    "hg", "remove",
                    op.join(modulesRoot, srcGrp, srcMod, srcSub) + "/*"
                ])
            # add entry in target module
            removed = []
            added = [op.basename(srcFile)]
            cmakelistPath = op.join(modulesRoot, destinationPrefix, srcSub,
                                    "CMakeLists.txt")
            if not op.exists(cmakelistPath):
                sourceAPI.initializeSrcCMakeLists(cmakelistPath, targetModule)
                call(["hg", "add", cmakelistPath.replace(otbDir, ".")])
            sourceAPI.updateSourceList(cmakelistPath,
                                       "OTB" + targetModule + "_SRC", added,
                                       removed)

        #  -for input files in 'app'
        if srcSub == "app" and len(words) == 5:
            cmakelistPath = op.join(modulesRoot, srcGrp, srcMod, srcSub,
                                    "CMakeLists.txt")
            nextCmakelistPath = op.join(modulesRoot, destinationPrefix, srcSub,
                                        "CMakeLists.txt")
            if not op.exists(nextCmakelistPath):
                sourceAPI.initializeAppCMakeLists(nextCmakelistPath,
                                                  targetModule)
                call(["hg", "add", nextCmakelistPath.replace(otbDir, ".")])
            # move the 'otb_create_application' bloc
            appName = sourceAPI.moveAppBloc(cmakelistPath, nextCmakelistPath,
                                            op.basename(srcFile))

            source_files = filter(
                os.path.isfile,
                glob.glob(
                    op.join(modulesRoot, srcGrp, srcMod, srcSub) + '/*.c*'))
            if len(source_files) == 0:
                # There are no more sources here, the whole src module can be removed
                print "Removing " + srcSub + " dir in " + srcGrp + "/" + srcMod + ", since it does not contain source files anymore"
                call([
                    "hg", "remove",
                    op.join(modulesRoot, srcGrp, srcMod, srcSub) + "/*"
                ])

            # move tests
            testCmakelistPath = op.join(modulesRoot, srcGrp, srcMod, "test",
                                        "CMakeLists.txt")
            nextTestCmakelistPath = op.join(modulesRoot, destinationPrefix,
                                            "test", "CMakeLists.txt")
            appTestBlocs = {}
            if op.exists(testCmakelistPath):
                appTestBlocs = sourceAPI.extractAppTestBlocs(
                    testCmakelistPath, appName)

            if len(appTestBlocs) > 0:
                if not op.exists(nextTestCmakelistPath):
                    if not op.exists(op.dirname(nextTestCmakelistPath)):
                        os.makedirs(op.dirname(nextTestCmakelistPath))
                    fd = open(nextTestCmakelistPath, 'w')
                    fd.write("otb_module_test()\n")
                    fd.write("\n")
                    fd.close()
                    call([
                        "hg", "add",
                        nextTestCmakelistPath.replace(otbDir, ".")
                    ])

                fd = open(nextTestCmakelistPath, 'a')
                fd.write("#----------- " + appName +
                         " TESTS ----------------\n")
                for tname in appTestBlocs:
                    fd.writelines(appTestBlocs[tname])
                    fd.write("\n")
                fd.close()

        if srcSub == "test" and len(words) == 5:
            # analyse test
            res = createTestManifest.parseTestCxx(op.join(otbDir, targetFile))

            if res["hasMain"]:
                print "Test with main (" + targetFile + ") : not handled for now"
            else:
                # remove entry in previous module source list
                removed = [op.basename(srcFile)]
                added = []
                oldCmakelistPath = op.join(
                    modulesRoot,
                    op.join(srcGrp, op.join(srcMod, "test/CMakeLists.txt")))
                oldTestDriverPath = op.join(
                    modulesRoot,
                    op.join(
                        srcGrp,
                        op.join(srcMod,
                                "test/otb" + srcMod + "TestDriver.cxx")))
                sourceAPI.updateSourceList(oldCmakelistPath,
                                           "OTB" + srcMod + "Tests", added,
                                           removed)

                # add entry in target module source list
                removed = []
                added = [op.basename(srcFile)]
                cmakelistPath = op.join(
                    modulesRoot,
                    op.join(destinationPrefix, "test/CMakeLists.txt"))
                testDriverPath = op.join(
                    modulesRoot,
                    op.join(destinationPrefix,
                            "test/otb" + targetModule + "TestDriver.cxx"))
                if not op.exists(cmakelistPath):
                    # there was no test before : initialize CMakeLists.txt
                    sourceAPI.initializeTestCMakeLists(cmakelistPath,
                                                       targetModule)
                    call(["hg", "add", cmakelistPath.replace(otbDir, ".")])
                if not op.exists(testDriverPath):
                    # there was no test before : initialize test driver
                    sourceAPI.initializeTestDriver(testDriverPath)
                    call(["hg", "add", testDriverPath.replace(otbDir, ".")])
                sourceAPI.updateSourceList(cmakelistPath,
                                           "OTB" + targetModule + "Tests",
                                           added, removed)

                #  - move test declaration
                testCode = dispatchTests.findTestFromExe(
                    oldCmakelistPath, "otb" + srcMod + "TestDriver", "",
                    res["testFunctions"], 'otb_')

                # get set_test_properties()
                testProp = dispatchTests.findTestProperties(
                    oldCmakelistPath, testCode.keys())

                print "Found " + str(len(testCode)) + " tests to move"

                sourceAPI.moveTestCode(oldCmakelistPath, cmakelistPath,
                                       testCode, testProp)

    for mod in newModuleList:
        curGroup = manifestParser.getGroup(mod, newGroups)

        # fix the otb-module.cmake
        cmake_module_path = op.join(
            modulesRoot, op.join(curGroup, op.join(mod, "otb-module.cmake")))
        cmakel_module_path = op.join(
            modulesRoot, op.join(curGroup, op.join(mod, "CMakeLists.txt")))
        if (not op.exists(cmake_module_path)) and (mod == targetModule):
            # initialize new otb-module.cmake if new module
            sourceAPI.initializeOTBModuleCmake(cmake_module_path, mod)
            with_lib = False
            if op.exists(op.join(curGroup, op.join(mod, "src"))):
                with_lib = True
            sourceAPI.initializeOTBModuleCMakeLists(cmakel_module_path, mod,
                                                    with_lib)
            call(["hg", "add", cmakel_module_path.replace(otbDir, ".")])

            call(["hg", "add", cmake_module_path.replace(otbDir, ".")])

        #  - fix the target_link_libraries
        src_dir = op.join(modulesRoot, curGroup, mod, "src")

        sub_src_CMakeList = op.join(
            modulesRoot, op.join(curGroup, op.join(mod, "src/CMakeLists.txt")))

    # Update the module dependencies
    print "Updating module dependencies ..."
    count = updateModuleDependencies.update(otbDir)
    print str(count) + " modules were updated"

    # Update the module test driver
    print "Updating test drivers ..."
    count = updateTestDrivers.update(otbDir)
    print str(count) + " files updated"

    # Update the group declaration in doxygen
    print "Updating doxygen group declaration ..."
    updateDoxyGroup.update(otbDir)

    # TODO : hg commit by the user
    print "\nTo commit those changes, run: hg commit -m \"ENH: Automatic move of files to module " + targetGroup + "/" + targetModule + "\"\n"
    print "It is advised to run updateModuleDependencies.py script beforehand.\n"