crontab.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. #
  2. # Copyright 2017, Martin Owens <doctormo@gmail.com>
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 3.0 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library.
  16. #
  17. # pylint: disable=logging-format-interpolation,too-many-lines
  18. """
  19. from crontab import CronTab
  20. import sys
  21. # Create a new non-installed crontab
  22. cron = CronTab(tab='')
  23. job = cron.new(command='/usr/bin/echo')
  24. job.minute.during(5,50).every(5)
  25. job.hour.every(4)
  26. job.dow.on('SUN')
  27. job.month.during('APR', 'JUN')
  28. job.month.also.during('OCT', 'DEC')
  29. job.every(2).days()
  30. job.setall(1, 12, None, None, None)
  31. job2 = cron.new(command='/foo/bar', comment='SomeID')
  32. job2.every_reboot()
  33. jobs = list(cron.find_command('bar'))
  34. job3 = jobs[0]
  35. job3.clear()
  36. job3.minute.every(1)
  37. sys.stdout.write(str(cron.render()))
  38. job3.enable(False)
  39. for job4 in cron.find_command('echo'):
  40. sys.stdout.write(job4)
  41. for job5 in cron.find_comment('SomeID'):
  42. sys.stdout.write(job5)
  43. for job6 in cron:
  44. sys.stdout.write(job6)
  45. for job7 in cron:
  46. job7.every(3).hours()
  47. sys.stdout.write(job7)
  48. job7.every().dow()
  49. cron.remove_all(command='/foo/bar')
  50. cron.remove_all(comment='This command')
  51. cron.remove_all(time='* * * * *')
  52. cron.remove_all()
  53. output = cron.render()
  54. cron.write()
  55. cron.write(filename='/tmp/output.txt')
  56. #cron.write_to_user(user=True)
  57. #cron.write_to_user(user='root')
  58. # Croniter Extentions allow you to ask for the scheduled job times, make
  59. # sure you have croniter installed, it's not a hard dependancy.
  60. job3.schedule().get_next()
  61. job3.schedule().get_prev()
  62. """
  63. import os
  64. import re
  65. import shlex
  66. import types
  67. import codecs
  68. import logging
  69. import tempfile
  70. import platform
  71. import subprocess as sp
  72. from time import sleep
  73. from datetime import time, date, datetime, timedelta
  74. try:
  75. from collections import OrderedDict
  76. except ImportError:
  77. # python 2.6 and below causes this error
  78. try:
  79. from ordereddict import OrderedDict
  80. except ImportError:
  81. raise ImportError("OrderedDict is required for python-crontab, you can"
  82. " install ordereddict 1.1 from pypi for python2.6")
  83. __pkgname__ = 'python-crontab'
  84. __version__ = '2.3.6'
  85. ITEMREX = re.compile(r'^\s*([^@#\s]+)\s+([^@#\s]+)\s+([^@#\s]+)\s+([^@#\s]+)'
  86. r'\s+([^@#\s]+)\s+([^\n]*?)(\s+#\s*([^\n]*)|$)')
  87. SPECREX = re.compile(r'^\s*@(\w+)\s([^#\n]*)(\s+#\s*([^\n]*)|$)')
  88. DEVNULL = ">/dev/null 2>&1"
  89. WEEK_ENUM = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
  90. MONTH_ENUM = [None, 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
  91. 'sep', 'oct', 'nov', 'dec']
  92. SPECIALS = {"reboot": '@reboot',
  93. "hourly": '0 * * * *',
  94. "daily": '0 0 * * *',
  95. "weekly": '0 0 * * 0',
  96. "monthly": '0 0 1 * *',
  97. "yearly": '0 0 1 1 *',
  98. "annually": '0 0 1 1 *',
  99. "midnight": '0 0 * * *'}
  100. SPECIAL_IGNORE = ['midnight', 'annually']
  101. S_INFO = [
  102. {'max': 59, 'min': 0, 'name': 'Minutes'},
  103. {'max': 23, 'min': 0, 'name': 'Hours'},
  104. {'max': 31, 'min': 1, 'name': 'Day of Month'},
  105. {'max': 12, 'min': 1, 'name': 'Month', 'enum': MONTH_ENUM},
  106. {'max': 6, 'min': 0, 'name': 'Day of Week', 'enum': WEEK_ENUM},
  107. ]
  108. # Detect Python3 and which OS for temperments.
  109. PY3 = platform.python_version()[0] == '3'
  110. WINOS = platform.system() == 'Windows'
  111. SYSTEMV = not WINOS and os.uname()[0] in ["SunOS", "AIX", "HP-UX"]
  112. SYSTEMV = not WINOS and (
  113. os.uname()[0] in ["SunOS", "AIX", "HP-UX"]
  114. or
  115. os.uname()[4] in ["mips"]
  116. )
  117. # Switch this on if you want your crontabs to have zero padding.
  118. ZERO_PAD = False
  119. LOG = logging.getLogger('crontab')
  120. CRONCMD = "/usr/bin/crontab"
  121. SHELL = os.environ.get('SHELL', '/bin/sh')
  122. # The shell won't actually work on windows here, but
  123. # it should be updated later in the below conditional.
  124. # pylint: disable=W0622,invalid-name,too-many-public-methods
  125. # pylint: disable=function-redefined,too-many-instance-attributes
  126. current_user = lambda: None
  127. if not WINOS:
  128. import pwd
  129. def current_user():
  130. """Returns the username of the current user"""
  131. return pwd.getpwuid(os.getuid())[0]
  132. if PY3:
  133. unicode = str
  134. basestring = str
  135. def open_pipe(cmd, *args, **flags):
  136. """Runs a program and orders the arguments for compatability.
  137. a. keyword args are flags and always appear /before/ arguments for bsd
  138. """
  139. cmd_args = tuple(shlex.split(cmd))
  140. env = flags.pop('env', None)
  141. for (key, value) in flags.items():
  142. if len(key) == 1:
  143. cmd_args += ("-%s" % key),
  144. if value is not None:
  145. cmd_args += unicode(value),
  146. else:
  147. cmd_args += ("--%s=%s" % (key, value)),
  148. args = tuple(arg for arg in (cmd_args + tuple(args)) if arg)
  149. return sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE, env=env)
  150. def _unicode(text):
  151. """Convert to the best string format for this python version"""
  152. if isinstance(text, str) and not PY3:
  153. return unicode(text, 'utf-8')
  154. if isinstance(text, bytes) and PY3:
  155. return text.decode('utf-8')
  156. return text
  157. class CronTab(object):
  158. """
  159. Crontab object which can access any time based cron using the standard.
  160. user - Set the user of the crontab (default: None)
  161. * 'user' = Load from $username's crontab (instead of tab or tabfile)
  162. * None = Don't load anything from any user crontab.
  163. * True = Load from current $USER's crontab (unix only)
  164. * False = This is a system crontab, each command has a username
  165. tab - Use a string variable as the crontab instead of installed crontab
  166. tabfile - Use a file for the crontab instead of installed crontab
  167. log - Filename for logfile instead of /var/log/syslog
  168. """
  169. def __init__(self, user=None, tab=None, tabfile=None, log=None):
  170. self.lines = None
  171. self.crons = None
  172. self.filen = None
  173. self.env = None
  174. self._parked_env = OrderedDict()
  175. # Protect windows users
  176. self.root = not WINOS and os.getuid() == 0
  177. # Storing user flag / username
  178. self._user = user
  179. # Load string or filename as inital crontab
  180. self.intab = tab
  181. self.read(tabfile)
  182. self._log = log
  183. @property
  184. def log(self):
  185. """Returns the CronLog object for this tab (user or root tab only)"""
  186. from cronlog import CronLog
  187. if self._log is None or isinstance(self._log, basestring):
  188. self._log = CronLog(self._log, user=self.user or 'root')
  189. return self._log
  190. @property
  191. def user(self):
  192. """Return user's username of this crontab if applicable"""
  193. if self._user is True:
  194. return current_user()
  195. return self._user
  196. @property
  197. def user_opt(self):
  198. """Returns the user option for the crontab commandline"""
  199. # Fedora and Mac require the current user to not specify
  200. # But Ubuntu/Debian doesn't care. Be careful here.
  201. if self._user and self._user is not True:
  202. if self._user != current_user():
  203. return {'u': self._user}
  204. return {}
  205. def __setattr__(self, name, value):
  206. """Catch setting crons and lines directly"""
  207. if name == 'lines' and value:
  208. for line in value:
  209. self.append(CronItem.from_line(line, cron=self), line, read=True)
  210. elif name == 'crons' and value:
  211. raise AttributeError("You can NOT set crons attribute directly")
  212. else:
  213. super(CronTab, self).__setattr__(name, value)
  214. def read(self, filename=None):
  215. """
  216. Read in the crontab from the system into the object, called
  217. automatically when listing or using the object. use for refresh.
  218. """
  219. self.crons = []
  220. self.lines = []
  221. self.env = OrderedVariableList()
  222. lines = []
  223. if self.intab is not None:
  224. lines = self.intab.split('\n')
  225. elif filename:
  226. self.filen = filename
  227. with codecs.open(filename, 'r', encoding='utf-8') as fhl:
  228. lines = fhl.readlines()
  229. elif self.user:
  230. (out, err) = open_pipe(CRONCMD, l='', **self.user_opt).communicate()
  231. if err and 'no crontab for' in unicode(err):
  232. pass
  233. elif err:
  234. raise IOError("Read crontab %s: %s" % (self.user, err))
  235. lines = out.decode('utf-8').split("\n")
  236. self.lines = lines
  237. def append(self, item, line='', read=False):
  238. """Append a CronItem object to this CronTab"""
  239. if item.is_valid():
  240. item.env.update(self._parked_env)
  241. self._parked_env = OrderedDict()
  242. if read and not item.comment and self.lines and \
  243. self.lines[-1] and self.lines[-1][0] == '#':
  244. item.set_comment(self.lines.pop()[1:].strip())
  245. self.crons.append(item)
  246. self.lines.append(item)
  247. elif '=' in line:
  248. if ' ' not in line or line.index('=') < line.index(' '):
  249. (name, value) = line.split('=', 1)
  250. value = value.strip()
  251. for quot in "\"'":
  252. if value[0] == quot and value[-1] == quot:
  253. value = value.strip(quot)
  254. break
  255. self._parked_env[name.strip()] = value
  256. else:
  257. if not self.crons and self._parked_env:
  258. self.env.update(self._parked_env)
  259. self._parked_env = OrderedDict()
  260. self.lines.append(line.replace('\n', ''))
  261. def write(self, filename=None, user=None, errors=False):
  262. """Write the crontab to it's source or a given filename."""
  263. if filename:
  264. self.filen = filename
  265. elif user is not None:
  266. self.filen = None
  267. self.intab = None
  268. self._user = user
  269. # Add to either the crontab or the internal tab.
  270. if self.intab is not None:
  271. self.intab = self.render()
  272. # And that's it if we never saved to a file
  273. if not self.filen:
  274. return
  275. if self.filen:
  276. fileh = open(self.filen, 'wb')
  277. else:
  278. filed, path = tempfile.mkstemp()
  279. fileh = os.fdopen(filed, 'wb')
  280. fileh.write(self.render(errors=errors).encode('utf-8'))
  281. fileh.close()
  282. if not self.filen:
  283. # Add the entire crontab back to the user crontab
  284. if self.user:
  285. proc = open_pipe(CRONCMD, path, **self.user_opt)
  286. # This could do with being cleaned up quite a bit
  287. proc.wait()
  288. proc.stdout.close()
  289. proc.stderr.close()
  290. os.unlink(path)
  291. else:
  292. os.unlink(path)
  293. raise IOError("Please specify user or filename to write.")
  294. def write_to_user(self, user=True):
  295. """Write the crontab to a user (or root) instead of a file."""
  296. return self.write(user=user)
  297. def run_pending(self, **kwargs):
  298. """Run all commands in this crontab if pending (generator)"""
  299. for job in self:
  300. ret = job.run_pending(**kwargs)
  301. if ret not in [None, -1]:
  302. yield ret
  303. def run_scheduler(self, timeout=-1, **kwargs):
  304. """Run the CronTab as an internal scheduler (generator)"""
  305. count = 0
  306. while count != timeout:
  307. now = datetime.now()
  308. if 'warp' in kwargs:
  309. now += timedelta(seconds=count * 60)
  310. for value in self.run_pending(now=now):
  311. yield value
  312. sleep(kwargs.get('cadence', 60))
  313. count += 1
  314. def render(self, errors=False):
  315. """Render this crontab as it would be in the crontab.
  316. errors - Should we not comment out invalid entries and cause errors?
  317. """
  318. crons = []
  319. for line in self.lines:
  320. if isinstance(line, (unicode, str)):
  321. if line.strip().startswith('#') or not line.strip():
  322. crons.append(line)
  323. elif not errors:
  324. crons.append('# DISABLED LINE\n# ' + line)
  325. else:
  326. raise ValueError("Invalid line: %s" % line)
  327. elif isinstance(line, CronItem):
  328. if not line.is_valid() and not errors:
  329. line.enabled = False
  330. crons.append(unicode(line))
  331. # Environment variables are attached to cron lines so order will
  332. # always work no matter how you add lines in the middle of the stack.
  333. result = unicode(self.env) + u'\n'.join(crons)
  334. if result and result[-1] not in (u'\n', u'\r'):
  335. result += u'\n'
  336. return result
  337. def new(self, command='', comment='', user=None):
  338. """
  339. Create a new cron with a command and comment.
  340. Returns the new CronItem object.
  341. """
  342. if not user and self.user is False:
  343. raise ValueError("User is required for system crontabs.")
  344. item = CronItem(command, comment, user=user, cron=self)
  345. self.append(item)
  346. return item
  347. def find_command(self, command):
  348. """Return an iter of jobs matching any part of the command."""
  349. for job in list(self.crons):
  350. if isinstance(command, type(ITEMREX)):
  351. if command.findall(job.command):
  352. yield job
  353. elif command in job.command:
  354. yield job
  355. def find_comment(self, comment):
  356. """Return an iter of jobs that match the comment field exactly."""
  357. for job in list(self.crons):
  358. if isinstance(comment, type(ITEMREX)):
  359. if comment.findall(job.comment):
  360. yield job
  361. elif comment == job.comment:
  362. yield job
  363. def find_time(self, *args):
  364. """Return an iter of jobs that match this time pattern"""
  365. for job in list(self.crons):
  366. if job.slices == CronSlices(*args):
  367. yield job
  368. @property
  369. def commands(self):
  370. """Return a generator of all unqiue commands used in this crontab"""
  371. returned = []
  372. for cron in self.crons:
  373. if cron.command not in returned:
  374. yield cron.command
  375. returned.append(cron.command)
  376. @property
  377. def comments(self):
  378. """Return a generator of all unique comments/Id used in this crontab"""
  379. returned = []
  380. for cron in self.crons:
  381. if cron.comment and cron.comment not in returned:
  382. yield cron.comment
  383. returned.append(cron.comment)
  384. def remove_all(self, *args, **kwargs):
  385. """Removes all crons using the stated command OR that have the
  386. stated comment OR removes everything if no arguments specified.
  387. command - Remove all with this command
  388. comment - Remove all with this comment or ID
  389. time - Remove all with this time code
  390. """
  391. if args:
  392. raise AttributeError("Invalid use: remove_all(command='cmd')")
  393. if 'command' in kwargs:
  394. return self.remove(*self.find_command(kwargs['command']))
  395. elif 'comment' in kwargs:
  396. return self.remove(*self.find_comment(kwargs['comment']))
  397. elif 'time' in kwargs:
  398. return self.remove(*self.find_time(kwargs['time']))
  399. return self.remove(*self.crons[:])
  400. def remove(self, *items):
  401. """Remove a selected cron from the crontab."""
  402. result = 0
  403. for item in items:
  404. if isinstance(item, (list, tuple, types.GeneratorType)):
  405. for subitem in item:
  406. result += self._remove(subitem)
  407. elif isinstance(item, CronItem):
  408. result += self._remove(item)
  409. else:
  410. raise TypeError("You may only remove CronItem objects, "\
  411. "please use remove_all() to specify by name, id, etc.")
  412. return result
  413. def _remove(self, item):
  414. """Internal removal of an item"""
  415. # Manage siblings when items are deleted
  416. for sibling in self.lines[self.lines.index(item)+1:]:
  417. if isinstance(sibling, CronItem):
  418. env = sibling.env
  419. sibling.env = item.env
  420. sibling.env.update(env)
  421. sibling.env.job = sibling
  422. break
  423. elif sibling == '':
  424. self.lines.remove(sibling)
  425. else:
  426. break
  427. self.crons.remove(item)
  428. self.lines.remove(item)
  429. return 1
  430. def __repr__(self):
  431. kind = 'System ' if self._user is False else ''
  432. if self.filen:
  433. return "<%sCronTab '%s'>" % (kind, self.filen)
  434. elif self.user and not self.user_opt:
  435. return "<My CronTab>"
  436. elif self.user:
  437. return "<User CronTab '%s'>" % self.user
  438. return "<Unattached %sCronTab>" % kind
  439. def __iter__(self):
  440. """Return generator so we can track jobs after removal"""
  441. for job in list(self.crons.__iter__()):
  442. yield job
  443. def __getitem__(self, i):
  444. return self.crons[i]
  445. def __unicode__(self):
  446. return self.render()
  447. def __len__(self):
  448. return len(self.crons)
  449. def __str__(self):
  450. return self.render()
  451. class CronItem(object):
  452. """
  453. An item which objectifies a single line of a crontab and
  454. May be considered to be a cron job object.
  455. """
  456. def __init__(self, command='', comment='', user=None, cron=None):
  457. self.cron = cron
  458. self.user = user
  459. self.valid = False
  460. self.enabled = True
  461. self.special = False
  462. self.comment = None
  463. self.command = None
  464. self.last_run = None
  465. self.env = OrderedVariableList(job=self)
  466. # Marker labels Ansible jobs etc
  467. self.marker = None
  468. self.pre_comment = False
  469. self._log = None
  470. # Initalise five cron slices using static info.
  471. self.slices = CronSlices()
  472. self.set_comment(comment)
  473. if command:
  474. self.set_command(command)
  475. self.valid = True
  476. @classmethod
  477. def from_line(cls, line, user=None, cron=None):
  478. """Generate CronItem from a cron-line and parse out command and comment"""
  479. obj = cls(user=user, cron=cron)
  480. obj.parse(line.strip())
  481. return obj
  482. def delete(self):
  483. """Delete this item and remove it from it's parent"""
  484. if not self.cron:
  485. raise UnboundLocalError("Cron item is not in a crontab!")
  486. else:
  487. self.cron.remove(self)
  488. def set_command(self, cmd):
  489. """Set the command and filter as needed"""
  490. self.command = cmd.strip()
  491. def set_comment(self, cmt):
  492. """Set the comment and don't filter"""
  493. if cmt and cmt[:8] == 'Ansible:':
  494. self.marker = 'Ansible'
  495. self.pre_comment = True
  496. self.comment = cmt[8:].lstrip()
  497. else:
  498. self.comment = cmt
  499. def parse(self, line):
  500. """Parse a cron line string and save the info as the objects."""
  501. line = _unicode(line)
  502. if not line or line[0] == '#':
  503. self.enabled = False
  504. line = line[1:].strip()
  505. # We parse all lines so we can detect disabled entries.
  506. self._set_parse(ITEMREX.findall(line))
  507. self._set_parse(SPECREX.findall(line))
  508. def _set_parse(self, result):
  509. """Set all the parsed variables into the item"""
  510. if not result:
  511. return
  512. self.comment = result[0][-1]
  513. if self.cron.user is False:
  514. # Special flag to look for per-command user
  515. ret = result[0][-3].split(None, 1)
  516. self.set_command(ret[-1])
  517. if len(ret) == 2:
  518. self.user = ret[0]
  519. else:
  520. self.valid = False
  521. self.enabled = False
  522. LOG.error(str("Missing user or command in system cron line."))
  523. else:
  524. self.set_command(result[0][-3])
  525. try:
  526. self.setall(*result[0][:-3])
  527. self.valid = True
  528. except (ValueError, KeyError) as err:
  529. if self.enabled:
  530. LOG.error(str(err))
  531. self.valid = False
  532. self.enabled = False
  533. def enable(self, enabled=True):
  534. """Set if this cron job is enabled or not"""
  535. if enabled in [True, False]:
  536. self.enabled = enabled
  537. return self.enabled
  538. def is_enabled(self):
  539. """Return true if this job is enabled (not commented out)"""
  540. return self.enabled
  541. def is_valid(self):
  542. """Return true if this job is valid"""
  543. return self.valid
  544. def render(self):
  545. """Render this set cron-job to a string"""
  546. self.command = _unicode(self.command)
  547. user = ''
  548. if self.cron and self.cron.user is False:
  549. if not self.user:
  550. raise ValueError("Job to system-cron format, no user set!")
  551. user = self.user + ' '
  552. result = u"%s %s%s" % (unicode(self.slices), user, self.command)
  553. if self.comment:
  554. comment = self.comment = _unicode(self.comment)
  555. if self.marker:
  556. comment = u"#%s: %s" % (self.marker, comment)
  557. else:
  558. comment = u"# " + comment
  559. if SYSTEMV or self.pre_comment:
  560. result = comment + "\n" + result
  561. else:
  562. result += ' ' + comment
  563. if not self.enabled:
  564. result = u"# " + result
  565. return unicode(self.env) + result
  566. def every_reboot(self):
  567. """Set to every reboot instead of a time pattern: @reboot"""
  568. self.clear()
  569. return self.slices.setall('@reboot')
  570. def every(self, unit=1):
  571. """
  572. Replace existing time pattern with a single unit, setting all lower
  573. units to first value in valid range.
  574. For instance job.every(3).days() will be `0 0 */3 * *`
  575. while job.day().every(3) would be `* * */3 * *`
  576. Many of these patterns exist as special tokens on Linux, such as
  577. `@midnight` and `@hourly`
  578. """
  579. return Every(self.slices, unit)
  580. def setall(self, *args):
  581. """Replace existing time pattern with these five values given as args:
  582. job.setall("1 2 * * *")
  583. job.setall(1, 2) == '1 2 * * *'
  584. job.setall(0, 0, None, '>', 'SUN') == '0 0 * 12 SUN'
  585. """
  586. return self.slices.setall(*args)
  587. def clear(self):
  588. """Clear the special and set values"""
  589. return self.slices.clear()
  590. def frequency(self, year=None):
  591. """Returns the number of times this item will execute in a given year
  592. (defaults to this year)
  593. """
  594. return self.slices.frequency(year=year)
  595. def frequency_per_year(self, year=None):
  596. """Returns the number of /days/ this item will execute on in a year
  597. (defaults to this year)
  598. """
  599. return self.slices.frequency_per_year(year=year)
  600. def frequency_per_day(self):
  601. """Returns the number of time this item will execute in any day"""
  602. return self.slices.frequency_per_day()
  603. def frequency_per_hour(self):
  604. """Returns the number of times this item will execute in any hour"""
  605. return self.slices.frequency_per_hour()
  606. def run_pending(self, now=None):
  607. """Runs the command if scheduled"""
  608. now = now or datetime.now()
  609. if self.is_enabled():
  610. if self.last_run is None:
  611. self.last_run = now
  612. next_time = self.schedule(self.last_run).get_next()
  613. if next_time < now:
  614. self.last_run = now
  615. return self.run()
  616. return -1
  617. def run(self):
  618. """Runs the given command as a pipe"""
  619. env = os.environ.copy()
  620. env.update(self.env.all())
  621. shell = self.env.get('SHELL', SHELL)
  622. (out, err) = open_pipe(shell, '-c', self.command, env=env).communicate()
  623. if err:
  624. LOG.error(err.decode("utf-8"))
  625. return out.decode("utf-8").strip()
  626. def schedule(self, date_from=None):
  627. """Return a croniter schedule if available."""
  628. if not date_from:
  629. date_from = datetime.now()
  630. try:
  631. # Croniter is an optional import
  632. from croniter.croniter import croniter
  633. except ImportError:
  634. raise ImportError("Croniter not available. Please install croniter"
  635. " python module via pip or your package manager")
  636. return croniter(self.slices.clean_render(), date_from, ret_type=datetime)
  637. def description(self, **kw):
  638. """
  639. Returns a description of the crontab's schedule (if available)
  640. **kw - Keyword arguments to pass to cron_descriptor (see docs)
  641. """
  642. try:
  643. from cron_descriptor import ExpressionDescriptor
  644. except ImportError:
  645. raise ImportError("cron_descriptor not available. Please install"\
  646. "cron_descriptor python module via pip or your package manager")
  647. exdesc = ExpressionDescriptor(self.slices.clean_render(), **kw)
  648. return exdesc.get_description()
  649. @property
  650. def log(self):
  651. """Return a cron log specific for this job only"""
  652. if not self._log and self.cron:
  653. self._log = self.cron.log.for_program(self.command)
  654. return self._log
  655. @property
  656. def minute(self):
  657. """Return the minute slice"""
  658. return self.slices[0]
  659. @property
  660. def minutes(self):
  661. """Same as minute"""
  662. return self.minute
  663. @property
  664. def hour(self):
  665. """Return the hour slice"""
  666. return self.slices[1]
  667. @property
  668. def hours(self):
  669. """Same as hour"""
  670. return self.hour
  671. @property
  672. def day(self):
  673. """Return the day slice"""
  674. return self.dom
  675. @property
  676. def dom(self):
  677. """Return the day-of-the month slice"""
  678. return self.slices[2]
  679. @property
  680. def month(self):
  681. """Return the month slice"""
  682. return self.slices[3]
  683. @property
  684. def months(self):
  685. """Same as month"""
  686. return self.month
  687. @property
  688. def dow(self):
  689. """Return the day of the week slice"""
  690. return self.slices[4]
  691. def __repr__(self):
  692. return "<CronItem '%s'>" % unicode(self)
  693. def __len__(self):
  694. return len(unicode(self))
  695. def __getitem__(self, key):
  696. return self.slices[key]
  697. def __lt__(self, value):
  698. return self.frequency() < CronSlices(value).frequency()
  699. def __gt__(self, value):
  700. return self.frequency() > CronSlices(value).frequency()
  701. def __str__(self):
  702. return self.__unicode__()
  703. def __unicode__(self):
  704. if not self.is_valid() and self.enabled:
  705. raise ValueError('Refusing to render invalid crontab.'
  706. ' Disable to continue.')
  707. return self.render()
  708. class Every(object):
  709. """Provide an interface to the job.every() method:
  710. Available Calls:
  711. minute, minutes, hour, hours, dom, doms, month, months, dow, dows
  712. Once run all units will be cleared (set to *) then proceeding units
  713. will be set to '0' and the target unit will be set as every x units.
  714. """
  715. def __init__(self, item, units):
  716. self.slices = item
  717. self.unit = units
  718. for (key, name) in enumerate(['minute', 'hour', 'dom', 'month', 'dow',
  719. 'min', 'hour', 'day', 'moon', 'weekday']):
  720. setattr(self, name, self.set_attr(key % 5))
  721. setattr(self, name+'s', self.set_attr(key % 5))
  722. def set_attr(self, target):
  723. """Inner set target, returns function"""
  724. def innercall():
  725. """Returned inner call for setting slice targets"""
  726. self.slices.clear()
  727. # Day-of-week is actually a level 2 set, not level 4.
  728. for key in range(target == 4 and 2 or target):
  729. self.slices[key].on('<')
  730. self.slices[target].every(self.unit)
  731. return innercall
  732. def year(self):
  733. """Special every year target"""
  734. if self.unit > 1:
  735. raise ValueError("Invalid value '%s', outside 1 year" % self.unit)
  736. self.slices.setall('@yearly')
  737. class CronSlices(list):
  738. """Controls a list of five time 'slices' which reprisent:
  739. minute frequency, hour frequency, day of month frequency,
  740. month requency and finally day of the week frequency.
  741. """
  742. def __init__(self, *args):
  743. super(CronSlices, self).__init__([CronSlice(info) for info in S_INFO])
  744. self.special = None
  745. self.setall(*args)
  746. self.is_valid = self.is_self_valid
  747. def is_self_valid(self, *args):
  748. """Object version of is_valid"""
  749. return CronSlices.is_valid(*(args or (self,)))
  750. @classmethod
  751. def is_valid(cls, *args): #pylint: disable=method-hidden
  752. """Returns true if the arguments are valid cron pattern"""
  753. try:
  754. return bool(cls(*args))
  755. except (ValueError, KeyError):
  756. return False
  757. def setall(self, *slices):
  758. """Parses the various ways date/time frequency can be specified"""
  759. self.clear()
  760. if len(slices) == 1:
  761. (slices, self.special) = self._parse_value(slices[0])
  762. if slices[0] == '@reboot':
  763. return
  764. if id(slices) == id(self):
  765. raise AssertionError("Can not set cron to itself!")
  766. for set_a, set_b in zip(self, slices):
  767. set_a.parse(set_b)
  768. @staticmethod
  769. def _parse_value(value):
  770. """Parse a single value into an array of slices"""
  771. if isinstance(value, basestring) and value:
  772. return CronSlices._parse_str(value)
  773. if isinstance(value, CronItem):
  774. return value.slices, None
  775. elif isinstance(value, datetime):
  776. return [value.minute, value.hour, value.day, value.month, '*'], None
  777. elif isinstance(value, time):
  778. return [value.minute, value.hour, '*', '*', '*'], None
  779. elif isinstance(value, date):
  780. return [0, 0, value.day, value.month, '*'], None
  781. # It might be possible to later understand timedelta objects
  782. # but there's no convincing mathematics to do the conversion yet.
  783. elif not isinstance(value, (list, tuple)):
  784. raise ValueError("Unknown type: {}".format(type(value).__name__))
  785. return value, None
  786. @staticmethod
  787. def _parse_str(value):
  788. """Parse a string which contains slice information"""
  789. key = value.lstrip('@').lower()
  790. if value.count(' ') == 4:
  791. return value.strip().split(' '), None
  792. elif key in SPECIALS.keys():
  793. return SPECIALS[key].split(' '), '@' + key
  794. elif value.startswith('@'):
  795. raise ValueError("Unknown special '{}'".format(value))
  796. return [value], None
  797. def clean_render(self):
  798. """Return just numbered parts of this crontab"""
  799. return ' '.join([unicode(s) for s in self])
  800. def render(self):
  801. "Return just the first part of a cron job (the numbers or special)"
  802. slices = self.clean_render()
  803. if self.special:
  804. return self.special
  805. elif not SYSTEMV:
  806. for (name, value) in SPECIALS.items():
  807. if value == slices and name not in SPECIAL_IGNORE:
  808. return "@%s" % name
  809. return slices
  810. def clear(self):
  811. """Clear the special and set values"""
  812. self.special = None
  813. for item in self:
  814. item.clear()
  815. def frequency(self, year=None):
  816. """Return frequence per year times frequency per day"""
  817. return self.frequency_per_year(year=year) * self.frequency_per_day()
  818. def frequency_per_year(self, year=None):
  819. """Returns the number of times this item will execute
  820. in a given year (default is this year)"""
  821. result = 0
  822. if not year:
  823. year = date.today().year
  824. weekdays = list(self[4])
  825. for month in self[3]:
  826. for day in self[2]:
  827. try:
  828. if date(year, month, day).weekday() in weekdays:
  829. result += 1
  830. except ValueError:
  831. continue
  832. return result
  833. def frequency_per_day(self):
  834. """Returns the number of times this item will execute in any day"""
  835. return len(self[0]) * len(self[1])
  836. def frequency_per_hour(self):
  837. """Returns the number of times this item will execute in any hour"""
  838. return len(self[0])
  839. def __str__(self):
  840. return self.render()
  841. def __eq__(self, arg):
  842. return self.render() == CronSlices(arg).render()
  843. class SundayError(KeyError):
  844. """Sunday was specified as 7 instead of 0"""
  845. pass
  846. class Also(object):
  847. """Link range values together (appending instead of replacing)"""
  848. def __init__(self, obj):
  849. self.obj = obj
  850. def every(self, *a):
  851. """Also every one of these"""
  852. return self.obj.every(*a, also=True)
  853. def on(self, *a):
  854. """Also on these"""
  855. return self.obj.on(*a, also=True)
  856. def during(self, *a):
  857. """Also during these"""
  858. return self.obj.during(*a, also=True)
  859. class CronSlice(object):
  860. """Cron slice object which shows a time pattern"""
  861. def __init__(self, info, value=None):
  862. if isinstance(info, int):
  863. info = S_INFO[info]
  864. self.min = info.get('min', None)
  865. self.max = info.get('max', None)
  866. self.name = info.get('name', None)
  867. self.enum = info.get('enum', None)
  868. self.parts = []
  869. if value:
  870. self.parse(value)
  871. def parse(self, value):
  872. """Set values into the slice."""
  873. self.clear()
  874. if value is not None:
  875. for part in unicode(value).split(','):
  876. if part.find("/") > 0 or part.find("-") > 0 or part == '*':
  877. self.parts += self.get_range(part)
  878. continue
  879. self.parts.append(self.parse_value(part, sunday=0))
  880. def render(self, resolve=False):
  881. """Return the slice rendered as a crontab.
  882. resolve - return integer values instead of enums (default False)
  883. """
  884. if not self.parts:
  885. return '*'
  886. return _render_values(self.parts, ',', resolve)
  887. def __repr__(self):
  888. return "<CronSlice '%s'>" % unicode(self)
  889. def __eq__(self, value):
  890. return unicode(self) == unicode(value)
  891. def __str__(self):
  892. return self.__unicode__()
  893. def __unicode__(self):
  894. return self.render()
  895. def every(self, n_value, also=False):
  896. """Set the every X units value"""
  897. if not also:
  898. self.clear()
  899. self.parts += self.get_range(int(n_value))
  900. return self.parts[-1]
  901. def on(self, *n_value, **opts):
  902. """Set the time values to the specified placements."""
  903. if not opts.get('also', False):
  904. self.clear()
  905. for set_a in n_value:
  906. self.parts += self.parse_value(set_a, sunday=0),
  907. return self.parts
  908. def during(self, vfrom, vto, also=False):
  909. """Set the During value, which sets a range"""
  910. if not also:
  911. self.clear()
  912. self.parts += self.get_range(unicode(vfrom) + '-' + unicode(vto))
  913. return self.parts[-1]
  914. @property
  915. def also(self):
  916. """Appends rather than replaces the new values"""
  917. return Also(self)
  918. def clear(self):
  919. """clear the slice ready for new vaues"""
  920. self.parts = []
  921. def get_range(self, *vrange):
  922. """Return a cron range for this slice"""
  923. ret = CronRange(self, *vrange)
  924. if ret.dangling is not None:
  925. return [ret.dangling, ret]
  926. return [ret]
  927. def __iter__(self):
  928. """Return the entire element as an iterable"""
  929. ret = {}
  930. # An empty part means '*' which is every(1)
  931. if not self.parts:
  932. self.every(1)
  933. for part in self.parts:
  934. if isinstance(part, CronRange):
  935. for bit in part.range():
  936. ret[bit] = 1
  937. else:
  938. ret[int(part)] = 1
  939. for val in ret:
  940. yield val
  941. def __len__(self):
  942. """Returns the number of times this slice happens in it's range"""
  943. return len(list(self.__iter__()))
  944. def parse_value(self, val, sunday=None):
  945. """Parse the value of the cron slice and raise any errors needed"""
  946. if val == '>':
  947. val = self.max
  948. elif val == '<':
  949. val = self.min
  950. try:
  951. out = get_cronvalue(val, self.enum)
  952. except ValueError:
  953. raise ValueError("Unrecognised %s: '%s'" % (self.name, val))
  954. except KeyError:
  955. raise KeyError("No enumeration for %s: '%s'" % (self.name, val))
  956. if self.max == 6 and int(out) == 7:
  957. if sunday is not None:
  958. return sunday
  959. raise SundayError("Detected Sunday as 7 instead of 0!")
  960. if int(out) < self.min or int(out) > self.max:
  961. raise ValueError("'{1}', not in {0.min}-{0.max} for {0.name}".format(self, val))
  962. return out
  963. def get_cronvalue(value, enums):
  964. """Returns a value as int (pass-through) or a special enum value"""
  965. if isinstance(value, int):
  966. return value
  967. elif unicode(value).isdigit():
  968. return int(str(value))
  969. if not enums:
  970. raise KeyError("No enumeration allowed")
  971. return CronValue(unicode(value), enums)
  972. class CronValue(object): # pylint: disable=too-few-public-methods
  973. """Represent a special value in the cron line"""
  974. def __init__(self, value, enums):
  975. self.text = value
  976. self.value = enums.index(value.lower())
  977. def __lt__(self, value):
  978. return self.value < int(value)
  979. def __repr__(self):
  980. return unicode(self)
  981. def __str__(self):
  982. return self.text
  983. def __int__(self):
  984. return self.value
  985. def _render_values(values, sep=',', resolve=False):
  986. """Returns a rendered list, sorted and optionally resolved"""
  987. if len(values) > 1:
  988. values.sort()
  989. return sep.join([_render(val, resolve) for val in values])
  990. def _render(value, resolve=False):
  991. """Return a single value rendered"""
  992. if isinstance(value, CronRange):
  993. return value.render(resolve)
  994. if resolve:
  995. return str(int(value))
  996. return unicode(u'{:02d}'.format(value) if ZERO_PAD else value)
  997. class CronRange(object):
  998. """A range between one value and another for a time range."""
  999. def __init__(self, vslice, *vrange):
  1000. # holds an extra dangling entry, for example sundays.
  1001. self.dangling = None
  1002. self.slice = vslice
  1003. self.cron = None
  1004. self.seq = 1
  1005. if not vrange:
  1006. self.all()
  1007. elif isinstance(vrange[0], basestring):
  1008. self.parse(vrange[0])
  1009. elif isinstance(vrange[0], (int, CronValue)):
  1010. if len(vrange) == 2:
  1011. (self.vfrom, self.vto) = vrange
  1012. else:
  1013. self.seq = vrange[0]
  1014. self.all()
  1015. def parse(self, value):
  1016. """Parse a ranged value in a cronjob"""
  1017. if value.count('/') == 1:
  1018. value, seq = value.split('/')
  1019. try:
  1020. self.seq = self.slice.parse_value(seq)
  1021. except SundayError:
  1022. self.seq = 1
  1023. value = "0-0"
  1024. if self.seq < 1 or self.seq > self.slice.max:
  1025. raise ValueError("Sequence can not be divided by zero or max")
  1026. if value.count('-') == 1:
  1027. vfrom, vto = value.split('-')
  1028. self.vfrom = self.slice.parse_value(vfrom, sunday=0)
  1029. try:
  1030. self.vto = self.slice.parse_value(vto)
  1031. except SundayError:
  1032. if self.vfrom == 1:
  1033. self.vfrom = 0
  1034. else:
  1035. self.dangling = 0
  1036. self.vto = self.slice.parse_value(vto, sunday=6)
  1037. if self.vto < self.vfrom:
  1038. LOG.warning("Bad range '{0.vfrom}-{0.vto}'".format(self))
  1039. elif value == '*':
  1040. self.all()
  1041. else:
  1042. raise ValueError('Unknown cron range value "%s"' % value)
  1043. def all(self):
  1044. """Set this slice to all units between the miniumum and maximum"""
  1045. self.vfrom = self.slice.min
  1046. self.vto = self.slice.max
  1047. def render(self, resolve=False):
  1048. """Render the ranged value for a cronjob"""
  1049. value = '*'
  1050. if int(self.vfrom) > self.slice.min or int(self.vto) < self.slice.max:
  1051. if self.vfrom == self.vto:
  1052. value = unicode(self.vfrom)
  1053. else:
  1054. value = _render_values([self.vfrom, self.vto], '-', resolve)
  1055. if self.seq != 1:
  1056. value += "/%d" % self.seq
  1057. if value != '*' and SYSTEMV:
  1058. value = ','.join([unicode(val) for val in self.range()])
  1059. return value
  1060. def range(self):
  1061. """Returns the range of this cron slice as a iterable list"""
  1062. return range(int(self.vfrom), int(self.vto)+1, self.seq)
  1063. def every(self, value):
  1064. """Set the sequence value for this range."""
  1065. self.seq = int(value)
  1066. def __lt__(self, value):
  1067. return int(self.vfrom) < int(value)
  1068. def __gt__(self, value):
  1069. return int(self.vto) > int(value)
  1070. def __int__(self):
  1071. return int(self.vfrom)
  1072. def __str__(self):
  1073. return self.__unicode__()
  1074. def __unicode__(self):
  1075. return self.render()
  1076. class OrderedVariableList(OrderedDict):
  1077. """An ordered dictionary with a linked list containing
  1078. the previous OrderedVariableList which this list depends.
  1079. Duplicates in this list are weeded out in favour of the previous
  1080. list in the chain.
  1081. This is all in aid of the ENV variables list which must exist one
  1082. per job in the chain.
  1083. """
  1084. def __init__(self, *args, **kw):
  1085. self.job = kw.pop('job', None)
  1086. super(OrderedVariableList, self).__init__(*args, **kw)
  1087. @property
  1088. def previous(self):
  1089. """Returns the previous env in the list of jobs in the cron"""
  1090. if self.job is not None and self.job.cron is not None:
  1091. index = self.job.cron.crons.index(self.job)
  1092. if index == 0:
  1093. return self.job.cron.env
  1094. return self.job.cron[index-1].env
  1095. return None
  1096. def all(self):
  1097. """
  1098. Returns the full dictionary, everything from this dictionary
  1099. plus all those in the chain above us.
  1100. """
  1101. if self.job is not None:
  1102. ret = self.previous.all().copy()
  1103. ret.update(self)
  1104. return ret
  1105. return self.copy()
  1106. def __getitem__(self, key):
  1107. previous = self.previous
  1108. if key in self:
  1109. return super(OrderedVariableList, self).__getitem__(key)
  1110. elif previous is not None:
  1111. return previous.all()[key]
  1112. raise KeyError("Environment Variable '%s' not found." % key)
  1113. def __str__(self):
  1114. """Constructs to variable list output used in cron jobs"""
  1115. ret = []
  1116. for key, value in self.items():
  1117. if self.previous:
  1118. if self.previous.all().get(key, None) == value:
  1119. continue
  1120. if ' ' in unicode(value) or value == '':
  1121. value = '"%s"' % value
  1122. ret.append("%s=%s" % (key, unicode(value)))
  1123. ret.append('')
  1124. return "\n".join(ret)