# Path: django_extensions/management/jobs.py
# def get_jobs(when=None, only_scheduled=False):
#     """
#     Returns a dictionary mapping of job names together with there respective
#     application class.
#     """
#     global _jobs
#     # FIXME: HACK: make sure the project dir is on the path when executed as ./manage.py
#     import sys
#     try:
#         cpath = os.path.dirname(os.path.realpath(sys.argv[0]))
#         ppath = os.path.dirname(cpath)
#         if ppath not in sys.path:
#             sys.path.append(ppath)
#     except:
#         pass
#     if _jobs is None:
#         _jobs = {}
#         if True:
#             from django.conf import settings
#             for app_name in settings.INSTALLED_APPS:
#                 scandirs = (None, 'hourly', 'daily', 'weekly', 'monthly', 'yearly')
#                 if when:
#                     scandirs = None, when
#                 for subdir in scandirs:
#                     try:
#                         path = find_job_module(app_name, subdir)
#                         for name in find_jobs(path):
#                             if (app_name, name) in _jobs:
#                                 raise JobError("Duplicate job %s" % name)
#                             job = import_job(app_name, name, subdir)
#                             if only_scheduled and job.when == None:
#                                 # only include jobs which are scheduled
#                                 continue
#                             if when and job.when != when:
#                                 # generic job not in same schedule
#                                 continue
#                             _jobs[(app_name, name)] = job
#                     except ImportError:
#                         pass # No job module -- continue scanning
#     return _jobs
# 
# def print_jobs(when=None, only_scheduled=False, show_when=True, \
#                 show_appname=False, show_header=True):
#     jobmap = get_jobs(when, only_scheduled=only_scheduled)
#     print "Job List: %i jobs" % len(jobmap)
#     jlist = jobmap.keys()
#     jlist.sort()
#     appname_spacer = "%%-%is" % max(len(e[0]) for e in jlist)
#     name_spacer = "%%-%is" % max(len(e[1]) for e in jlist)
#     when_spacer = "%%-%is" % max(len(e.when) for e in jobmap.values() if e.when)
#     if show_header:
#         line = " "
#         if show_appname:
#             line += appname_spacer % "appname" + " - "
#         line += name_spacer % "jobname"
#         if show_when:
#             line += " - " + when_spacer % "when"
#         line += " - help"
#         print line
#         print "-"*80
# 
#     for app_name, job_name in jlist:
#         job = jobmap[(app_name, job_name)]
#         line = " "
#         if show_appname:
#             line += appname_spacer % app_name + " - "
#         line += name_spacer % job_name
#         if show_when:
#             line += " - " + when_spacer % (job.when and job.when or "")
#         line += " - " + job.help
#         print line


from django.core.management.base import LabelCommand
from optparse import make_option
from django_extensions.management.jobs import get_jobs, print_jobs
        from django_extensions.management import signals
        from django.db import models
        from django.conf import settings
                import traceback
    option_list = LabelCommand.option_list + (
        make_option('--list', '-l', action="store_true", dest="list_jobs",
            help="List all jobs with their description"),
    )
    help = "Runs scheduled maintenance jobs."
    args = "[hourly daily weekly monthly yearly]"
    label = ""

    requires_model_validation = True

    def usage_msg(self):
        print "Run scheduled jobs. Please specify 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'"

    def runjobs(self, when, options):
        verbosity = int(options.get('verbosity', 1))
        jobs = get_jobs(when, only_scheduled=True)
        list = jobs.keys()
        list.sort()
        for app_name, job_name in list:
            job = jobs[(app_name, job_name)]
            if verbosity>1:
                print "Executing %s job: %s (app: %s)" % (when, job_name, app_name)
            try:
                job().execute()
            except Exception, e:
                print "ERROR OCCURED IN %s JOB: %s (APP: %s)" % (when.upper(), job_name, app_name)
                print "START TRACEBACK:"
                traceback.print_exc()
                print "END TRACEBACK\n"


