blob: b22e22a1fd56c5e5f096c0f34e47b41d6b7571fb [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
2# Copyright (C) 2008 The Android Open Source Project
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
16import re
17import os
18import sys
19import subprocess
20from command import Command
21
22class Forall(Command):
23 common = False
24 helpSummary = "Run a shell command in each project"
25 helpUsage = """
26%prog [<project>...] -c <command> [<arg>...]
27"""
28 helpDescription = """
29Executes the same shell command in each project.
30
31Environment
32-----------
33pwd is the project's working directory.
34
35REPO_PROJECT is set to the unique name of the project.
36
37shell positional arguments ($1, $2, .., $#) are set to any arguments
38following <command>.
39
40stdin, stdout, stderr are inherited from the terminal and are
41not redirected.
42"""
43
44 def _Options(self, p):
45 def cmd(option, opt_str, value, parser):
46 setattr(parser.values, option.dest, list(parser.rargs))
47 while parser.rargs:
48 del parser.rargs[0]
49 p.add_option('-c', '--command',
50 help='Command (and arguments) to execute',
51 dest='command',
52 action='callback',
53 callback=cmd)
54
55 def Execute(self, opt, args):
56 if not opt.command:
57 self.Usage()
58
59 cmd = [opt.command[0]]
60
61 shell = True
62 if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
63 shell = False
64
65 if shell:
66 cmd.append(cmd[0])
67 cmd.extend(opt.command[1:])
68
69 rc = 0
70 for project in self.GetProjects(args):
71 env = dict(os.environ.iteritems())
72 env['REPO_PROJECT'] = project.name
73
74 p = subprocess.Popen(cmd,
75 cwd = project.worktree,
76 shell = shell,
77 env = env)
78 r = p.wait()
79 if r != 0 and r != rc:
80 rc = r
81 if rc != 0:
82 sys.exit(rc)