OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Module for build host support.""" | 5 """Module for build host support.""" |
6 | 6 |
7 import os | 7 import os |
8 import pipes | 8 import pipes |
9 import subprocess | 9 import subprocess |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 """ | 34 """ |
35 return False | 35 return False |
36 | 36 |
37 @classmethod | 37 @classmethod |
38 def Select(cls, context): | 38 def Select(cls, context): |
39 for host in cls.Plugins(): | 39 for host in cls.Plugins(): |
40 if host.Matches(): | 40 if host.Matches(): |
41 return host | 41 return host |
42 | 42 |
43 def _Execute(self, context, command, | 43 def _Execute(self, context, command, |
44 shell=False, capture=False, ignore_dry_run=False, | 44 shell=False, capture=False, silent=False, |
45 return_status=False): | 45 ignore_dry_run=False, return_status=False): |
46 """This is the only method that launches external programs. | 46 """This is the only method that launches external programs. |
47 | 47 |
48 It is a thin wrapper around subprocess.Popen that handles cr specific | 48 It is a thin wrapper around subprocess.Popen that handles cr specific |
49 issues. The command is expanded in the context, so that context variables | 49 issues. The command is expanded in the context, so that context variables |
50 are substituted. | 50 are substituted. |
51 Args: | 51 Args: |
52 context: the cr context to run under. | 52 context: the cr context to run under. |
53 command: the command to run. | 53 command: the command to run. |
54 shell: whether to run the command using the shell. | 54 shell: whether to run the command using the shell. |
55 capture: controls wether the output of the command is captured. | 55 capture: controls wether the output of the command is captured. |
(...skipping 15 matching lines...) Expand all Loading... |
71 if context.verbose: | 71 if context.verbose: |
72 print ' '.join(command) | 72 print ' '.join(command) |
73 if context.verbose >= _TRAIL_VERBOSITY: | 73 if context.verbose >= _TRAIL_VERBOSITY: |
74 print 'Command expanded the following variables:' | 74 print 'Command expanded the following variables:' |
75 for key, value in trail: | 75 for key, value in trail: |
76 print ' ', key, '=', value | 76 print ' ', key, '=', value |
77 if ignore_dry_run or not context.dry_run: | 77 if ignore_dry_run or not context.dry_run: |
78 out = None | 78 out = None |
79 if capture: | 79 if capture: |
80 out = subprocess.PIPE | 80 out = subprocess.PIPE |
| 81 elif silent: |
| 82 out = open(os.devnull, "w") |
81 try: | 83 try: |
82 p = subprocess.Popen( | 84 p = subprocess.Popen( |
83 command, shell=shell, | 85 command, shell=shell, |
84 env={k: str(v) for k, v in context.exported.items()}, | 86 env={k: str(v) for k, v in context.exported.items()}, |
85 stdout=out) | 87 stdout=out) |
86 except OSError: | 88 except OSError: |
87 print 'Failed to exec', command | 89 print 'Failed to exec', command |
88 # Don't log the trail if we already have | 90 # Don't log the trail if we already have |
89 if context.verbose < _TRAIL_VERBOSITY: | 91 if context.verbose < _TRAIL_VERBOSITY: |
90 print 'Variables used to build the command were:' | 92 print 'Variables used to build the command were:' |
91 for key, value in trail: | 93 for key, value in trail: |
92 print ' ', key, '=', value | 94 print ' ', key, '=', value |
93 exit(1) | 95 exit(1) |
94 try: | 96 try: |
95 output, _ = p.communicate() | 97 output, _ = p.communicate() |
96 except KeyboardInterrupt: | 98 except KeyboardInterrupt: |
97 p.terminate() | 99 p.terminate() |
98 p.wait() | 100 p.wait() |
99 exit(1) | 101 exit(1) |
| 102 finally: |
| 103 if silent: |
| 104 out.close() |
100 if return_status: | 105 if return_status: |
101 return p.returncode | 106 return p.returncode |
102 if p.returncode != 0: | 107 if p.returncode != 0: |
103 print 'Error {0} executing command {1}'.format(p.returncode, command) | 108 print 'Error {0} executing command {1}'.format(p.returncode, command) |
104 exit(p.returncode) | 109 exit(p.returncode) |
105 return output or '' | 110 return output or '' |
106 return '' | 111 return '' |
107 | 112 |
108 @cr.Plugin.activemethod | 113 @cr.Plugin.activemethod |
109 def Shell(self, context, *command): | 114 def Shell(self, context, *command): |
110 command = ' '.join([pipes.quote(arg) for arg in command]) | 115 command = ' '.join([pipes.quote(arg) for arg in command]) |
111 return self._Execute(context, [command], shell=True) | 116 return self._Execute(context, [command], shell=True) |
112 | 117 |
113 @cr.Plugin.activemethod | 118 @cr.Plugin.activemethod |
114 def Execute(self, context, *command): | 119 def Execute(self, context, *command): |
115 return self._Execute(context, command, shell=False) | 120 return self._Execute(context, command, shell=False) |
116 | 121 |
117 @cr.Plugin.activemethod | 122 @cr.Plugin.activemethod |
| 123 def ExecuteSilently(self, context, *command): |
| 124 return self._Execute(context, command, shell=False, silent=True) |
| 125 |
| 126 @cr.Plugin.activemethod |
118 def CaptureShell(self, context, *command): | 127 def CaptureShell(self, context, *command): |
119 return self._Execute(context, command, | 128 return self._Execute(context, command, |
120 shell=True, capture=True, ignore_dry_run=True) | 129 shell=True, capture=True, ignore_dry_run=True) |
121 | 130 |
122 @cr.Plugin.activemethod | 131 @cr.Plugin.activemethod |
123 def Capture(self, context, *command): | 132 def Capture(self, context, *command): |
124 return self._Execute(context, command, capture=True, ignore_dry_run=True) | 133 return self._Execute(context, command, capture=True, ignore_dry_run=True) |
125 | 134 |
126 @cr.Plugin.activemethod | 135 @cr.Plugin.activemethod |
127 def ExecuteStatus(self, context, *command): | 136 def ExecuteStatus(self, context, *command): |
(...skipping 12 matching lines...) Expand all Loading... |
140 result = [] | 149 result = [] |
141 extensions = [''] | 150 extensions = [''] |
142 extensions.extend(os.environ.get('PATHEXT', '').split(os.pathsep)) | 151 extensions.extend(os.environ.get('PATHEXT', '').split(os.pathsep)) |
143 for path in os.environ.get('PATH', '').split(os.pathsep): | 152 for path in os.environ.get('PATH', '').split(os.pathsep): |
144 partial = os.path.join(path, name) | 153 partial = os.path.join(path, name) |
145 for extension in extensions: | 154 for extension in extensions: |
146 filename = partial + extension | 155 filename = partial + extension |
147 if os.path.exists(filename) and filename not in result: | 156 if os.path.exists(filename) and filename not in result: |
148 result.append(filename) | 157 result.append(filename) |
149 return result | 158 return result |
OLD | NEW |