Clang Project

clang_source_code/utils/analyzer/SATestUtils.py
1import os
2from subprocess import check_output, check_call
3import sys
4
5
6Verbose = 1
7
8def which(command, paths=None):
9    """which(command, [paths]) - Look up the given command in the paths string
10    (or the PATH environment variable, if unspecified)."""
11
12    if paths is None:
13        paths = os.environ.get('PATH', '')
14
15    # Check for absolute match first.
16    if os.path.exists(command):
17        return command
18
19    # Would be nice if Python had a lib function for this.
20    if not paths:
21        paths = os.defpath
22
23    # Get suffixes to search.
24    # On Cygwin, 'PATHEXT' may exist but it should not be used.
25    if os.pathsep == ';':
26        pathext = os.environ.get('PATHEXT', '').split(';')
27    else:
28        pathext = ['']
29
30    # Search the paths...
31    for path in paths.split(os.pathsep):
32        for ext in pathext:
33            p = os.path.join(path, command + ext)
34            if os.path.exists(p):
35                return p
36
37    return None
38
39
40def hasNoExtension(FileName):
41    (Root, Ext) = os.path.splitext(FileName)
42    return (Ext == "")
43
44
45def isValidSingleInputFile(FileName):
46    (Root, Ext) = os.path.splitext(FileName)
47    return Ext in (".i", ".ii", ".c", ".cpp", ".m", "")
48
49
50def getSDKPath(SDKName):
51    """
52    Get the path to the SDK for the given SDK name. Returns None if
53    the path cannot be determined.
54    """
55    if which("xcrun") is None:
56        return None
57
58    Cmd = "xcrun --sdk " + SDKName + " --show-sdk-path"
59    return check_output(Cmd, shell=True).rstrip()
60
61
62def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
63              Stderr=sys.stderr):
64    """
65    Run the provided script if it exists.
66    """
67    if os.path.exists(ScriptPath):
68        try:
69            if Verbose == 1:
70                Stdout.write("  Executing: %s\n" % (ScriptPath,))
71            check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
72                       stderr=PBuildLogFile,
73                       stdout=PBuildLogFile,
74                       shell=True)
75            check_call("'%s'" % ScriptPath, cwd=Cwd,
76                       stderr=PBuildLogFile,
77                       stdout=PBuildLogFile,
78                       shell=True)
79        except:
80            Stderr.write("Error: Running %s failed. See %s for details.\n" % (
81                         ScriptPath, PBuildLogFile.name))
82            sys.exit(-1)
83
84
85def isCommentCSVLine(Entries):
86    """
87    Treat CSV lines starting with a '#' as a comment.
88    """
89    return len(Entries) > 0 and Entries[0].startswith("#")
90