blob: 079b06a4cbb769817a81598d6771f7c0e0531b5b [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
Valeriy Savchenkoc98872e2020-05-14 13:31:01 +03008
George Karpenkovbf92c442017-10-24 23:52:48 +00009def which(command, paths=None):
10 """which(command, [paths]) - Look up the given command in the paths string
11 (or the PATH environment variable, if unspecified)."""
12
13 if paths is None:
14 paths = os.environ.get('PATH', '')
15
16 # Check for absolute match first.
17 if os.path.exists(command):
18 return command
19
20 # Would be nice if Python had a lib function for this.
21 if not paths:
22 paths = os.defpath
23
24 # Get suffixes to search.
25 # On Cygwin, 'PATHEXT' may exist but it should not be used.
26 if os.pathsep == ';':
27 pathext = os.environ.get('PATHEXT', '').split(';')
28 else:
29 pathext = ['']
30
31 # Search the paths...
32 for path in paths.split(os.pathsep):
33 for ext in pathext:
34 p = os.path.join(path, command + ext)
35 if os.path.exists(p):
36 return p
37
38 return None
39
40
George Karpenkovbf92c442017-10-24 23:52:48 +000041def hasNoExtension(FileName):
42 (Root, Ext) = os.path.splitext(FileName)
43 return (Ext == "")
44
45
46def isValidSingleInputFile(FileName):
47 (Root, Ext) = os.path.splitext(FileName)
48 return Ext in (".i", ".ii", ".c", ".cpp", ".m", "")
49
50
George Karpenkovf37d3a52018-02-08 21:22:42 +000051def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
52 Stderr=sys.stderr):
George Karpenkovbf92c442017-10-24 23:52:48 +000053 """
54 Run the provided script if it exists.
55 """
56 if os.path.exists(ScriptPath):
57 try:
58 if Verbose == 1:
George Karpenkovf37d3a52018-02-08 21:22:42 +000059 Stdout.write(" Executing: %s\n" % (ScriptPath,))
George Karpenkovbf92c442017-10-24 23:52:48 +000060 check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
61 stderr=PBuildLogFile,
62 stdout=PBuildLogFile,
63 shell=True)
64 check_call("'%s'" % ScriptPath, cwd=Cwd,
65 stderr=PBuildLogFile,
66 stdout=PBuildLogFile,
67 shell=True)
68 except:
George Karpenkovf37d3a52018-02-08 21:22:42 +000069 Stderr.write("Error: Running %s failed. See %s for details.\n" % (
70 ScriptPath, PBuildLogFile.name))
George Karpenkovbf92c442017-10-24 23:52:48 +000071 sys.exit(-1)
72
73
George Karpenkovbf92c442017-10-24 23:52:48 +000074def isCommentCSVLine(Entries):
75 """
76 Treat CSV lines starting with a '#' as a comment.
77 """
78 return len(Entries) > 0 and Entries[0].startswith("#")