blob: 0ed4a4b02f793efc880d3e3d16bb2efada101182 [file] [log] [blame]
George Karpenkovbf92c442017-10-24 23:52:48 +00001import os
Artem Dergacheve8b29c02019-05-29 18:49:31 +00002from subprocess import check_call
George Karpenkovbf92c442017-10-24 23:52:48 +00003import 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
George Karpenkovbf92c442017-10-24 23:52:48 +000040def 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
George Karpenkovf37d3a52018-02-08 21:22:42 +000050def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
51 Stderr=sys.stderr):
George Karpenkovbf92c442017-10-24 23:52:48 +000052 """
53 Run the provided script if it exists.
54 """
55 if os.path.exists(ScriptPath):
56 try:
57 if Verbose == 1:
George Karpenkovf37d3a52018-02-08 21:22:42 +000058 Stdout.write(" Executing: %s\n" % (ScriptPath,))
George Karpenkovbf92c442017-10-24 23:52:48 +000059 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:
George Karpenkovf37d3a52018-02-08 21:22:42 +000068 Stderr.write("Error: Running %s failed. See %s for details.\n" % (
69 ScriptPath, PBuildLogFile.name))
George Karpenkovbf92c442017-10-24 23:52:48 +000070 sys.exit(-1)
71
72
George Karpenkovbf92c442017-10-24 23:52:48 +000073def isCommentCSVLine(Entries):
74 """
75 Treat CSV lines starting with a '#' as a comment.
76 """
77 return len(Entries) > 0 and Entries[0].startswith("#")