#!/usr/bin/python
"""Queue manager
This module depends on pyinotify: http://pyinotify.sourceforge.net/
It should:
  * listen for new files on a directory, 
  * decompress the archives to a temporary directory,
  * pass path of the directory to commander,
  * waits for the commander to finish.

Note, the last two steps should be grouped together: queue_manager should
call a script ./callback located in archive which does this shit."""


import sys
import tempfile
import shutil
import misc
import vmcheckerpaths

from subprocess import check_call
from os.path import join
from pyinotify import WatchManager, Notifier, ProcessEvent, EventsCodes


__author__ = 'Alexandru Mosoi <brtzsnr@gmail.com>'


class _QueueManager(ProcessEvent):
    def process_IN_CLOSE_WRITE(self, event):
        _process_job(event.path, event.name)


def _process_job(path, name):
    location = tempfile.mkdtemp(prefix='vmchecker-')
    archive = join(path, name)
    print 'Expanding archive `%s\' at `%s\'.' % (archive, location)

    check_call(('tar', '-C', location, '-xzvf', archive))

    print 'Cleaning `%s\'' % location
    shutil.rmtree(location)


def main():
    wm = WatchManager()
    notifier = Notifier(wm, _QueueManager())
    wm.add_watch(vmcheckerpaths.dir_queue(), EventsCodes.ALL_FLAGS['IN_CLOSE_WRITE'])
    notifier.loop(callback=lambda self: self.proc_fun())


if __name__ == '__main__':
    main()

@@ -903,16 +903,92 @@
 hecker-'
+, %0A                                dir=vmcheckerpaths.dir_tester_unzip_tmp()
 )%0A    ar
@@ -1100,19 +1100,21 @@
 all(
-('tar
+%5B'unzip
 ', '-
-C
+d
 ', l
@@ -1125,25 +1125,16 @@
 ion,
- '-xzvf',
  archive
 ))%0A%0A
@@ -1129,17 +1129,17 @@
  archive
-)
+%5D
 )%0A%0A    p

use a temp path in VMCHECKER_ROOT