list.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import sys
  18. from command import Command, MirrorSafeCommand
  19. class List(Command, MirrorSafeCommand):
  20. common = True
  21. helpSummary = "List projects and their associated directories"
  22. helpUsage = """
  23. %prog [-f] [<project>...]
  24. %prog [-f] -r str1 [str2]..."
  25. """
  26. helpDescription = """
  27. List all projects; pass '.' to list the project for the cwd.
  28. This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
  29. """
  30. def _Options(self, p, show_smart=True):
  31. p.add_option('-r', '--regex',
  32. dest='regex', action='store_true',
  33. help="Filter the project list based on regex or wildcard matching of strings")
  34. p.add_option('-f', '--fullpath',
  35. dest='fullpath', action='store_true',
  36. help="Display the full work tree path instead of the relative path")
  37. p.add_option('-n', '--name-only',
  38. dest='name_only', action='store_true',
  39. help="Display only the name of the repository")
  40. p.add_option('-p', '--path-only',
  41. dest='path_only', action='store_true',
  42. help="Display only the path of the repository")
  43. def Execute(self, opt, args):
  44. """List all projects and the associated directories.
  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. Args:
  49. opt: The options.
  50. args: Positional args. Can be a list of projects to list, or empty.
  51. """
  52. if opt.fullpath and opt.name_only:
  53. print('error: cannot combine -f and -n', file=sys.stderr)
  54. sys.exit(1)
  55. if not opt.regex:
  56. projects = self.GetProjects(args)
  57. else:
  58. projects = self.FindProjects(args)
  59. def _getpath(x):
  60. if opt.fullpath:
  61. return x.worktree
  62. return x.relpath
  63. lines = []
  64. for project in projects:
  65. if opt.name_only and not opt.path_only:
  66. lines.append("%s" % ( project.name))
  67. elif opt.path_only and not opt.name_only:
  68. lines.append("%s" % (_getpath(project)))
  69. else:
  70. lines.append("%s : %s" % (_getpath(project), project.name))
  71. lines.sort()
  72. print('\n'.join(lines))
  73. def FindProjects(self, args):
  74. result = []
  75. for project in self.GetProjects(''):
  76. for arg in args:
  77. pattern = re.compile(r'%s' % arg, re.IGNORECASE)
  78. if pattern.search(project.name) or pattern.search(project.relpath):
  79. result.append(project)
  80. break
  81. result.sort(key=lambda project: project.relpath)
  82. return result