support for custom django-admin commands written as packages

40 views
Skip to first unread message

Marwan Al-Sabbagh

unread,
Jun 5, 2011, 11:15:12 AM6/5/11
to django-d...@googlegroups.com
Hello,
  Currently one can create a custom command by implementing a Command
object in a python module such as
"polls/management/commands/closepoll.py". It would be great if Django
also supported these commands being implemented in a python package so
in this case Command would be found in
"polls/management/commands/closepoll/__init__.py". This would be
useful when a command contains a lot of logic or code and the
developer would like the ability to split up the logic into multiple
files to make the code base more manageable. I faced this issue
recently when I implemented a command that would be run as a cron job
to periodically import data into Django from a legacy system that had
some pretty hairy logic to deal with.
I've already made the changes to support the feature, it only
required a change to the function
django.core.management.find_commands() to look for packages as well as
modules in each commands directory. I thought I'd run it by
django-developers before creating the ticket on trac with the patch.
What do you guys think?

cheers,
Marwan AlSabbagh

Carl Meyer

unread,
Jun 6, 2011, 2:31:29 PM6/6/11
to django-d...@googlegroups.com
Hi Marwan,

I'm not a big fan of the way Django finds management commands. Searching
directly on the filesystem makes Django apps with management commands
incompatible with legitimate builtin Python features such as import
hooks or zipfile imports. IMHO, regardless of what you think of those
Python features, this makes Django somewhat less than "just Python."

That said, I don't have a great alternative in mind at the moment, but I
don't like the idea of taking the current bad system and extending it
with even more complexity.

Generally my approach to the problem you describe is to make the
management command itself a minimal wrapper around the core code,
implemented as functions and classes within appropriately-named
submodules of the app itself, not within the
myapp.management.commands.mycommand namespace. I almost always at some
point want to reuse that code as library code, so I actually find it
preferable if the bulk of the code lives outside the management.commands
namespace, and is just imported and used by the management command. This
can also encourage a separation of responsibilities that makes testing
easier.

Carl

Marwan Al-Sabbagh

unread,
Jun 7, 2011, 3:28:33 AM6/7/11
to django-d...@googlegroups.com
I agree with many of your points. It would be nice if Django did not deal with the file system directly but just imports the packages. There have been some efforts to do custom commands in a better way. there is the project django-boss https://github.com/zacharyvoase/django-boss which stemmed out of this blog post http://blog.zacharyvoase.com/2009/12/09/django-boss/. Zachary Voase runs through a number of things that could be improved in the way custom commands work.
I though did want to use the django-admin custom commands and so an opportunity to make it more flexible in who you implement the commands. When I had a look at the code I saw this was possible by making a small change to one function, so I felt it didn't add much complexity to the existing system. I've a ticket with the patch at https://code.djangoproject.com/ticket/16167. Thanks for the feedback, and I would be open to helping improve the whole way custom commands as a whole work in Django too.

Marwan


--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.


Reply all
Reply to author
Forward
0 new messages