Example #1
0
def write_crontab(jobs):
    """
    Converts a series of cron jobs into a crontab style representation.

    The output consists of job lines, which are commented out if
    their schedule is not known.  Timezone lines are inserted
    where the timezone changes between jobs.  If job identifiers
    are present, CRABID will be set on the corresponding job lines.
    """

    crontab = []
    timezone = None
    firstrow = True

    for job in jobs:
        # Check if job has a schedule attached.
        time = job['time']
        if time is None:
            time = '### CRAB: UNKNOWN SCHEDULE ###'

        # Track the timezone, so that we do not repeat CRON_TZ
        # assignments unnecessarily.
        if job['timezone'] is not None and job['timezone'] != timezone:
            timezone = job['timezone']
            crontab.append('CRON_TZ=' + quote_multiword(timezone))

        elif (job['timezone'] is None and
                (timezone is not None or firstrow)):
            crontab.append('### CRAB: UNKNOWN TIMEZONE ###')
            timezone = None

        # Build the command string via a list of parts.
        command = []

        # Include the crabid in if present.
        if job['crabid'] is not None:
            command.append('CRABID=' + quote_multiword(job['crabid']))

        # Include additional variables, if "vars" is present in the job.
        vars_ = job.get('vars')
        if vars_ is not None:
            for var in sorted(vars_.keys()):
                command.append(var + '=' + quote_multiword(vars_[var]))

        command.append(job['command'])

        # Process percent signs in the command, and add input if present.
        command = ' '.join(command).replace('%', '\%')

        input_ = job.get('input')
        if input_ is not None:
            command += '%' + '%'.join(
                x.replace('%', '\%') for x in input_.splitlines())

        crontab.append(time + ' ' + command)

        firstrow = False

    return crontab
Example #2
0
    def get_crontab(self, host, user):
        """Fetches the job entries for a particular host and user and builds
        a crontab style representation.

        The output consists of job lines, which are commented out if
        their schedule is not in the database.  Timezone lines are inserted
        where the timezone changes between jobs.  If job identifiers
        are present, CRABID will be set on the corresponding job lines."""

        crontab = []
        timezone = None
        firstrow = True

        jobs = self.get_jobs(host, user)

        for job in jobs:
            # Check if job has a schedule attached.
            time = job['time']
            if time is None:
                time = '### CRAB: UNKNOWN SCHEDULE ###'

            # Track the timezone, so that we do not repeat CRON_TZ
            # assignments unnecessarily.
            if job['timezone'] is not None and job['timezone'] != timezone:
                timezone = job['timezone']
                crontab.append('CRON_TZ=' + quote_multiword(timezone))

            elif job['timezone'] is None and (timezone is not None
                                              or firstrow):
                crontab.append('### CRAB: UNKNOWN TIMEZONE ###')
                timezone = None

            # Include the crabid in the command if present.
            command = job['command']
            if job['crabid'] is not None:
                command = 'CRABID=' + quote_multiword(
                    job['crabid']) + ' ' + command

            crontab.append(time + ' ' + command)

            firstrow = False

        return crontab
Example #3
0
    def get_crontab(self, host, user):
        """Fetches the job entries for a particular host and user and builds
        a crontab style representation.

        The output consists of job lines, which are commented out if
        their schedule is not in the database.  Timezone lines are inserted
        where the timezone changes between jobs.  If job identifiers
        are present, CRABID will be set on the corresponding job lines."""

        crontab = []
        timezone = None
        firstrow = True

        jobs = self.get_jobs(host, user)

        for job in jobs:
            # Check if job has a schedule attached.
            time = job['time']
            if time is None:
                time = '### CRAB: UNKNOWN SCHEDULE ###'

            # Track the timezone, so that we do not repeat CRON_TZ
            # assignments unnecessarily.
            if job['timezone'] is not None and job['timezone'] != timezone:
                timezone = job['timezone']
                crontab.append('CRON_TZ=' + quote_multiword(timezone))

            elif (job['timezone'] is None and
                    (timezone is not None or firstrow)):
                crontab.append('### CRAB: UNKNOWN TIMEZONE ###')
                timezone = None

            # Include the crabid in the command if present.
            command = job['command']
            if job['crabid'] is not None:
                command = ('CRABID=' + quote_multiword(job['crabid']) +
                           ' ' + command)

            crontab.append(time + ' ' + command)

            firstrow = False

        return crontab