George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 1 | import os |
Artem Dergachev | e8b29c0 | 2019-05-29 18:49:31 +0000 | [diff] [blame] | 2 | from subprocess import check_call |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 3 | import sys |
| 4 | |
| 5 | |
| 6 | Verbose = 1 |
| 7 | |
Valeriy Savchenko | c98872e | 2020-05-14 13:31:01 +0300 | [diff] [blame] | 8 | |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 9 | def 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 Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 41 | def hasNoExtension(FileName): |
| 42 | (Root, Ext) = os.path.splitext(FileName) |
| 43 | return (Ext == "") |
| 44 | |
| 45 | |
| 46 | def isValidSingleInputFile(FileName): |
| 47 | (Root, Ext) = os.path.splitext(FileName) |
| 48 | return Ext in (".i", ".ii", ".c", ".cpp", ".m", "") |
| 49 | |
| 50 | |
George Karpenkov | f37d3a5 | 2018-02-08 21:22:42 +0000 | [diff] [blame] | 51 | def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout, |
| 52 | Stderr=sys.stderr): |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 53 | """ |
| 54 | Run the provided script if it exists. |
| 55 | """ |
| 56 | if os.path.exists(ScriptPath): |
| 57 | try: |
| 58 | if Verbose == 1: |
George Karpenkov | f37d3a5 | 2018-02-08 21:22:42 +0000 | [diff] [blame] | 59 | Stdout.write(" Executing: %s\n" % (ScriptPath,)) |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 60 | 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 Karpenkov | f37d3a5 | 2018-02-08 21:22:42 +0000 | [diff] [blame] | 69 | Stderr.write("Error: Running %s failed. See %s for details.\n" % ( |
| 70 | ScriptPath, PBuildLogFile.name)) |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 71 | sys.exit(-1) |
| 72 | |
| 73 | |
George Karpenkov | bf92c44 | 2017-10-24 23:52:48 +0000 | [diff] [blame] | 74 | def isCommentCSVLine(Entries): |
| 75 | """ |
| 76 | Treat CSV lines starting with a '#' as a comment. |
| 77 | """ |
| 78 | return len(Entries) > 0 and Entries[0].startswith("#") |