1import os
2from subprocess import 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 runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
51              Stderr=sys.stderr):
52    """
53    Run the provided script if it exists.
54    """
55    if os.path.exists(ScriptPath):
56        try:
57            if Verbose == 1:
58                Stdout.write("  Executing: %s\n" % (ScriptPath,))
59            check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
60                       stderr=PBuildLogFile,
61                       stdout=PBuildLogFile,
62                       shell=True)
63            check_call("'%s'" % ScriptPath, cwd=Cwd,
64                       stderr=PBuildLogFile,
65                       stdout=PBuildLogFile,
66                       shell=True)
67        except:
68            Stderr.write("Error: Running %s failed. See %s for details.\n" % (
69                         ScriptPath, PBuildLogFile.name))
70            sys.exit(-1)
71
72
73def isCommentCSVLine(Entries):
74    """
75    Treat CSV lines starting with a '#' as a comment.
76    """
77    return len(Entries) > 0 and Entries[0].startswith("#")
78