Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions chkcrontab
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
Expand Down Expand Up @@ -31,21 +31,19 @@ import chkcrontab_lib as check

from optparse import OptionParser
from argparse import ArgumentParser
try:
from _version import __version__
except ImportError:
__version__ = 'unknown'

def main(argv):
"""main program."""
parser = ArgumentParser(
description='Parse crontab files; check for potential syntax errors.',
version=__version__)
)
parser.add_argument('crontab', help='crontab file to parse')
parser.add_argument('-w', '--whitelist', action='append',
dest='whitelisted_users',
help='user to ignore when warning of unrecognized users; may be passed multiple times',
default=['postgres', 'buildbot'])
parser.add_argument('-u', '--user', action='store_true',
dest='user', help='crontab user')
parser.add_argument('-n', '--no-check-passwd', action='store_false',
dest='check_passwd', help='disable user lookup')
parser.add_argument('-q', '--quiet', action='store_true',
Expand All @@ -57,4 +55,5 @@ def main(argv):
return check.check_crontab(args, check.LogCounter(args.quiet))

if __name__ == '__main__':
sys.exit(main(sys.argv))
main(sys.argv)
sys.exit(0)
36 changes: 24 additions & 12 deletions chkcrontab_lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
Expand Down Expand Up @@ -716,6 +716,8 @@ def ValidateAndLog(self, log):
# User checks.
if self.user in self.whitelisted_users:
pass
elif self.user is False:
pass
elif len(self.user) > 31:
log.LineError(log.MSG_INVALID_USER,
'Username too long "%s"' % self.user)
Expand Down Expand Up @@ -806,7 +808,8 @@ def ValidateAndLog(self, log):
class CronLineFactory(object):
"""Classify a line in a cron field by what type of line it is."""

def __init__(self):
def __init__(self, is_user_crontab=False):
self.is_user_crontab = is_user_crontab
pass

def ParseLine(self, line, options ):
Expand All @@ -819,14 +822,20 @@ def ParseLine(self, line, options ):
Returns:
A CronLine* class (must have a ValidateAndLog method).
"""
chkcrontab_cmd = re.compile('##*\s*chkcrontab:\s*(.*)=(.*)')
assignment_line_re = re.compile('[a-zA-Z_][a-zA-Z0-9_]*\s*=(.*)')
at_line_re = re.compile('@(\S+)\s+(\S+)\s+(.*)')
cron_time_field_re = '[\*0-9a-zA-Z,/-]+'
time_field_job_line_re = re.compile(
'^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(\S+)\s+(.*)' %
(cron_time_field_re, cron_time_field_re, cron_time_field_re,
cron_time_field_re, cron_time_field_re))
chkcrontab_cmd = re.compile(r'##*\s*chkcrontab:\s*(.*)=(.*)')
assignment_line_re = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*\s*=(.*)')
at_line_re = re.compile(r'@(\S+)\s+(\S+)\s+(.*)')
cron_time_field_re = r'[\*0-9a-zA-Z,/-]+'
if self.is_user_crontab:
time_field_job_line_re = re.compile(
r'^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(.*)' %
(cron_time_field_re, cron_time_field_re, cron_time_field_re,
cron_time_field_re, cron_time_field_re))
else:
time_field_job_line_re = re.compile(
r'^\s*(%s)\s+(%s)\s+(%s)\s+(%s)\s+(%s)\s+(\S+)\s+(.*)' %
(cron_time_field_re, cron_time_field_re, cron_time_field_re,
cron_time_field_re, cron_time_field_re))

if not line:
return CronLineEmpty()
Expand Down Expand Up @@ -857,7 +866,10 @@ def ParseLine(self, line, options ):
'month': match.groups()[3],
'day of week': match.groups()[4],
}
return CronLineTime(field, match.groups()[5], match.groups()[6], options)
if self.is_user_crontab:
return CronLineTime(field, False, match.groups()[5], options)
else:
return CronLineTime(field, match.groups()[5], match.groups()[6], options)

return CronLineUnknown()

Expand Down Expand Up @@ -1100,7 +1112,7 @@ def check_crontab(arguments, log):
' [A-Za-z0-9_-]+ .')

line_no = 0
cron_line_factory = CronLineFactory()
cron_line_factory = CronLineFactory(arguments.user)
with open(arguments.crontab, 'r') as crontab_f:
for line in crontab_f:
missing_newline = line[-1] != "\n"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
Expand Down