The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 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 | |
| 16 | import re |
| 17 | import os |
| 18 | import sys |
| 19 | import subprocess |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 20 | from command import Command, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 22 | class Forall(Command, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | common = False |
| 24 | helpSummary = "Run a shell command in each project" |
| 25 | helpUsage = """ |
| 26 | %prog [<project>...] -c <command> [<arg>...] |
| 27 | """ |
| 28 | helpDescription = """ |
| 29 | Executes the same shell command in each project. |
| 30 | |
| 31 | Environment |
| 32 | ----------- |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 33 | pwd is the project's working directory. If the current client is |
| 34 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
| 36 | REPO_PROJECT is set to the unique name of the project. |
| 37 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 38 | REPO_PATH is the path relative the the root of the client. |
| 39 | |
| 40 | REPO_REMOTE is the name of the remote system from the manifest. |
| 41 | |
| 42 | REPO_LREV is the name of the revision from the manifest, translated |
| 43 | to a local tracking branch. If you need to pass the manifest |
| 44 | revision to a locally executed git command, use REPO_LREV. |
| 45 | |
| 46 | REPO_RREV is the name of the revision from the manifest, exactly |
| 47 | as written in the manifest. |
| 48 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 50 | following <command>. |
| 51 | |
| 52 | stdin, stdout, stderr are inherited from the terminal and are |
| 53 | not 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. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 81 | mirror = self.manifest.IsMirror |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | rc = 0 |
| 83 | for project in self.GetProjects(args): |
| 84 | env = dict(os.environ.iteritems()) |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 85 | 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 Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 94 | .GetRemote(project.remote.name)\ |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 95 | .ToLocal(project.revision)) |
| 96 | setenv('REPO_RREV', project.revision) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 98 | if mirror: |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 99 | setenv('GIT_DIR', project.gitdir) |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 100 | cwd = project.gitdir |
| 101 | else: |
| 102 | cwd = project.worktree |
| 103 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 104 | p = subprocess.Popen(cmd, |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 105 | cwd = cwd, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | 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) |