blob: 6058a755ebbae23224def15138c77c8520aad3b5 [file] [log] [blame]
Doug Andersonfce89f22011-03-14 16:07:42 -07001#
2# Copyright (C) 2011 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
Olof Johanssond75c6692012-10-09 08:25:55 +020016import re
17
Doug Andersonfce89f22011-03-14 16:07:42 -070018from command import Command, MirrorSafeCommand
19
20class List(Command, MirrorSafeCommand):
21 common = True
22 helpSummary = "List projects and their associated directories"
23 helpUsage = """
Olof Johanssond75c6692012-10-09 08:25:55 +020024%prog [-f] [<project>...]
25%prog [-f] -r str1 [str2]..."
Doug Andersonfce89f22011-03-14 16:07:42 -070026"""
27 helpDescription = """
28List all projects; pass '.' to list the project for the cwd.
29
30This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
31"""
32
Olof Johanssond75c6692012-10-09 08:25:55 +020033 def _Options(self, p, show_smart=True):
34 p.add_option('-r', '--regex',
35 dest='regex', action='store_true',
36 help="Filter the project list based on regex or wildcard matching of strings")
37 p.add_option('-f', '--fullpath',
38 dest='fullpath', action='store_true',
39 help="Display the full work tree path instead of the relative path")
40
Doug Andersonfce89f22011-03-14 16:07:42 -070041 def Execute(self, opt, args):
42 """List all projects and the associated directories.
43
44 This may be possible to do with 'repo forall', but repo newbies have
45 trouble figuring that out. The idea here is that it should be more
46 discoverable.
47
48 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020049 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070050 args: Positional args. Can be a list of projects to list, or empty.
51 """
Olof Johanssond75c6692012-10-09 08:25:55 +020052 if not opt.regex:
53 projects = self.GetProjects(args)
54 else:
55 projects = self.FindProjects(args)
56
57 def _getpath(x):
58 if opt.fullpath:
59 return x.worktree
60 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070061
62 lines = []
63 for project in projects:
Olof Johanssond75c6692012-10-09 08:25:55 +020064 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070065
66 lines.sort()
67 print '\n'.join(lines)
Olof Johanssond75c6692012-10-09 08:25:55 +020068
69 def FindProjects(self, args):
70 result = []
71 for project in self.GetProjects(''):
72 for arg in args:
73 pattern = re.compile(r'%s' % arg, re.IGNORECASE)
74 if pattern.search(project.name) or pattern.search(project.relpath):
75 result.append(project)
76 break
77 result.sort(key=lambda project: project.relpath)
78 return result