progrium / ginkgo

Python service microframework
http://ginkgo.readthedocs.org
MIT License
325 stars 51 forks source link

ginkgo/runner.py: print traceback when import/key error occurs #54

Closed hblanks closed 12 years ago

hblanks commented 12 years ago

This patch updates the ginkgo / ginkgoctl CLI's so that instead of:

[root@23.20.50.77][dev]# ginkgo ./deployment/collector.conf.py
Starting process with ./deployment/collector.conf.py...
usage: ginkgo [-v] [-h] [target]
ginkgo: error: Unable to load class path: blah.blah.foo:
No module named gar

You get:

[root@23.20.50.77][dev]# ginkgo ./deployment/collector.conf.py
Starting process with ./deployment/collector.conf.py...
usage: ginkgo [-v] [-h] [target]
ginkgo: error: Unable to load class path: blah.blah.foo:
No module named gar

Traceback (most recent call last):
  File "/home/hunter/ginkgo/ginkgo/runner.py", line 98, in load_class
    module = runpy.run_module(module_name + ".__init__")
  File "/usr/local/python-2.7/lib/python2.7/runpy.py", line 170, in run_module
    mod_name, loader, code, fname = _get_module_details(mod_name)
  File "/usr/local/python-2.7/lib/python2.7/runpy.py", line 101, in _get_module_details
    loader = get_loader(mod_name)
  File "/usr/local/python-2.7/lib/python2.7/pkgutil.py", line 456, in get_loader
    return find_loader(fullname)
  File "/usr/local/python-2.7/lib/python2.7/pkgutil.py", line 466, in find_loader
    for importer in iter_importers(fullname):
  File "/usr/local/python-2.7/lib/python2.7/pkgutil.py", line 422, in iter_importers
    __import__(pkg)
  ... <- your code here!
ImportError: No module named gar

In a sense, it'd be kind of nice if we didn't use argparse.error() for cases like this, when the issue is not an invalid argument on the command line, but rather a bug in the user's underlying ginkgo service.

hblanks commented 12 years ago

My commit has been updated so that it only prints tracebacks when the ImportError has occurred within the user's codebase. (i.e. a nested ImportError)

progrium commented 12 years ago

Good catch! Thanks Hunter