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