def add_svlbi_cron_job(month, year, save_dir, user=True): """ Function that adds entry to crontab with checking last SVLBI schedule for changes. :param month: Month [1 - 12]. :param year: Year (eg. 2016). :param save_dir: Directory to store files. :param user: (optional) User of crontab. If ``True`` then current user. (default: ``True``) """ # First download current version of SVLBI schedule and put it to # user-specified directory logging.debug("Downloading last SVLBI schedule to {}".format(save_dir)) if not os.path.isdir(save_dir): subprocess.Popen(['mkdir', '-p', save_dir]) watcher.get_last_svlbi_schedule(month, year, os.path.join(save_dir, 'svlbi.txt')) # Add crontab with user specified dates and directory cron = CronTab(user=user) cmd = '/home/ilya/.local/bin/cron_command.py {} {} {}'.format( month, year, save_dir) cron_job = cron.new(command=cmd, comment="checking SVLBI schedule for" " {}-{}".format(month, year)) if month == '1': month_ = '12' else: month_ = str(int(month) - 1) cron_job.month.during(month_, month) cron_job.minute.every(30) assert cron_job.is_valid() == True # comments = cron.find_comment('SVLBI schedule for {}-{}'.format(month, year)) # if comments: # raise Exception("There is already job with given parameters!") print cron_job.render() cron.write_to_user(user=user)
def add_svlbi_cron_job(month, year, save_dir, user=True): """ Function that adds entry to crontab with checking last SVLBI schedule for changes. :param month: Month [1 - 12]. :param year: Year (eg. 2016). :param save_dir: Directory to store files. :param user: (optional) User of crontab. If ``True`` then current user. (default: ``True``) """ # First download current version of SVLBI schedule and put it to # user-specified directory logging.debug("Downloading last SVLBI schedule to {}".format(save_dir)) if not os.path.isdir(save_dir): subprocess.Popen(['mkdir', '-p', save_dir]) watcher.get_last_svlbi_schedule(month, year, os.path.join(save_dir, 'svlbi.txt')) # Add crontab with user specified dates and directory cron = CronTab(user=user) cmd = '/home/ilya/.local/bin/cron_command.py {} {} {}'.format(month, year, save_dir) cron_job = cron.new(command=cmd, comment="checking SVLBI schedule for" " {}-{}".format(month, year)) if month == '1': month_ = '12' else: month_ = str(int(month) - 1) cron_job.month.during(month_, month) cron_job.minute.every(30) assert cron_job.is_valid() == True # comments = cron.find_comment('SVLBI schedule for {}-{}'.format(month, year)) # if comments: # raise Exception("There is already job with given parameters!") print cron_job.render() cron.write_to_user(user=user)
secrets = netrc.netrc() netrclogin, netrcaccount, netrcpassword = secrets.authenticators(smtpserver) if tls: s.starttls() s.login(netrclogin, netrcpassword) s.sendmail('*****@*****.**', ['*****@*****.**'], msg.as_string()) s.quit() logging.debug("Moving file {} to {}!".format(basename(f2), basename(f1))) shutil.move(f2, f1) else: logging.debug("Files {} & {} are the same!".format(basename(f1), basename(f2))) os.unlink(f2) if __name__ == '__main__': if not len(sys.argv) == 4: print("Usage: cron_command.py month year directory") sys.exit(0) month = sys.argv[1] year = sys.argv[2] # User-specified directory dir = sys.argv[3] # Get last SVLBI schedule watcher.get_last_svlbi_schedule(month, year, os.path.join(dir, 'svlbi_new.txt')) func(os.path.join(dir, 'svlbi.txt'), os.path.join(dir, 'svlbi_new.txt'))