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