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