flexget.plugins.output_exec
Covered: 38 lines
Missed: 5 lines
Skipped 13 lines
Percent: 88 %
 1
import logging
 2
import pipes
 3
import shlex
 4
import subprocess
 5
from flexget.plugin import *
 7
log = logging.getLogger('exec')
10
class OutputExec:
11
    """
12
    Execute command for entries that reach output.
14
    Example:
16
    exec: echo 'found %(title)s at %(url)s > file
18
    You can use all (available) entry fields in the command.
19
    """
21
    def validator(self):
22
        from flexget import validator
23
        return validator.factory('text')
26
    @priority(100)
27
    def on_feed_output(self, feed):
28
        for entry in feed.accepted:
29
            try:
30
                cmd = feed.config['exec']
31
                args = []
33
                for arg in shlex.split(cmd.encode('utf-8'), comments=True):
34
                    arg = unicode(arg, 'utf-8')
35
                    formatted = arg % entry
37
                    arg = pipes.quote(formatted)
38
                    args.append(arg)
39
                cmd = ' '.join(args)
40
            except KeyError, e:
41
                log.error('Entry %s does not have required field %s' % (entry['title'], e.message))
42
                continue
43
            if feed.manager.options.test:
44
                log.info('Would execute: %s' % cmd)
45
                continue
46
            log.debug('executing cmd: %s' % cmd)
47
            p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=False)
48
            (r, w) = (p.stdout, p.stdin)
49
            response = r.read()
50
            r.close()
51
            w.close()
52
            if response:
53
                log.info('Stdout: %s' % response)
55
register_plugin(OutputExec, 'exec')