Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 1 | # |
| 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 Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 16 | import re |
| 17 | |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 18 | from command import Command, MirrorSafeCommand |
| 19 | |
| 20 | class List(Command, MirrorSafeCommand): |
| 21 | common = True |
| 22 | helpSummary = "List projects and their associated directories" |
| 23 | helpUsage = """ |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 24 | %prog [-f] [<project>...] |
| 25 | %prog [-f] -r str1 [str2]..." |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 26 | """ |
| 27 | helpDescription = """ |
| 28 | List all projects; pass '.' to list the project for the cwd. |
| 29 | |
| 30 | This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'. |
| 31 | """ |
| 32 | |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 33 | 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 Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 41 | 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 Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 49 | opt: The options. |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 50 | args: Positional args. Can be a list of projects to list, or empty. |
| 51 | """ |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 52 | 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 Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 61 | |
| 62 | lines = [] |
| 63 | for project in projects: |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 64 | lines.append("%s : %s" % (_getpath(project), project.name)) |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 65 | |
| 66 | lines.sort() |
| 67 | print '\n'.join(lines) |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 68 | |
| 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 |