Bug 3316 - Importing Django model in an HG hook causes catastrophe
Summary: Importing Django model in an HG hook causes catastrophe
Status: RESOLVED WONTFIX
Alias: None
Product: Mercurial
Classification: Unclassified
Component: Mercurial (show other bugs)
Version: unspecified
Hardware: All All
: normal bug
Assignee: Bugzilla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-08 18:03 UTC by Nick Jennings
Modified: 2012-05-13 04:52 UTC (History)
3 users (show)

See Also:
Python Version: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nick Jennings 2012-03-08 18:03 UTC
See my original post here: http://stackoverflow.com/questions/9620997/using-
a-django-model-from-a-mercurial-python-hook

Basically, when I try to import a Django model in my mercurial hook, I get a 
ton of errors, heres the top of my mercurial hook file:

#!/usr/bin/env python
import sys
path = '/opt'
if path not in sys.path:
    sys.path.append(path)

from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)

from mysite.myapp.models import Build
import datetime



Now, when it executes, I get the following error:



** unknown exception encountered, please report by visiting
**  http://mercurial.selenic.com/wiki/BugTracker
** Python 2.7.2+ (default, Oct  4 2011, 20:06:09) [GCC 4.6.1]
** Mercurial Distributed SCM (version 1.9.1)
** Extensions loaded:
Traceback (most recent call last):
  File "/usr/bin/hg", line 38, in <module>
    mercurial.dispatch.run()
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 27, in 
run
    sys.exit(dispatch(request(sys.argv[1:])))
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 64, in 
dispatch
    return _runcatch(req)
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 87, in 
_runcatch
    return _dispatch(req)
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 679, 
in _dispatch
    cmdpats, cmdoptions)
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 454, 
in runcommand
    ret = _runcommand(ui, options, cmd, d)
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 733, 
in _runcommand
    return checkargs()
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 687, 
in checkargs
return cmdfunc()
  File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 676, 
in <lambda>
    d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
  File "/usr/lib/python2.7/dist-packages/mercurial/util.py", line 385, in 
check
return func(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/mercurial/commands.py", line 3884, 
in push
    newbranch=opts.get('new_branch'))
  File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 1428, 
in push
lock=lock)
  File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 1849, 
in addchangegroup
source=srctype, url=url)
  File "/usr/lib/python2.7/dist-packages/mercurial/localrepo.py", line 224, 
in hook
    return hook.hook(self.ui, self, name, throw, **args)
  File "/usr/lib/python2.7/dist-packages/mercurial/hook.py", line 160, in 
hook
    mod = extensions.loadpath(path, 'hghook.%s' % hname)
  File "/usr/lib/python2.7/dist-packages/mercurial/extensions.py", line 45, 
in loadpath
    return imp.load_source(module_name, path)
  File "/opt/unapse/belvedere/mercurial_hook.py", line 14, in <module>
    from unapse.belvedere.models import Build
  File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 
109, in _demandimport
    mod = _origimport(name, globals, locals)
  File "/opt/unapse/belvedere/models.py", line 1, in <module>
    from django.db import models
  File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 
109, in _demandimport
    mod = _origimport(name, globals, locals)
  File "/usr/lib/pymodules/python2.7/django/db/__init__.py", line 14, in 
<module>
    if not settings.DATABASES:
  File "/usr/lib/pymodules/python2.7/django/utils/functional.py", line 276, 
in __getattr__
    self._setup()
  File "/usr/lib/pymodules/python2.7/django/conf/__init__.py", line 42, in 
_setup
    self._wrapped = Settings(settings_module)
  File "/usr/lib/pymodules/python2.7/django/conf/__init__.py", line 139, in 
__init__
    logging_config_func(self.LOGGING)
  File "/usr/lib/python2.7/logging/config.py", line 776, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 575, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'mail_admins': __import__() argument 
1 must be string, not DictConfigurator
Comment 1 Matt Mackall 2012-03-08 18:25 UTC
Hooks written in Python are unsupported-totally-on-your-own-territory, so
this bug is dead on arrival. Closing.

However, this is probably an interaction with Mercurial's demandloader. You
can probably want to do something like:

from mercurial import demandload
demandload.ignore.append("mydemandloadincompatiblemodule")

However, your life will be a lot simpler if you just launch your Python
script as a separate process. The extra milliseconds of execution time will
likely never add up to time already spent trying to do it the hard way.
Comment 2 Nick Jennings 2012-03-08 19:19 UTC
Hi Matt, thank you very much for the informative reply. I was actually trying 
to use the python/mercurial integration as a plus. I'd like to ask, why do you 
guys even promote is as an options instead of just pushing (and, suggesting 
for people like me) command line parameters and HG comment integration as the 
more preferred method of integration with mercurial?

I actually thought it would be easier to integrate with the objects passed to 
me, than to have to make some command-line wrapper to call HG, or - after the 
hassle - just going with a BASH script, which then calls a python script.
Comment 3 Matt Mackall 2012-03-08 19:32 UTC
If you can tell us where we're promoting it, please tell us, so we can stamp
it out.

See here:

http://mercurial.selenic.com/wiki/MercurialApi
http://mercurial.selenic.com/wiki/PythonHglib
Comment 4 Nick Jennings 2012-03-08 20:39 UTC
Here is where I caught the scent: http://hgbook.red-bean.com/read/handling-
repository-events-with-hooks.html

Maybe "promoting" it is strong, but i definitely got the feeling that i was a 
more integrated way of handling things. Nonetheless! It's fair-enough to say, 
it's an API that under development and we make no guarantees... but - you're 
basically saying, that this wont be fixed. I though my issue was actually, 
pretty mundane. Who wouldn't want to run mercurial hooks for their django 
project, afterall :) ... What about some documentation centering around python 
hooks which use a "certified" API such as command line args? could we work on 
some documentation to that regard?
Comment 5 Bugzilla 2012-05-12 09:29 UTC

--- Bug imported by bugzilla@serpentine.com 2012-05-12 09:29 EDT  ---

This bug was previously known as _bug_ 3315 at http://mercurial.selenic.com/bts/issue3315