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 |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 18 | import sys |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 19 | |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 20 | from command import Command, MirrorSafeCommand |
| 21 | |
| 22 | class List(Command, MirrorSafeCommand): |
| 23 | common = True |
| 24 | helpSummary = "List projects and their associated directories" |
| 25 | helpUsage = """ |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 26 | %prog [-f] [<project>...] |
| 27 | %prog [-f] -r str1 [str2]..." |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 28 | """ |
| 29 | helpDescription = """ |
| 30 | List all projects; pass '.' to list the project for the cwd. |
| 31 | |
| 32 | This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'. |
| 33 | """ |
| 34 | |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 35 | 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 Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 42 | 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 Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 48 | |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 49 | 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 Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 57 | opt: The options. |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 58 | args: Positional args. Can be a list of projects to list, or empty. |
| 59 | """ |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 60 | |
| 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 Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 65 | 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 Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 74 | |
| 75 | lines = [] |
| 76 | for project in projects: |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 77 | 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 Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 83 | |
| 84 | lines.sort() |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 85 | print('\n'.join(lines)) |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 86 | |
| 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 |