#!/usr/bin/env python

"""
Static Analyzer qualification infrastructure: adding a new project to 
the Repository Directory.

 Add a new project for testing: build it and add to the Project Map file.
   Assumes it's being run from the Repository Directory.
   The project directory should be added inside the Repository Directory and 
   have the same name as the project ID
   
 The project should use the following files for set up:
      - pre_run_static_analyzer.sh - prepare the build environment.
                                     Ex: make clean can be a part of it.
      - run_static_analyzer.cmd - a list of commands to run through scan-build.
                                     Each command should be on a separate line.
                                     Choose from: configure, make, xcodebuild 
"""
import SATestBuild

import os
import csv
import sys

# Add a new project for testing: build it and add to the Project Map file.
# Params:
#   Dir is the directory where the sources are.
#   ID is a short string used to identify a project.
def addNewProject(ID, IsScanBuild) :
    CurDir = os.path.abspath(os.curdir)
    Dir = SATestBuild.getProjectDir(ID)
    if not os.path.exists(Dir):
        print "Error: Project directory is missing: %s" % Dir
        sys.exit(-1)
        
    # Build the project.
    SATestBuild.testProject(ID, True, IsScanBuild, Dir)

    # Add the project ID to the project map.
    ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
    if os.path.exists(ProjectMapPath):
        PMapFile = open(ProjectMapPath, "r+b")
    else:
        print "Warning: Creating the Project Map file!!"
        PMapFile = open(ProjectMapPath, "w+b")
    try:
        PMapReader = csv.reader(PMapFile)
        for I in PMapReader:
            IID = I[0]
            if ID == IID:
                print >> sys.stderr, 'Warning: Project with ID \'', ID, \
                        '\' already exists.'
                sys.exit(-1)

        PMapWriter = csv.writer(PMapFile)
        PMapWriter.writerow( (ID, int(IsScanBuild)) );
    finally:
        PMapFile.close()
        
    print "The project map is updated: ", ProjectMapPath
            

# TODO: Add an option not to build. 
# TODO: Set the path to the Repository directory.
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print >> sys.stderr, 'Usage: ', sys.argv[0],\
                             'project_ID <mode>' \
                             'mode - 0 for single file project; 1 for scan_build'
        sys.exit(-1)
    
    IsScanBuild = 1    
    if (len(sys.argv) >= 3):
        IsScanBuild = int(sys.argv[2])  
    assert((IsScanBuild == 0) | (IsScanBuild == 1))
        
    addNewProject(sys.argv[1], IsScanBuild)

@@ -864,16 +864,200 @@
 rt sys%0A%0A
+def isExistingProject(PMapFile, projectID) :%0A    PMapReader = csv.reader(PMapFile)%0A    for I in PMapReader:%0A        if projectID == I%5B0%5D:%0A            return True%0A    return False    %0A%0A
 # Add a 
@@ -1893,124 +1893,55 @@
     
-PMapReader = csv.reader(PMapFile)%0A        for I in PMapReader:%0A            IID = I%5B0%5D%0A            if ID == IID:%0A    
+if (isExistingProject(PMapFile, ID)) :        %0A
     
@@ -1964,19 +1964,19 @@
  sys.std
-err
+out
 , 'Warni
@@ -2030,16 +2030,25 @@
         
+         
 '%5C' alre
@@ -2072,33 +2072,112 @@
         
-    sys.exit(-1)%0A
+print %3E%3E sys.stdout, %22Reference output has been regenerated.%22%0A        else:                     
 %0A       
@@ -2169,32 +2169,36 @@
        %0A        
+    
 PMapWriter = csv
@@ -2211,24 +2211,28 @@
 r(PMapFile)%0A
+    
         PMap
@@ -2282,51 +2282,12 @@
     
-finally:%0A        PMapFile.close()%0A        %0A
+    
     
@@ -2339,16 +2339,54 @@
 MapPath%0A
+    finally:%0A        PMapFile.close()%0A
         

support for regenerate reference output