blob: 0d5c27f782ea9789b77572affbded7711a19a23d [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
Sarah Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
Olof Johanssond75c6692012-10-09 08:25:55 +020017import re
18
Doug Andersonfce89f22011-03-14 16:07:42 -070019from command import Command, MirrorSafeCommand
20
21class List(Command, MirrorSafeCommand):
22 common = True
23 helpSummary = "List projects and their associated directories"
24 helpUsage = """
Olof Johanssond75c6692012-10-09 08:25:55 +020025%prog [-f] [<project>...]
26%prog [-f] -r str1 [str2]..."
Doug Andersonfce89f22011-03-14 16:07:42 -070027"""
28 helpDescription = """
29List all projects; pass '.' to list the project for the cwd.
30
31This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
32"""
33
Olof Johanssond75c6692012-10-09 08:25:55 +020034 def _Options(self, p, show_smart=True):
35 p.add_option('-r', '--regex',
36 dest='regex', action='store_true',
37 help="Filter the project list based on regex or wildcard matching of strings")
38 p.add_option('-f', '--fullpath',
39 dest='fullpath', action='store_true',
40 help="Display the full work tree path instead of the relative path")
41
Doug Andersonfce89f22011-03-14 16:07:42 -070042 def Execute(self, opt, args):
43 """List all projects and the associated directories.
44
45 This may be possible to do with 'repo forall', but repo newbies have
46 trouble figuring that out. The idea here is that it should be more
47 discoverable.
48
49 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020050 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070051 args: Positional args. Can be a list of projects to list, or empty.
52 """
Olof Johanssond75c6692012-10-09 08:25:55 +020053 if not opt.regex:
54 projects = self.GetProjects(args)
55 else:
56 projects = self.FindProjects(args)
57
58 def _getpath(x):
59 if opt.fullpath:
60 return x.worktree
61 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070062
63 lines = []
64 for project in projects:
Olof Johanssond75c6692012-10-09 08:25:55 +020065 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070066
67 lines.sort()
Sarah Owenscecd1d82012-11-01 22:59:27 -070068 print('\n'.join(lines))
Olof Johanssond75c6692012-10-09 08:25:55 +020069
70 def FindProjects(self, args):
71 result = []
72 for project in self.GetProjects(''):
73 for arg in args:
74 pattern = re.compile(r'%s' % arg, re.IGNORECASE)
75 if pattern.search(project.name) or pattern.search(project.relpath):
76 result.append(project)
77 break
78 result.sort(key=lambda project: project.relpath)
79 return result