| 35 | | Raises ImportError if the management module cannot be found for any reason. |
| 36 | | """ |
| 37 | | parts = app_name.split('.') |
| 38 | | parts.append('management') |
| 39 | | parts.reverse() |
| 40 | | path = None |
| 41 | | while parts: |
| 42 | | part = parts.pop() |
| 43 | | f, path, descr = find_module(part, path and [path] or None) |
| 44 | | return path |
| | 23 | def find_commands(app_name): |
| | 24 | """ |
| | 25 | Given a path to a management directory, returns a list of all the command |
| | 26 | names that are available. |
| | 28 | Returns an empty list if no commands are defined. |
| | 29 | """ |
| | 30 | management_dir = find_management_module(app_name) |
| | 31 | command_dir = os.path.join(management_dir, 'commands') |
| | 32 | try: |
| | 33 | return [f[:-3] for f in os.listdir(command_dir) |
| | 34 | if not f.startswith('_') and f.endswith('.py')] |
| | 35 | except OSError: |
| | 36 | return [] |
| | 37 | |
| | 38 | def find_management_module(app_name): |
| | 39 | """ |
| | 40 | Determines the path to the management module for the application named, |
| | 41 | without acutally importing the application or the management module. |
| | 42 | |
| | 43 | Raises ImportError if the management module cannot be found for any reason. |
| | 44 | """ |
| | 45 | parts = app_name.split('.') |
| | 46 | parts.append('management') |
| | 47 | parts.reverse() |
| | 48 | path = None |
| | 49 | while parts: |
| | 50 | part = parts.pop() |
| | 51 | f, path, descr = find_module(part, path and [path] or None) |
| | 52 | return path |
| | 53 | |
| | 54 | else: |
| | 55 | |
| | 56 | # Python 2.5 |
| | 57 | # The iter_modules function has the advantage to be more cleanser generic: also finds |
| | 58 | # packages in zip files, recognizes other file extensions than .py |
| | 59 | |
| | 60 | def find_commands(app_name): |
| | 61 | """ |
| | 62 | Given an application name, returns a list of all the commands found. |
| | 63 | |
| | 64 | Raises ImportError if no commands are defined. |
| | 65 | """ |
| | 66 | packages = {} |
| | 67 | mgmt_package = "%s.management.commands" % app_name |
| | 68 | # The next line imports the *package*, not all modules in the package |
| | 69 | __import__(mgmt_package) |
| | 70 | path = getattr(sys.modules[mgmt_package], '__path__', None) |
| | 71 | return [i[1] for i in iter_modules(path)] |
| | 72 | |
| | 73 | |