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 | ff84fea | 2009-04-13 12:11:59 -0700 | [diff] [blame] | 33 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 34 | pwd is the project's working directory. If the current client is |
| 35 | a mirror client, then pwd is the Git repository. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | |
| 37 | REPO_PROJECT is set to the unique name of the project. |
| 38 | |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 39 | REPO_PATH is the path relative the the root of the client. |
| 40 | |
| 41 | REPO_REMOTE is the name of the remote system from the manifest. |
| 42 | |
| 43 | REPO_LREV is the name of the revision from the manifest, translated |
| 44 | to a local tracking branch. If you need to pass the manifest |
| 45 | revision to a locally executed git command, use REPO_LREV. |
| 46 | |
| 47 | REPO_RREV is the name of the revision from the manifest, exactly |
| 48 | as written in the manifest. |
| 49 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | shell positional arguments ($1, $2, .., $#) are set to any arguments |
| 51 | following <command>. |
| 52 | |
| 53 | stdin, stdout, stderr are inherited from the terminal and are |
| 54 | not redirected. |
| 55 | """ |
| 56 | |
| 57 | def _Options(self, p): |
| 58 | def cmd(option, opt_str, value, parser): |
| 59 | setattr(parser.values, option.dest, list(parser.rargs)) |
| 60 | while parser.rargs: |
| 61 | del parser.rargs[0] |
| 62 | p.add_option('-c', '--command', |
| 63 | help='Command (and arguments) to execute', |
| 64 | dest='command', |
| 65 | action='callback', |
| 66 | callback=cmd) |
| 67 | |
| 68 | def Execute(self, opt, args): |
| 69 | if not opt.command: |
| 70 | self.Usage() |
| 71 | |
| 72 | cmd = [opt.command[0]] |
| 73 | |
| 74 | shell = True |
| 75 | if re.compile(r'^[a-z0-9A-Z_/\.-]+$').match(cmd[0]): |
| 76 | shell = False |
| 77 | |
| 78 | if shell: |
| 79 | cmd.append(cmd[0]) |
| 80 | cmd.extend(opt.command[1:]) |
| 81 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 82 | mirror = self.manifest.IsMirror |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | rc = 0 |
| 84 | for project in self.GetProjects(args): |
| 85 | env = dict(os.environ.iteritems()) |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 86 | def setenv(name, val): |
| 87 | if val is None: |
| 88 | val = '' |
| 89 | env[name] = val |
| 90 | |
| 91 | setenv('REPO_PROJECT', project.name) |
| 92 | setenv('REPO_PATH', project.relpath) |
| 93 | setenv('REPO_REMOTE', project.remote.name) |
| 94 | setenv('REPO_LREV', project\ |
Jeff Bailey | be0e8ac | 2009-01-21 19:05:15 -0500 | [diff] [blame] | 95 | .GetRemote(project.remote.name)\ |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 96 | .ToLocal(project.revision)) |
| 97 | setenv('REPO_RREV', project.revision) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 99 | if mirror: |
Shawn O. Pearce | 1775dbe | 2009-03-17 08:03:04 -0700 | [diff] [blame] | 100 | setenv('GIT_DIR', project.gitdir) |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 101 | cwd = project.gitdir |
| 102 | else: |
| 103 | cwd = project.worktree |
| 104 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | p = subprocess.Popen(cmd, |
Shawn O. Pearce | 4446946 | 2009-03-03 17:51:01 -0800 | [diff] [blame] | 106 | cwd = cwd, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | shell = shell, |
| 108 | env = env) |
| 109 | r = p.wait() |
| 110 | if r != 0 and r != rc: |
| 111 | rc = r |
| 112 | if rc != 0: |
| 113 | sys.exit(rc) |