Skip to content
Snippets Groups Projects
Commit 4dece1d4 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

Worker: implemented the move_out_of_way command.

Works on files, directories, and non-existing paths.
parent e04a2a3c
Branches
Tags
No related merge requests found
...@@ -248,6 +248,37 @@ class SleepCommand(AbstractCommand): ...@@ -248,6 +248,37 @@ class SleepCommand(AbstractCommand):
await self.worker.register_log('Done sleeping for %s seconds' % time_in_seconds) 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 @attr.s
class AbstractSubprocessCommand(AbstractCommand): class AbstractSubprocessCommand(AbstractCommand):
readline_timeout = attr.ib(default=SUBPROC_READLINE_TIMEOUT) readline_timeout = attr.ib(default=SUBPROC_READLINE_TIMEOUT)
......
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())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment