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 |
| 18 | import sys |
| 19 | |
| 20 | from error import NoSuchProjectError |
| 21 | |
| 22 | class Command(object): |
| 23 | """Base class for any command line action in repo. |
| 24 | """ |
| 25 | |
| 26 | common = False |
| 27 | manifest = None |
| 28 | _optparse = None |
| 29 | |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 30 | def WantPager(self, opt): |
| 31 | return False |
| 32 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | @property |
| 34 | def OptionParser(self): |
| 35 | if self._optparse is None: |
| 36 | try: |
| 37 | me = 'repo %s' % self.NAME |
| 38 | usage = self.helpUsage.strip().replace('%prog', me) |
| 39 | except AttributeError: |
| 40 | usage = 'repo %s' % self.NAME |
| 41 | self._optparse = optparse.OptionParser(usage = usage) |
| 42 | self._Options(self._optparse) |
| 43 | return self._optparse |
| 44 | |
| 45 | def _Options(self, p): |
| 46 | """Initialize the option parser. |
| 47 | """ |
| 48 | |
| 49 | def Usage(self): |
| 50 | """Display usage and terminate. |
| 51 | """ |
| 52 | self.OptionParser.print_usage() |
| 53 | sys.exit(1) |
| 54 | |
| 55 | def Execute(self, opt, args): |
| 56 | """Perform the action, after option parsing is complete. |
| 57 | """ |
| 58 | raise NotImplementedError |
| 59 | |
| 60 | def GetProjects(self, args, missing_ok=False): |
| 61 | """A list of projects that match the arguments. |
| 62 | """ |
| 63 | all = self.manifest.projects |
| 64 | result = [] |
| 65 | |
| 66 | if not args: |
| 67 | for project in all.values(): |
| 68 | if missing_ok or project.Exists: |
| 69 | result.append(project) |
| 70 | else: |
| 71 | by_path = None |
| 72 | |
| 73 | for arg in args: |
| 74 | project = all.get(arg) |
| 75 | |
| 76 | if not project: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 77 | path = os.path.abspath(arg).replace('\\', '/') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
| 79 | if not by_path: |
| 80 | by_path = dict() |
| 81 | for p in all.values(): |
| 82 | by_path[p.worktree] = p |
| 83 | |
| 84 | if os.path.exists(path): |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 85 | oldpath = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 86 | while path \ |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 87 | and path != oldpath \ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 88 | and path != self.manifest.topdir: |
| 89 | try: |
| 90 | project = by_path[path] |
| 91 | break |
| 92 | except KeyError: |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 93 | oldpath = path |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | path = os.path.dirname(path) |
| 95 | else: |
| 96 | try: |
| 97 | project = by_path[path] |
| 98 | except KeyError: |
| 99 | pass |
| 100 | |
| 101 | if not project: |
| 102 | raise NoSuchProjectError(arg) |
| 103 | if not missing_ok and not project.Exists: |
| 104 | raise NoSuchProjectError(arg) |
| 105 | |
| 106 | result.append(project) |
| 107 | |
| 108 | def _getpath(x): |
| 109 | return x.relpath |
| 110 | result.sort(key=_getpath) |
| 111 | return result |
| 112 | |
| 113 | class InteractiveCommand(Command): |
| 114 | """Command which requires user interaction on the tty and |
| 115 | must not run within a pager, even if the user asks to. |
| 116 | """ |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 117 | def WantPager(self, opt): |
| 118 | return False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | |
| 120 | class PagedCommand(Command): |
| 121 | """Command which defaults to output in a pager, as its |
| 122 | display tends to be larger than one screen full. |
| 123 | """ |
Shawn O. Pearce | db45da1 | 2009-04-18 13:49:13 -0700 | [diff] [blame] | 124 | def WantPager(self, opt): |
| 125 | return True |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 126 | |
| 127 | class MirrorSafeCommand(object): |
| 128 | """Command permits itself to run within a mirror, |
| 129 | and does not require a working directory. |
| 130 | """ |