OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
3 # for details. All rights reserved. Use of this source code is governed by a | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 """Used to run pub before the SDK has been built""" | |
6 | |
7 import argparse | |
8 import os | |
9 import platform | |
10 import subprocess | |
11 import sys | |
12 | |
13 SCRIPT_DIR = os.path.dirname(sys.argv[0]) | |
14 DART_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..')) | |
15 PUB_PATH = os.path.join(DART_ROOT, 'third_party/pkg/pub/bin/pub.dart') | |
16 CANARY_PATH = os.path.join(DART_ROOT, 'tools', 'canary.dart') | |
17 | |
18 usage = """run_pub.py --package-root=<package root>""" | |
19 | |
20 def BuildArguments(): | |
21 result = argparse.ArgumentParser(usage=usage) | |
22 result.add_argument("--package-root", help="package root", default=None) | |
23 result.add_argument("--dart-executable", help="dart binary", default=None) | |
24 return result | |
25 | |
26 def ProcessOptions(options, args): | |
27 return ((options.package_root != None) and | |
28 (options.dart_executable != None)) | |
29 | |
30 def GetPrebuiltDartExecutablePath(suffix): | |
31 osdict = {'Darwin':'macos', 'Linux':'linux', 'Windows':'windows'} | |
32 system = platform.system() | |
33 executable_name = 'dart' | |
34 if system == 'Windows': | |
35 executable_name = 'dart.exe' | |
36 try: | |
37 osname = osdict[system] | |
38 except KeyError: | |
39 print >>sys.stderr, ('WARNING: platform "%s" not supported') % (system) | |
40 return None; | |
41 return os.path.join(DART_ROOT, | |
42 'tools', | |
43 'testing', | |
44 'bin', | |
45 osname, | |
46 executable_name + suffix) | |
47 | |
48 def RunPub(dart, pkg_root, args): | |
49 return subprocess.call([dart, '--package-root=' + pkg_root, PUB_PATH] + args) | |
50 | |
51 def TryRunningExecutable(dart_executable, pkg_root): | |
52 try: | |
53 return subprocess.call([dart_executable, | |
54 '--package-root=' + pkg_root, | |
55 CANARY_PATH]) == 42 | |
56 except: | |
57 return False; | |
58 | |
59 def DisplayBootstrapWarning(): | |
60 print """\ | |
61 | |
62 | |
63 WARNING: Your system cannot run the prebuilt Dart executable. Using the | |
64 bootstrap Dart executable will make Debug builds long. | |
65 Please see Wiki for instructions on replacing prebuilt Dart executable. | |
66 | |
67 https://code.google.com/p/dart/wiki/ReplacingPrebuiltDartExecutable | |
68 | |
69 """ | |
70 | |
71 def FindDartExecutable(fallback_executable, package_root): | |
72 # If requested, use the bootstrap binary instead of the prebuilt | |
73 # executable. | |
74 if os.getenv('DART_USE_BOOTSTRAP_BIN') != None: | |
75 return fallback_executable | |
76 # Try to find a working prebuilt dart executable. | |
77 dart_executable = GetPrebuiltDartExecutablePath('') | |
78 if TryRunningExecutable(dart_executable, package_root): | |
79 return dart_executable | |
80 dart_executable = GetPrebuiltDartExecutablePath('-arm') | |
81 if TryRunningExecutable(dart_executable, package_root): | |
82 return dart_executable | |
83 dart_executable = GetPrebuiltDartExecutablePath('-mips') | |
84 if TryRunningExecutable(dart_executable, package_root): | |
85 return dart_executable | |
86 # If the system cannot execute a prebuilt dart executable, use the bootstrap | |
87 # executable instead. | |
88 DisplayBootstrapWarning() | |
89 return fallback_executable | |
90 | |
91 def main(): | |
92 # Parse the options. | |
93 parser = BuildArguments() | |
94 (options, args) = parser.parse_known_args() | |
95 if not ProcessOptions(options, args): | |
96 parser.print_help() | |
97 return 1 | |
98 dart_executable = FindDartExecutable(options.dart_executable, | |
99 options.package_root) | |
100 return RunPub(dart_executable, options.package_root, args) | |
101 | |
102 | |
103 if __name__ == '__main__': | |
104 sys.exit(main()) | |
OLD | NEW |