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 |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 17 | import sys |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 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 | |
David Pursehouse | da45e5d | 2013-05-15 17:34:45 +0900 | [diff] [blame^] | 34 | def _Options(self, p): |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 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") |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 41 | p.add_option('-n', '--name-only', |
| 42 | dest='name_only', action='store_true', |
| 43 | help="Display only the name of the repository") |
| 44 | p.add_option('-p', '--path-only', |
| 45 | dest='path_only', action='store_true', |
| 46 | help="Display only the path of the repository") |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 47 | |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 48 | def Execute(self, opt, args): |
| 49 | """List all projects and the associated directories. |
| 50 | |
| 51 | This may be possible to do with 'repo forall', but repo newbies have |
| 52 | trouble figuring that out. The idea here is that it should be more |
| 53 | discoverable. |
| 54 | |
| 55 | Args: |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 56 | opt: The options. |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 57 | args: Positional args. Can be a list of projects to list, or empty. |
| 58 | """ |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 59 | |
| 60 | if opt.fullpath and opt.name_only: |
| 61 | print('error: cannot combine -f and -n', file=sys.stderr) |
| 62 | sys.exit(1) |
| 63 | |
Olof Johansson | d75c669 | 2012-10-09 08:25:55 +0200 | [diff] [blame] | 64 | if not opt.regex: |
| 65 | projects = self.GetProjects(args) |
| 66 | else: |
| 67 | projects = self.FindProjects(args) |
| 68 | |
| 69 | def _getpath(x): |
| 70 | if opt.fullpath: |
| 71 | return x.worktree |
| 72 | return x.relpath |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 73 | |
| 74 | lines = [] |
| 75 | for project in projects: |
Chirayu Desai | 04d84a2 | 2013-03-17 18:46:23 +0530 | [diff] [blame] | 76 | if opt.name_only and not opt.path_only: |
| 77 | lines.append("%s" % ( project.name)) |
| 78 | elif opt.path_only and not opt.name_only: |
| 79 | lines.append("%s" % (_getpath(project))) |
| 80 | else: |
| 81 | lines.append("%s : %s" % (_getpath(project), project.name)) |
Doug Anderson | fce89f2 | 2011-03-14 16:07:42 -0700 | [diff] [blame] | 82 | |
| 83 | lines.sort() |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 84 | print('\n'.join(lines)) |