blob: 6dff252d03a617c1a42197389b76ccf8aaa976d5 [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())
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -070085 def setenv(name, val):
86 if val is None:
87 val = ''
88 env[name] = val
89
90 setenv('REPO_PROJECT', project.name)
91 setenv('REPO_PATH', project.relpath)
92 setenv('REPO_REMOTE', project.remote.name)
93 setenv('REPO_LREV', project\
Jeff Baileybe0e8ac2009-01-21 19:05:15 -050094 .GetRemote(project.remote.name)\
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -070095 .ToLocal(project.revision))
96 setenv('REPO_RREV', project.revision)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070097
Shawn O. Pearce44469462009-03-03 17:51:01 -080098 if mirror:
Shawn O. Pearce1775dbe2009-03-17 08:03:04 -070099 setenv('GIT_DIR', project.gitdir)
Shawn O. Pearce44469462009-03-03 17:51:01 -0800100 cwd = project.gitdir
101 else:
102 cwd = project.worktree
103
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700104 p = subprocess.Popen(cmd,
Shawn O. Pearce44469462009-03-03 17:51:01 -0800105 cwd = cwd,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700106 shell = shell,
107 env = env)
108 r = p.wait()
109 if r != 0 and r != rc:
110 rc = r
111 if rc != 0:
112 sys.exit(rc)