blob: 8e93787eb28936cd4b8a920c2be575c8c54e93dc [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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
16import os
17import optparse
18import sys
19
20from error import NoSuchProjectError
21
22class 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. Pearcedb45da12009-04-18 13:49:13 -070030 def WantPager(self, opt):
31 return False
32
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070033 @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 Newnamdf14a702011-01-09 17:31:57 -080077 path = os.path.abspath(arg).replace('\\', '/')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070078
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 Newnamdf14a702011-01-09 17:31:57 -080085 oldpath = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070086 while path \
Anthony Newnamdf14a702011-01-09 17:31:57 -080087 and path != oldpath \
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070088 and path != self.manifest.topdir:
89 try:
90 project = by_path[path]
91 break
92 except KeyError:
Anthony Newnamdf14a702011-01-09 17:31:57 -080093 oldpath = path
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070094 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
113class 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. Pearcedb45da12009-04-18 13:49:13 -0700117 def WantPager(self, opt):
118 return False
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700119
120class 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. Pearcedb45da12009-04-18 13:49:13 -0700124 def WantPager(self, opt):
125 return True
Shawn O. Pearcec95583b2009-03-03 17:47:06 -0800126
127class MirrorSafeCommand(object):
128 """Command permits itself to run within a mirror,
129 and does not require a working directory.
130 """