# Path: django_extensions/management/jobs.py
# def get_jobs(when=None, only_scheduled=False):
#     """
#     Returns a dictionary mapping of job names together with there respective
#     application class.
#     """
#     global _jobs
#     # FIXME: HACK: make sure the project dir is on the path when executed as ./manage.py
#     import sys
#     try:
#         cpath = os.path.dirname(os.path.realpath(sys.argv[0]))
#         ppath = os.path.dirname(cpath)
#         if ppath not in sys.path:
#             sys.path.append(ppath)
#     except:
#         pass
#     if _jobs is None:
#         _jobs = {}
#         if True:
#             from django.conf import settings
#             for app_name in settings.INSTALLED_APPS:
#                 scandirs = (None, 'hourly', 'daily', 'weekly', 'monthly', 'yearly')
#                 if when:
#                     scandirs = None, when
#                 for subdir in scandirs:
#                     try:
#                         path = find_job_module(app_name, subdir)
#                         for name in find_jobs(path):
#                             if (app_name, name) in _jobs:
#                                 raise JobError("Duplicate job %s" % name)
#                             job = import_job(app_name, name, subdir)
#                             if only_scheduled and job.when == None:
#                                 # only include jobs which are scheduled
#                                 continue
#                             if when and job.when != when:
#                                 # generic job not in same schedule
#                                 continue
#                             _jobs[(app_name, name)] = job
#                     except ImportError:
#                         pass # No job module -- continue scanning
#     return _jobs
# 
# def print_jobs(when=None, only_scheduled=False, show_when=True, \
#                 show_appname=False, show_header=True):
#     jobmap = get_jobs(when, only_scheduled=only_scheduled)
#     print "Job List: %i jobs" % len(jobmap)
#     jlist = jobmap.keys()
#     jlist.sort()
#     appname_spacer = "%%-%is" % max(len(e[0]) for e in jlist)
#     name_spacer = "%%-%is" % max(len(e[1]) for e in jlist)
#     when_spacer = "%%-%is" % max(len(e.when) for e in jobmap.values() if e.when)
#     if show_header:
#         line = " "
#         if show_appname:
#             line += appname_spacer % "appname" + " - "
#         line += name_spacer % "jobname"
#         if show_when:
#             line += " - " + when_spacer % "when"
#         line += " - help"
#         print line
#         print "-"*80
# 
#     for app_name, job_name in jlist:
#         job = jobmap[(app_name, job_name)]
#         line = " "
#         if show_appname:
#             line += appname_spacer % app_name + " - "
#         line += name_spacer % job_name
#         if show_when:
#             line += " - " + when_spacer % (job.when and job.when or "")
#         line += " - " + job.help
#         print line


# Path: django_extensions/management/commands/runjobs.py
from django.core.management.base import LabelCommand
from optparse import make_option
from django_extensions.management.jobs import get_jobs, print_jobs
        from django_extensions.management import signals
        from django.db import models
        from django.conf import settings
                import traceback

    option_list = LabelCommand.option_list + (
        make_option('--list', '-l', action="store_true", dest="list_jobs",
            help="List all jobs with their description"),
    )
    help = "Runs scheduled maintenance jobs."
    args = "[hourly daily weekly monthly yearly]"
    label = ""

    requires_model_validation = True

    def usage_msg(self):
        print "Run scheduled jobs. Please specify 'hourly', 'daily', 'weekly', 'monthly' or 'yearly'"

    def runjobs(self, when, options):
        verbosity = int(options.get('verbosity', 1))
        jobs = get_jobs(when, only_scheduled=True)
        list = jobs.keys()
        list.sort()
        for app_name, job_name in list:
            job = jobs[(app_name, job_name)]
            if verbosity>1:
                print "Executing %s job: %s (app: %s)" % (when, job_name, app_name)
            try:
                job().execute()
            except Exception, e:
                print "ERROR OCCURED IN %s JOB: %s (APP: %s)" % (when.upper(), job_name, app_name)
                print "START TRACEBACK:"
                traceback.print_exc()
                print "END TRACEBACK\n"


    def runjobs_by_signals(self, when, options):