blob: bc0d455d82b7ebefe2f5f9888534c1fe4d4730ab [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
Shawn O. Pearce44469462009-03-03 17:51:01 -080020from command import Command, MirrorSafeCommand
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021
Shawn O. Pearce44469462009-03-03 17:51:01 -080022class Forall(Command, MirrorSafeCommand):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023 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-----------
Shawn O. Pearce44469462009-03-03 17:51:01 -080033pwd is the project's working directory. If the current client is
34a mirror client, then pwd is the Git repository.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070035
36REPO_PROJECT is set to the unique name of the project.
37
Jeff Baileybe0e8ac2009-01-21 19:05:15 -050038REPO_PATH is the path relative the the root of the client.
39
40REPO_REMOTE is the name of the remote system from the manifest.
41
42REPO_LREV is the name of the revision from the manifest, translated
43to a local tracking branch. If you need to pass the manifest
44revision to a locally executed git command, use REPO_LREV.
45
46REPO_RREV is the name of the revision from the manifest, exactly
47as written in the manifest.
48
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070049shell positional arguments ($1, $2, .., $#) are set to any arguments
50following <command>.
51
52stdin, stdout, stderr are inherited from the terminal and are
53not redirected.
54"""
55
56 def _Options(self, p):
57 def cmd(option, opt_str, value, parser):
58 setattr(parser.values, option.dest, list(parser.rargs))
59 while parser.rargs:
60 del parser.rargs[0]
61 p.add_option('-c', '--command',
62 help='Command (and arguments) to execute',
63 dest='command',
64 action='callback',
65 callback=cmd)
66
67 def Execute(self, opt, args):
68 if not opt.command:
69 self.Usage()
70
71 cmd = [opt.command[0]]
72
73 shell = True
74 if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]):
75 shell = False
76
77 if shell:
78 cmd.append(cmd[0])
79 cmd.extend(opt.command[1:])
80
Shawn O. Pearce44469462009-03-03 17:51:01 -080081 mirror = self.manifest.IsMirror
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070082 rc = 0
83 for project in self.GetProjects(args):
84 env = dict(os.environ.iteritems())
85 env['REPO_PROJECT'] = project.name
Jeff Baileybe0e8ac2009-01-21 19:05:15 -050086 env['REPO_PATH'] = project.relpath
87 env['REPO_REMOTE'] = project.remote.name
88 env['REPO_LREV'] = project\
89 .GetRemote(project.remote.name)\
90 .ToLocal(project.revision)
91 env['REPO_RREV'] = project.revision
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070092
Shawn O. Pearce44469462009-03-03 17:51:01 -080093 if mirror:
94 env['GIT_DIR'] = project.gitdir
95 cwd = project.gitdir
96 else:
97 cwd = project.worktree
98
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070099 p = subprocess.Popen(cmd,
Shawn O. Pearce44469462009-03-03 17:51:01 -0800100 cwd = cwd,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 shell = shell,
102 env = env)
103 r = p.wait()
104 if r != 0 and r != rc:
105 rc = r
106 if rc != 0:
107 sys.exit(rc)