__init__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2008 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import os
  17. # A mapping of the subcommand name to the class that implements it.
  18. all_commands = {}
  19. my_dir = os.path.dirname(__file__)
  20. for py in os.listdir(my_dir):
  21. if py == '__init__.py':
  22. continue
  23. if py.endswith('.py'):
  24. name = py[:-3]
  25. clsn = name.capitalize()
  26. while clsn.find('_') > 0:
  27. h = clsn.index('_')
  28. clsn = clsn[0:h] + clsn[h + 1:].capitalize()
  29. mod = __import__(__name__,
  30. globals(),
  31. locals(),
  32. ['%s' % name])
  33. mod = getattr(mod, name)
  34. try:
  35. cmd = getattr(mod, clsn)
  36. except AttributeError:
  37. raise SyntaxError('%s/%s does not define class %s' % (
  38. __name__, py, clsn))
  39. name = name.replace('_', '-')
  40. cmd.NAME = name
  41. all_commands[name] = cmd
  42. # Add 'branch' as an alias for 'branches'.
  43. all_commands['branch'] = all_commands['branches']