From 4dece1d46d4c389bd0143eedc6eb66da70e54a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@stuvel.eu> Date: Fri, 27 Jan 2017 11:59:28 +0100 Subject: [PATCH] Worker: implemented the move_out_of_way command. Works on files, directories, and non-existing paths. --- .../flamenco_worker/runner.py | 31 ++++++++++ .../tests/test_runner_move_out_of_way.py | 62 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 packages/flamenco-worker-python/tests/test_runner_move_out_of_way.py diff --git a/packages/flamenco-worker-python/flamenco_worker/runner.py b/packages/flamenco-worker-python/flamenco_worker/runner.py index f65fa2be..194a152b 100644 --- a/packages/flamenco-worker-python/flamenco_worker/runner.py +++ b/packages/flamenco-worker-python/flamenco_worker/runner.py @@ -248,6 +248,37 @@ class SleepCommand(AbstractCommand): await self.worker.register_log('Done sleeping for %s seconds' % time_in_seconds) +@command_executor('move_out_of_way') +class MoveOutOfWayCommand(AbstractCommand): + def validate(self, settings: dict): + try: + src = settings['src'] + except KeyError: + return 'Missing "src"' + + if not isinstance(src, str): + return 'src must be a string' + + async def execute(self, settings: dict): + from pathlib import Path + from datetime import datetime + + src = Path(settings['src']) + if not src.exists(): + self._log.info('Render output path %s does not exist, not moving out of way', src) + await self.worker.register_log('%s: Render output path %s does not exist, ' + 'not moving out of way', self.command_name, src) + return + + mtime = src.stat().st_mtime + mdatetime = datetime.fromtimestamp(mtime) + dst = src.with_name('%s-%s' % (src.name, mdatetime.isoformat())) + + self._log.info('Moving %s to %s', src, dst) + await self.worker.register_log('%s: Moving %s to %s', self.command_name, src, dst) + src.rename(dst) + + @attr.s class AbstractSubprocessCommand(AbstractCommand): readline_timeout = attr.ib(default=SUBPROC_READLINE_TIMEOUT) diff --git a/packages/flamenco-worker-python/tests/test_runner_move_out_of_way.py b/packages/flamenco-worker-python/tests/test_runner_move_out_of_way.py new file mode 100644 index 00000000..59cba3f5 --- /dev/null +++ b/packages/flamenco-worker-python/tests/test_runner_move_out_of_way.py @@ -0,0 +1,62 @@ +from test_runner import AbstractCommandTest + + +class MoveOutOfWayTest(AbstractCommandTest): + def setUp(self): + super().setUp() + + from flamenco_worker.runner import MoveOutOfWayCommand + import tempfile + + self.tmpdir = tempfile.TemporaryDirectory() + self.cmd = MoveOutOfWayCommand( + worker=self.fworker, + task_id='12345', + command_idx=0, + ) + + def tearDown(self): + del self.tmpdir + + def test_nonexistant_source(self): + from pathlib import Path + + src = Path(self.tmpdir.name) / 'nonexistant-dir' + task = self.cmd.run({'src': str(src)}) + ok = self.loop.run_until_complete(task) + + self.assertTrue(ok) + self.assertFalse(src.exists()) + + def test_existing_source(self): + from pathlib import Path + import os + + src = Path(self.tmpdir.name) / 'existing-dir' + src.mkdir() + os.utime(str(src), (1330712280, 1330712292)) # fixed (atime, mtime) for testing + + task = self.cmd.run({'src': str(src)}) + ok = self.loop.run_until_complete(task) + self.assertTrue(ok) + + dst = src.with_name('existing-dir-2012-03-02T19:18:12') + self.assertTrue(dst.exists()) + self.assertFalse(src.exists()) + + def test_source_is_file(self): + from pathlib import Path + import os + + src = Path(self.tmpdir.name) / 'existing-file' + src.touch(exist_ok=False) + os.utime(str(src), (1330712280, 1330712292)) # fixed (atime, mtime) for testing + + task = self.cmd.run({'src': str(src)}) + ok = self.loop.run_until_complete(task) + self.assertTrue(ok) + + dst = src.with_name('existing-file-2012-03-02T19:18:12') + self.assertTrue(dst.exists()) + self.assertTrue(dst.is_file()) + self.assertFalse(src.exists()) -- GitLab