blob: 8eb06cdd5125d1ed6686f0213ca9bfe3554b0ddd [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
Chirayu Desai04d84a22013-03-17 18:46:23 +053018import sys
Olof Johanssond75c6692012-10-09 08:25:55 +020019
Doug Andersonfce89f22011-03-14 16:07:42 -070020from command import Command, MirrorSafeCommand
21
22class List(Command, MirrorSafeCommand):
23 common = True
24 helpSummary = "List projects and their associated directories"
25 helpUsage = """
Olof Johanssond75c6692012-10-09 08:25:55 +020026%prog [-f] [<project>...]
27%prog [-f] -r str1 [str2]..."
Doug Andersonfce89f22011-03-14 16:07:42 -070028"""
29 helpDescription = """
30List all projects; pass '.' to list the project for the cwd.
31
32This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
33"""
34
Olof Johanssond75c6692012-10-09 08:25:55 +020035 def _Options(self, p, show_smart=True):
36 p.add_option('-r', '--regex',
37 dest='regex', action='store_true',
38 help="Filter the project list based on regex or wildcard matching of strings")
39 p.add_option('-f', '--fullpath',
40 dest='fullpath', action='store_true',
41 help="Display the full work tree path instead of the relative path")
Chirayu Desai04d84a22013-03-17 18:46:23 +053042 p.add_option('-n', '--name-only',
43 dest='name_only', action='store_true',
44 help="Display only the name of the repository")
45 p.add_option('-p', '--path-only',
46 dest='path_only', action='store_true',
47 help="Display only the path of the repository")
Olof Johanssond75c6692012-10-09 08:25:55 +020048
Doug Andersonfce89f22011-03-14 16:07:42 -070049 def Execute(self, opt, args):
50 """List all projects and the associated directories.
51
52 This may be possible to do with 'repo forall', but repo newbies have
53 trouble figuring that out. The idea here is that it should be more
54 discoverable.
55
56 Args:
Olof Johanssond75c6692012-10-09 08:25:55 +020057 opt: The options.
Doug Andersonfce89f22011-03-14 16:07:42 -070058 args: Positional args. Can be a list of projects to list, or empty.
59 """
Chirayu Desai04d84a22013-03-17 18:46:23 +053060
61 if opt.fullpath and opt.name_only:
62 print('error: cannot combine -f and -n', file=sys.stderr)
63 sys.exit(1)
64
Olof Johanssond75c6692012-10-09 08:25:55 +020065 if not opt.regex:
66 projects = self.GetProjects(args)
67 else:
68 projects = self.FindProjects(args)
69
70 def _getpath(x):
71 if opt.fullpath:
72 return x.worktree
73 return x.relpath
Doug Andersonfce89f22011-03-14 16:07:42 -070074
75 lines = []
76 for project in projects:
Chirayu Desai04d84a22013-03-17 18:46:23 +053077 if opt.name_only and not opt.path_only:
78 lines.append("%s" % ( project.name))
79 elif opt.path_only and not opt.name_only:
80 lines.append("%s" % (_getpath(project)))
81 else:
82 lines.append("%s : %s" % (_getpath(project), project.name))
Doug Andersonfce89f22011-03-14 16:07:42 -070083
84 lines.sort()
Sarah Owenscecd1d82012-11-01 22:59:27 -070085 print('\n'.join(lines))
Olof Johanssond75c6692012-10-09 08:25:55 +020086
87 def FindProjects(self, args):
88 result = []
89 for project in self.GetProjects(''):
90 for arg in args:
91 pattern = re.compile(r'%s' % arg, re.IGNORECASE)
92 if pattern.search(project.name) or pattern.search(project.relpath):
93 result.append(project)
94 break
95 result.sort(key=lambda project: project.relpath)
96 return result