list.py 2.5 KB

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