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