Exemplo n.º 1
0
	def _generic_clear_jobs(args, quiet):
		Utils.DEBUG = not quiet
		count = 0
		cron = TCronTab(user=True)
		for job in cron:
			if job.is_superjob():
				job.set_name(SuperCron.TOBEDELETED)
				job.set_trigger("")
				count += 1
		cron.remove_all(comment=SuperCron.TOBEDELETED)
		cron.write_to_user(user=True)
		if count == 1:
			Utils.debug_print("1 job has been removed from your crontab.")
		else:
			Utils.debug_print("{} jobs have been removed from your crontab.".format(count))
Exemplo n.º 2
0
	def rename_job(args):
		"""rename a job in user's crontab"""
		if "quiet" in args:
			Utils.DEBUG = not args.quiet
		count = 0
		old_name = str(args.old_name)
		new_name = str(args.new_name)
		if Utils.check_job_name(new_name) == -1:
			Utils.debug_print("Error: job name cannot be '{}'.".format(name))
			sys.exit(1)
		if Utils.check_job_name(new_name) == -2:
			Utils.debug_print("Error: job name cannot contain a '%' symbol.")
			sys.exit(1)
		cron = TCronTab(user=True)
		for job in cron:
			if job.get_name() == old_name:
				job.set_name(new_name)
				count += 1
		if count:
			cron.activate_triggered_jobs(old_name, "deleted")
			cron.activate_triggered_jobs(new_name, "added")
		cron.write_to_user(user=True)
		if count == 0:
			Utils.debug_print("Error: job '{}' does not exist.".format(old_name))
		elif count == 1:
			Utils.debug_print("1 job has been renamed from '{}' to '{}'."
				.format(old_name, new_name))
		else:
			Utils.debug_print("{} jobs have been renamed from '{}' to '{}'."
				.format(count, old_name, new_name))
Exemplo n.º 3
0
	def search_job(args):
		"""That moment when you have to rely on a function to look for a job"""
		try:
			count = 0
			name = str(args.name)
			job_list = []
			cron = TCronTab(user=True)
			if name == "@all":
				for job in cron:
					job_name = job.get_name()
					enabled = "ON" if job.is_enabled() else "OFF"
					job_list.append([job_name, job.repr_trigger(), enabled, str(job.slices), job.command])
			elif name == "@supercron":
				for job in cron:
					if job.is_superjob():
						job_name = job.get_name()
						enabled = "ON" if job.is_enabled() else "OFF"
						job_list.append([job_name, enabled, job.repr_trigger(), str(job.slices), job.command])
			else:
				jobs = cron.find_name(name)
				for job in jobs:
					enabled = "ON" if job.is_enabled() else "OFF"
					job_list.append([name, enabled, job.repr_trigger(), str(job.slices), job.command])
			if job_list:
				col_widths = []
				col_titles = ["Name", "State", "Trigger", "Repetition", "Command"]
				for i in range(0, 5):
					col_widths.append(max(max(len(n[i]) for n in job_list) + 2, len(col_titles[i]) + 2))
				Utils.debug_print("".join(word.ljust(col_widths[i]) for word, i in zip(col_titles, range(0, 5))))
				Utils.debug_print("-" * (sum(col_widths) - 2))
				for job_item in job_list:
					Utils.debug_print("".join(word.ljust(col_widths[i]) for word, i in zip(job_item, range(0, 5))))
					count += 1
			else:
				Utils.debug_print("Zero search results.")
			return count
		except:
			# in case of any error, so the unittests can detect it
			return -1
