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 os |
| 17 | import optparse |
Conley Owens | d21720d | 2012-04-16 11:02:21 -0700 | [diff] [blame] | 18 | import platform |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 19 | import re |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
| 21 | |
| 22 | from error import NoSuchProjectError |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 23 | from error import InvalidProjectGroupsError |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | |
| 25 | class Command(object): |
| 26 | """Base class for any command line action in repo. |
| 27 | """ |
| 28 | |
| 29 | common = False |
| 30 | manifest = None |
| 31 | _optparse = None |
| 32 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 33 | def WantPager(self, opt): |
| 34 | return False |
| 35 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 36 | @property |
| 37 | def OptionParser(self): |
| 38 | if self._optparse is None: |
| 39 | try: |
| 40 | me = 'repo %s' % self.NAME |
| 41 | usage = self.helpUsage.strip().replace('%prog', me) |
| 42 | except AttributeError: |
| 43 | usage = 'repo %s' % self.NAME |
| 44 | self._optparse = optparse.OptionParser(usage = usage) |
| 45 | self._Options(self._optparse) |
| 46 | return self._optparse |
| 47 | |
| 48 | def _Options(self, p): |
| 49 | """Initialize the option parser. |
| 50 | """ |
| 51 | |
| 52 | def Usage(self): |
| 53 | """Display usage and terminate. |
| 54 | """ |
| 55 | self.OptionParser.print_usage() |
| 56 | sys.exit(1) |
| 57 | |
| 58 | def Execute(self, opt, args): |
| 59 | """Perform the action, after option parsing is complete. |
| 60 | """ |
| 61 | raise NotImplementedError |
Conley Owens | 971de8e | 2012-04-16 10:36:08 -0700 | [diff] [blame] | 62 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | def GetProjects(self, args, missing_ok=False): |
| 64 | """A list of projects that match the arguments. |
| 65 | """ |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 66 | all_projects = self.manifest.projects |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | result = [] |
| 68 | |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 69 | mp = self.manifest.manifestProject |
| 70 | |
| 71 | groups = mp.config.GetString('manifest.groups') |
Colin Cross | c39864f | 2012-04-23 13:41:58 -0700 | [diff] [blame] | 72 | if not groups: |
Conley Owens | bb1b5f5 | 2012-08-13 13:11:18 -0700 | [diff] [blame] | 73 | groups = 'all,-notdefault,platform-' + platform.system().lower() |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame^] | 74 | groups = [x for x in re.split(r'[,\s]+', groups) if x] |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 75 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | if not args: |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 77 | for project in all_projects.values(): |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 78 | if ((missing_ok or project.Exists) and |
| 79 | project.MatchesGroups(groups)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | result.append(project) |
| 81 | else: |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 82 | by_path = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | |
| 84 | for arg in args: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 85 | project = all_projects.get(arg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 86 | |
| 87 | if not project: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 88 | path = os.path.abspath(arg).replace('\\', '/') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | |
Shawn O. Pearce | cd81dd6 | 2012-10-26 12:18:00 -0700 | [diff] [blame] | 90 | if not by_path: |
| 91 | by_path = dict() |
| 92 | for p in all_projects.values(): |
| 93 | by_path[p.worktree] = p |
| 94 | |
| 95 | if os.path.exists(path): |
| 96 | oldpath = None |
| 97 | while path \ |
| 98 | and path != oldpath \ |
| 99 | and path != self.manifest.topdir: |
| 100 | try: |
| 101 | project = by_path[path] |
| 102 | break |
| 103 | except KeyError: |
| 104 | oldpath = path |
| 105 | path = os.path.dirname(path) |
| 106 | else: |
| 107 | try: |
| 108 | project = by_path[path] |
| 109 | except KeyError: |
| 110 | pass |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
| 112 | if not project: |
| 113 | raise NoSuchProjectError(arg) |
| 114 | if not missing_ok and not project.Exists: |
| 115 | raise NoSuchProjectError(arg) |
Colin Cross | 5acde75 | 2012-03-28 20:15:45 -0700 | [diff] [blame] | 116 | if not project.MatchesGroups(groups): |
| 117 | raise InvalidProjectGroupsError(arg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | |
| 119 | result.append(project) |
| 120 | |
| 121 | def _getpath(x): |
| 122 | return x.relpath |
| 123 | result.sort(key=_getpath) |
| 124 | return result |
| 125 | |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 126 | # pylint: disable=W0223 |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 127 | # Pylint warns that the `InteractiveCommand` and `PagedCommand` classes do not |
| 128 | # override method `Execute` which is abstract in `Command`. Since that method |
| 129 | # is always implemented in classes derived from `InteractiveCommand` and |
| 130 | # `PagedCommand`, this warning can be suppressed. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | class InteractiveCommand(Command): |
| 132 | """Command which requires user interaction on the tty and |
| 133 | must not run within a pager, even if the user asks to. |
| 134 | """ |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 135 | def WantPager(self, opt): |
| 136 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | |
| 138 | class PagedCommand(Command): |
| 139 | """Command which defaults to output in a pager, as its |
| 140 | display tends to be larger than one screen full. |
| 141 | """ |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 142 | def WantPager(self, opt): |
| 143 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 144 | |
David Pursehouse | 4f7bdea | 2012-10-22 12:50:15 +0900 | [diff] [blame] | 145 | # pylint: enable=W0223 |
David Pursehouse | 5c6eeac | 2012-10-11 16:44:48 +0900 | [diff] [blame] | 146 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 147 | class MirrorSafeCommand(object): |
| 148 | """Command permits itself to run within a mirror, |
| 149 | and does not require a working directory. |
| 150 | """ |