summaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor/exec.py
blob: c86b48059c27b3ed6d66214d62cd5a56229cbb57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-

# Copyright 2018 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Execute processes"""

from .common import PostProcessor
import subprocess


class ExecPP(PostProcessor):

    def __init__(self, pathfmt, options):
        PostProcessor.__init__(self)

        try:
            self.args = options["command"]
            self.args[0]  # test if 'args' is subscriptable
        except (KeyError, IndexError, TypeError):
            raise TypeError("option 'command' must be a non-empty list")

        if options.get("async", False):
            self._exec = subprocess.Popen

    def run(self, pathfmt):
        self._exec([
            arg.format_map(pathfmt.keywords)
            for arg in self.args
        ])

    def _exec(self, args):
        retcode = subprocess.Popen(args).wait()
        if retcode:
            self.log.warning(
                "executing '%s' returned non-zero exit status %d",
                " ".join(args), retcode)


__postprocessor__ = ExecPP