Exemplo n.º 4
0
	def trigger_job(args):
		remove_trigger = False
		count = 0
		cron = TCronTab(user=True)
		if "quiet" in args:
			Utils.DEBUG = not args.quiet
		name = str(args.name)
		trigger = str(args.trigger[0])
		jobs = cron.find_name(name)
		if trigger.lower().strip() == "none":
			remove_trigger = True
			for job in jobs:
				job.set_trigger("")
				count += 1
		else:
			trigger_list = Utils.parse_trigger(trigger.strip())
			if trigger_list:
				for job in jobs:
					job.set_trigger(trigger_list)
					count += 1
			else:
				print(trigger.strip())
				Utils.debug_print("Error: invalid trigger (expected format is \"NONE\" or \"ACTION if NAME is STATE\").")
				sys.exit(1)
		cron.write_to_user(user=True)
		if remove_trigger:
			if count == 1:
				Utils.debug_print("Trigger was removed from 1 job named '{}'.".format(name))
			else:
				Utils.debug_print("Trigger was removed from {} jobs named '{}'.".format(count, name))
		else:
			if count == 1:
				Utils.debug_print("Trigger '{}' was added to 1 job named '{}'."
					.format(trigger.strip(), name))
			else:
				Utils.debug_print("Trigger '{}' was added to {} jobs named '{}'."
					.format(trigger.strip(), count, name))
Exemplo n.º 5
0
	def _generic_enable_job(name, enable_it, quiet=None):
		"""enable or disable job(s) by their name"""
		if quiet != None:
			Utils.DEBUG = not quiet
		count = 0
		cron = TCronTab(user=True)
		for job in cron:
			if job.get_name() == name and job.is_enabled() != enable_it:
				job.enable(enable_it)
				count += 1
		if enable_it:
			action = "enabled"
			if count:
				cron.activate_triggered_jobs(name, "enabled")
		else:
			action = "disabled"
			if count:
				cron.activate_triggered_jobs(name, "disabled")
		cron.activate_triggered_jobs(name, "toggled")
		cron.write_to_user(user=True)
		if count == 1:
			Utils.debug_print("1 job named '{}' has been {}.".format(name, action))
		else:
			Utils.debug_print("{} jobs named '{}' have been {}.".format(count, name, action))
Exemplo n.º 6
0
	def delete_job(args):
		"""delete the specified job from user's crontab"""
		if "quiet" in args:
			Utils.DEBUG = not args.quiet
		name = str(args.name)
		count = 0
		cron = TCronTab(user=True)
		for job in cron:
			if job.get_name() == name:
				cron.remove(job)
				count += 1
		if count:
			cron.activate_triggered_jobs(name, "deleted")
		cron.write_to_user(user=True)
		if count == 1:
			Utils.debug_print("1 job named '{}' has been deleted.".format(name))
		else:
			Utils.debug_print("{} jobs named '{}' have been deleted.".format(count, name))
Exemplo n.º 7
0
	def add_job(args):
		"""add the job to crontab"""
		if "quiet" in args:
			Utils.DEBUG = not args.quiet
		name = str(args.name)
		if Utils.check_job_name(name) == -1:
			Utils.debug_print("Error: job name cannot be '{}'.".format(name))
			sys.exit(1)
		if Utils.check_job_name(name) == -2:
			Utils.debug_print("Error: job name cannot contain a '%' symbol.")
			sys.exit(1)
		command = str(args.command[0])
		repetition = str(args.repetition[0])
		repeat = Repetition.parse_repetition(repetition)
		if not repeat:
			Utils.debug_print("Error: invalid repetition sentence: '{}'."
				.format(repetition))
			sys.exit(1)
		cron = TCronTab(user=True)
		job = cron.new(command=command, comment=name)
		if "reboot" in repeat:
			job.every_reboot()
		else:
			if "min_every" in repeat:
				job.minute.every(repeat['min_every'])
			if "min_on" in repeat:
				job.minute.on(repeat['min_on'])
			if "hour_every" in repeat:
				job.hour.every(repeat['hour_every'])
			if "hour_on" in repeat:
				job.hour.on(repeat['hour_on'])
			if "day_every" in repeat:
				job.day.every(repeat['day_every'])
			if "day_on" in repeat:
				job.day.on(repeat['day_on'])
			if "dow_on" in repeat:
				job.dow.on(*repeat['dow_on'])
			if "dow_during" in repeat:
				job.dow.during(*repeat['dow_during'])
			if "month_every" in repeat:
				job.month.every(repeat['month_every'])
			if "month_during" in repeat:
				job.month.during(*repeat['month_during'])
			if "month_on" in repeat:
				job.month.on(*repeat['month_on'])
		job.enable()
		cron.activate_triggered_jobs(name, "added")
		cron.write_to_user(user=True)
		Utils.debug_print("Job named '{}' has been successfully added.".format(name))