1#!/usr/bin/env python
2
3"""
4Static Analyzer qualification infrastructure: adding a new project to
5the Repository Directory.
6
7 Add a new project for testing: build it and add to the Project Map file.
8   Assumes it's being run from the Repository Directory.
9   The project directory should be added inside the Repository Directory and
10   have the same name as the project ID
11
12 The project should use the following files for set up:
13      - pre_run_static_analyzer.sh - prepare the build environment.
14                                     Ex: make clean can be a part of it.
15      - run_static_analyzer.cmd - a list of commands to run through scan-build.
16                                     Each command should be on a separate line.
17                                     Choose from: configure, make, xcodebuild
18"""
19import SATestBuild
20
21import os
22import csv
23import sys
24
25def isExistingProject(PMapFile, projectID) :
26    PMapReader = csv.reader(PMapFile)
27    for I in PMapReader:
28        if projectID == I[0]:
29            return True
30    return False
31
32# Add a new project for testing: build it and add to the Project Map file.
33# Params:
34#   Dir is the directory where the sources are.
35#   ID is a short string used to identify a project.
36def addNewProject(ID, BuildMode) :
37    CurDir = os.path.abspath(os.curdir)
38    Dir = SATestBuild.getProjectDir(ID)
39    if not os.path.exists(Dir):
40        print "Error: Project directory is missing: %s" % Dir
41        sys.exit(-1)
42
43    # Build the project.
44    SATestBuild.testProject(ID, BuildMode, IsReferenceBuild=True, Dir=Dir)
45
46    # Add the project ID to the project map.
47    ProjectMapPath = os.path.join(CurDir, SATestBuild.ProjectMapFile)
48    if os.path.exists(ProjectMapPath):
49        PMapFile = open(ProjectMapPath, "r+b")
50    else:
51        print "Warning: Creating the Project Map file!!"
52        PMapFile = open(ProjectMapPath, "w+b")
53    try:
54        if (isExistingProject(PMapFile, ID)) :
55            print >> sys.stdout, 'Warning: Project with ID \'', ID, \
56                                 '\' already exists.'
57            print >> sys.stdout, "Reference output has been regenerated."
58        else:
59            PMapWriter = csv.writer(PMapFile)
60            PMapWriter.writerow( (ID, int(BuildMode)) );
61            print "The project map is updated: ", ProjectMapPath
62    finally:
63        PMapFile.close()
64
65
66# TODO: Add an option not to build.
67# TODO: Set the path to the Repository directory.
68if __name__ == '__main__':
69    if len(sys.argv) < 2:
70        print >> sys.stderr, 'Usage: ', sys.argv[0],\
71                             'project_ID <mode>' \
72                             'mode - 0 for single file project; ' \
73                             '1 for scan_build; ' \
74                             '2 for single file c++11 project'
75        sys.exit(-1)
76
77    BuildMode = 1
78    if (len(sys.argv) >= 3):
79        BuildMode = int(sys.argv[2])
80    assert((BuildMode == 0) | (BuildMode == 1) | (BuildMode == 2))
81
82    addNewProject(sys.argv[1], BuildMode)
83