Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2014 Google Inc. All rights reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """Simple script that locates the newest MSBuild in one of several locations. |
| 17 | |
| 18 | This script will find the highest version number of MSBuild and run it, |
| 19 | passing its arguments through to MSBuild. |
| 20 | """ |
| 21 | |
| 22 | import glob |
| 23 | import os |
| 24 | import re |
| 25 | import string |
| 26 | import subprocess |
| 27 | import sys |
| 28 | |
| 29 | SYSTEMROOT = os.getenv("SYSTEMROOT", "c:\\windows") |
| 30 | PROGRAM_FILES = os.getenv("ProgramFiles", "c:\\Program Files") |
| 31 | PROGRAM_FILES_X86 = os.getenv("ProgramFiles(x86)", "c:\\Program Files (x86)") |
| 32 | |
| 33 | SEARCH_FOLDERS = [ PROGRAM_FILES + "\\MSBuild\\*\\Bin\\MSBuild.exe", |
| 34 | PROGRAM_FILES_X86 + "\\MSBuild\\*\\Bin\\MSBuild.exe", |
| 35 | SYSTEMROOT + "\\Microsoft.NET\Framework\\*\\MSBuild.exe" ] |
| 36 | |
| 37 | def compare_version(a, b): |
| 38 | """Compare two version number strings of the form W.X.Y.Z. |
| 39 | |
| 40 | The numbers are compared most-significant to least-significant. |
| 41 | For example, 12.345.67.89 > 2.987.88.99. |
| 42 | |
| 43 | Args: |
| 44 | a: First version number string to compare |
| 45 | b: Second version number string to compare |
| 46 | |
| 47 | Returns: |
| 48 | 0 if the numbers are identical, a positive number if 'a' is larger, and |
| 49 | a negative number if 'b' is larger. |
| 50 | """ |
| 51 | aa = string.split(a, ".") |
| 52 | bb = string.split(b, ".") |
| 53 | for i in range(0, 4): |
| 54 | if aa[i] != bb[i]: |
| 55 | return cmp(int(aa[i]), int(bb[i])) |
| 56 | return 0 |
| 57 | |
| 58 | def main(): |
| 59 | msbuilds = [] |
| 60 | |
| 61 | for folder in SEARCH_FOLDERS: |
| 62 | for file in glob.glob(folder): |
| 63 | p = subprocess.Popen([file, "/version"], stdout=subprocess.PIPE) |
| 64 | out, err = p.communicate() |
| 65 | match = re.search("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$", out, re.M) |
| 66 | if match: |
| 67 | msbuilds.append({ 'ver':match.group(), 'exe':file }) |
| 68 | msbuilds.sort(lambda x, y: compare_version(x['ver'], y['ver']), reverse=True) |
| 69 | if len(msbuilds) == 0: |
| 70 | print "Unable to find MSBuild.\n" |
| 71 | return -1; |
| 72 | cmd = [msbuilds[0]['exe']] |
| 73 | cmd.extend(sys.argv[1:]) |
| 74 | return subprocess.call(cmd) |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | sys.exit(main()) |