list.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import re
  16. from command import Command, MirrorSafeCommand
  17. class List(Command, MirrorSafeCommand):
  18. common = True
  19. helpSummary = "List projects and their associated directories"
  20. helpUsage = """
  21. %prog [-f] [<project>...]
  22. %prog [-f] -r str1 [str2]..."
  23. """
  24. helpDescription = """
  25. List all projects; pass '.' to list the project for the cwd.
  26. This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
  27. """
  28. def _Options(self, p, show_smart=True):
  29. p.add_option('-r', '--regex',
  30. dest='regex', action='store_true',
  31. help="Filter the project list based on regex or wildcard matching of strings")
  32. p.add_option('-f', '--fullpath',
  33. dest='fullpath', action='store_true',
  34. help="Display the full work tree path instead of the relative path")
  35. def Execute(self, opt, args):
  36. """List all projects and the associated directories.
  37. This may be possible to do with 'repo forall', but repo newbies have
  38. trouble figuring that out. The idea here is that it should be more
  39. discoverable.
  40. Args:
  41. opt: The options.
  42. args: Positional args. Can be a list of projects to list, or empty.
  43. """
  44. if not opt.regex:
  45. projects = self.GetProjects(args)
  46. else:
  47. projects = self.FindProjects(args)
  48. def _getpath(x):
  49. if opt.fullpath:
  50. return x.worktree
  51. return x.relpath
  52. lines = []
  53. for project in projects:
  54. lines.append("%s : %s" % (_getpath(project), project.name))
  55. lines.sort()
  56. print '\n'.join(lines)
  57. def FindProjects(self, args):
  58. result = []
  59. for project in self.GetProjects(''):
  60. for arg in args:
  61. pattern = re.compile(r'%s' % arg, re.IGNORECASE)
  62. if pattern.search(project.name) or pattern.search(project.relpath):
  63. result.append(project)
  64. break
  65. result.sort(key=lambda project: project.relpath)
  66. return result