From 147d4a9b7612182767d168faa4f010d1972c0f69 Mon Sep 17 00:00:00 2001 From: Russ Ferriday Date: Wed, 11 Nov 2015 22:21:48 +0000 Subject: [PATCH 1/3] Allow specifying ENV variables in crontab --- .gitignore | 1 + CONTRIBUTORS.txt | 3 ++- kronos/__init__.py | 34 +++++++++++++++++++++++++--------- kronos/settings.py | 1 + kronos/tests/tests.py | 20 +++++++++++++++++--- 5 files changed, 46 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 7cd3c25..b0c5632 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build *.pyc dist django_kronos.egg-info +.idea/ diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index a5b8c3c..7bc2d11 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -1,3 +1,4 @@ Rodrigo N. Carreras (https://github.com/carrerasrodrigo) Joshua Blum (https://github.com/joshblum) -Paulo Cheadi Haddad Filho (https://github.com/paulochf) \ No newline at end of file +Paulo Cheadi Haddad Filho (https://github.com/paulochf) +Russ Ferriday (https://github.com/topiaruss) \ No newline at end of file diff --git a/kronos/__init__.py b/kronos/__init__.py index f81f295..aa483a0 100644 --- a/kronos/__init__.py +++ b/kronos/__init__.py @@ -9,7 +9,7 @@ from django.utils.importlib import import_module from kronos.settings import PROJECT_MODULE, KRONOS_PYTHON, KRONOS_MANAGE, \ - KRONOS_PYTHONPATH, KRONOS_POSTFIX, KRONOS_PREFIX + KRONOS_PYTHONPATH, KRONOS_POSTFIX, KRONOS_PREFIX, KRONOS_ENV from django.conf import settings from kronos.utils import read_crontab, write_crontab, delete_crontab from kronos.version import __version__ @@ -119,6 +119,10 @@ def install(): current_crontab = six.u(read_crontab()) new_crontab = '' + + for env_line in KRONOS_ENV.splitlines(): + new_crontab += '# KRONOS_ENV_BREAD_CRUMB for next\n%s\n' % env_line + for task in tasks: new_crontab += '%s\n' % task['fn'].cron_expression @@ -137,15 +141,27 @@ def printtasks(): def find_existing_jobs(current_crontab): - new_crontab = '' - for line in six.u(current_crontab).split('\n')[:-1]: - exp = '%(python)s %(manage)s runtask' % { - 'python': KRONOS_PYTHON, - 'manage': KRONOS_MANAGE, - } + remaining = [] + exp = '%(python)s %(manage)s runtask' % { + 'python': KRONOS_PYTHON, + 'manage': KRONOS_MANAGE, + } + for line in six.u(current_crontab).splitlines(): if not ('$KRONOS_BREAD_CRUMB' in line and exp in line): - new_crontab += '%s\n' % line - return new_crontab + remaining.append(line) + + without_env = [] + skip = False + for line in remaining: + if 'KRONOS_ENV_BREAD_CRUMB' in line: + skip = True + continue + if skip: + skip = False + continue + without_env += [line] + + return "\n".join(without_env) def uninstall(): diff --git a/kronos/settings.py b/kronos/settings.py index f8c1440..15a8bfe 100644 --- a/kronos/settings.py +++ b/kronos/settings.py @@ -9,3 +9,4 @@ PROJECT_MODULE = sys.modules['.'.join(settings.SETTINGS_MODULE.split('.')[:-1])] KRONOS_POSTFIX = getattr(settings, 'KRONOS_POSTFIX', '') KRONOS_PREFIX = getattr(settings, 'KRONOS_PREFIX', '') +KRONOS_ENV = getattr(settings, 'KRONOS_ENV', '') diff --git a/kronos/tests/tests.py b/kronos/tests/tests.py index 40bdeea..dbf3472 100644 --- a/kronos/tests/tests.py +++ b/kronos/tests/tests.py @@ -23,7 +23,7 @@ def setUp(self): load() @patch('subprocess.Popen') - def test_unintalltasks(self, mock): + def test_uninstalltasks(self, mock): """Test uninstalling tasks with the ``uninstalltasks`` command.""" mock.return_value = Mock( stdout=StringIO('crontab: installing new crontab'), @@ -35,17 +35,29 @@ def test_unintalltasks(self, mock): def test_find_existing_jobs(self): """Test uninstalling tasks with the ``uninstalltasks`` command.""" + env = """EXISTING1=other1 + # KRONOS_ENV_BREAD_CRUMB for next line + INSERTED_BY_KRONOS=important + EXISTING2=other2 + """ keep = '%(python)s %(manage)s runtask' % { 'python': KRONOS_PYTHON, 'manage': KRONOS_MANAGE, } keep2 = " keep_me $KRONOS_BREAD_CRUMB" remove = keep + keep2 - new_cron = find_existing_jobs("\n".join([keep, keep2, remove, ""])) + import pdb; pdb.set_trace() + new_cron = find_existing_jobs(env + "\n".join([ + keep, keep2, remove, ""])) self.assertIn(keep, new_cron) self.assertIn(keep2, new_cron) self.assertNotIn(remove, new_cron) + self.assertIn('EXISTING1', new_cron) + self.assertIn('EXISTING2', new_cron) + self.assertNotIn('KRONOS_ENV_BREAD_CRUMB', new_cron) + self.assertNotIn('INSERTED_BY_KRONOS', new_cron) + @patch('subprocess.Popen') def test_read_crontab(self, mock): @@ -134,6 +146,7 @@ def test_installtasks(self, mock): self.assertTrue(mock.called) + @patch('kronos.KRONOS_ENV', """FOO=bar\nBLACK=white""") @patch('subprocess.Popen') def test_installed_tasks(self, mock): """Test installing tasks with the ``installtasks`` command.""" @@ -141,9 +154,10 @@ def test_installed_tasks(self, mock): stdout=StringIO('crontab: installing new crontab'), stderr=StringIO('') ) - call_command('installtasks') calls = str(mock.mock_calls[-1]) + self.assertIn('FOO=bar', calls) + self.assertIn('runtask praise', calls) self.assertIn('runtask praise', calls) self.assertIn('runtask complain', calls) self.assertIn('manage.py task', calls) From 8d61a5d0bb1e1ad8cc665c097608b50854944580 Mon Sep 17 00:00:00 2001 From: Russ Ferriday Date: Wed, 11 Nov 2015 23:22:32 +0000 Subject: [PATCH 2/3] kill a tracepoint. --- kronos/tests/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/kronos/tests/tests.py b/kronos/tests/tests.py index dbf3472..c9c17df 100644 --- a/kronos/tests/tests.py +++ b/kronos/tests/tests.py @@ -46,7 +46,6 @@ def test_find_existing_jobs(self): } keep2 = " keep_me $KRONOS_BREAD_CRUMB" remove = keep + keep2 - import pdb; pdb.set_trace() new_cron = find_existing_jobs(env + "\n".join([ keep, keep2, remove, ""])) self.assertIn(keep, new_cron) From 160eddeb375816f41896c5608da8ec7428eda27d Mon Sep 17 00:00:00 2001 From: Russ Ferriday Date: Thu, 12 Nov 2015 12:02:40 +0000 Subject: [PATCH 3/3] Hide the format specific juggling within kronos. --- kronos/settings.py | 2 +- runtests.sh | 0 2 files changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 runtests.sh diff --git a/kronos/settings.py b/kronos/settings.py index 15a8bfe..37bd7f9 100644 --- a/kronos/settings.py +++ b/kronos/settings.py @@ -9,4 +9,4 @@ PROJECT_MODULE = sys.modules['.'.join(settings.SETTINGS_MODULE.split('.')[:-1])] KRONOS_POSTFIX = getattr(settings, 'KRONOS_POSTFIX', '') KRONOS_PREFIX = getattr(settings, 'KRONOS_PREFIX', '') -KRONOS_ENV = getattr(settings, 'KRONOS_ENV', '') +KRONOS_ENV = '\n'.join(getattr(settings, 'KRONOS_ENV', '').split('\\n')) diff --git a/runtests.sh b/runtests.sh old mode 100644 new mode 100755