| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2018-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Run external processes""" |
| 10 |
|
| 11 |
from .common import PostProcessor |
| 12 |
from .. import util, formatter |
| 13 |
import subprocess |
| 14 |
import os |
| 15 |
|
| 16 |
|
| 17 |
if util.WINDOWS: |
| 18 |
def quote(s): |
| 19 |
s = s.replace('"', '\\"') |
| 20 |
return f'"{s}"' |
| 21 |
else: |
| 22 |
from shlex import quote |
| 23 |
|
| 24 |
|
| 25 |
def trim(args): |
| 26 |
return (args.partition(" ") if isinstance(args, str) else args)[0] |
| 27 |
|
| 28 |
|
| 29 |
class ExecPP(PostProcessor): |
| 30 |
|
| 31 |
def __init__(self, job, options): |
| 32 |
PostProcessor.__init__(self, job) |
| 33 |
|
| 34 |
if cmds := options.get("commands"): |
| 35 |
self.cmds = [self._prepare_cmd(c) for c in cmds] |
| 36 |
execute = self.exec_many |
| 37 |
else: |
| 38 |
execute, self.args = self._prepare_cmd(options["command"]) |
| 39 |
if options.get("async", False): |
| 40 |
self._exec = self._popen |
| 41 |
|
| 42 |
self.output = options.get("output", True) |
| 43 |
self.verbose = options.get("verbose", True) |
| 44 |
self.session = False |
| 45 |
self.creationflags = 0 |
| 46 |
if options.get("session"): |
| 47 |
if util.WINDOWS: |
| 48 |
self.creationflags = subprocess.CREATE_NEW_PROCESS_GROUP |
| 49 |
else: |
| 50 |
self.session = True |
| 51 |
|
| 52 |
s = options.get("success") |
| 53 |
e = options.get("error") |
| 54 |
if s or e: |
| 55 |
from .. import actions |
| 56 |
self.action_success = None if s is None else actions.parse(s) |
| 57 |
self.action_error = None if e is None else actions.parse(e) |
| 58 |
self.action_args = {"job": job, "level": 0} |
| 59 |
else: |
| 60 |
self.action_success = self.action_error = None |
| 61 |
|
| 62 |
events = options.get("event") |
| 63 |
if events is None: |
| 64 |
events = ("after",) |
| 65 |
elif isinstance(events, str): |
| 66 |
events = events.split(",") |
| 67 |
job.register_hooks({event: execute for event in events}, options) |
| 68 |
|
| 69 |
if self._archive_init(job, options): |
| 70 |
self._archive_register(job) |
| 71 |
|
| 72 |
def _prepare_cmd(self, cmd): |
| 73 |
if isinstance(cmd, str): |
| 74 |
self._sub = util.re( |
| 75 |
r"(?i)\{(_directory(?:_unc)?|_filename" |
| 76 |
r"|_(?:temp)?path(?:_unc)?|)\}").sub |
| 77 |
return self.exec_string, cmd |
| 78 |
else: |
| 79 |
return self.exec_list, [formatter.parse(arg) for arg in cmd] |
| 80 |
|
| 81 |
def exec_list(self, pathfmt): |
| 82 |
archive = self.archive |
| 83 |
kwdict = pathfmt.kwdict |
| 84 |
|
| 85 |
if archive and archive.check(kwdict): |
| 86 |
return |
| 87 |
|
| 88 |
kwdict["_directory"] = pathfmt.directory |
| 89 |
kwdict["_filename"] = pathfmt.filename |
| 90 |
kwdict["_temppath"] = pathfmt.temppath |
| 91 |
kwdict["_path"] = pathfmt.path |
| 92 |
if util.WINDOWS: |
| 93 |
kwdict["_directory_unc"] = pathfmt.realdirectory |
| 94 |
kwdict["_path_unc"] = pathfmt.realpath |
| 95 |
|
| 96 |
args = [arg.format_map(kwdict) for arg in self.args] |
| 97 |
args[0] = os.path.expanduser(args[0]) |
| 98 |
retcode = self._exec(args, False) |
| 99 |
|
| 100 |
if archive and not retcode: |
| 101 |
archive.add(kwdict) |
| 102 |
return retcode |
| 103 |
|
| 104 |
def exec_string(self, pathfmt): |
| 105 |
archive = self.archive |
| 106 |
if archive and archive.check(pathfmt.kwdict): |
| 107 |
return |
| 108 |
|
| 109 |
self.pathfmt = pathfmt |
| 110 |
args = self._sub(self._replace, self.args) |
| 111 |
retcode = self._exec(args, True) |
| 112 |
|
| 113 |
if archive and not retcode: |
| 114 |
archive.add(pathfmt.kwdict) |
| 115 |
return retcode |
| 116 |
|
| 117 |
def exec_many(self, pathfmt): |
| 118 |
if archive := self.archive: |
| 119 |
if archive.check(pathfmt.kwdict): |
| 120 |
return |
| 121 |
self.archive = False |
| 122 |
|
| 123 |
retcode = 0 |
| 124 |
for execute, args in self.cmds: |
| 125 |
self.args = args |
| 126 |
if retcode := execute(pathfmt): |
| 127 |
# non-zero exit status |
| 128 |
break |
| 129 |
|
| 130 |
if archive: |
| 131 |
self.archive = archive |
| 132 |
if not retcode: |
| 133 |
archive.add(pathfmt.kwdict) |
| 134 |
return retcode |
| 135 |
|
| 136 |
def _exec(self, args, shell): |
| 137 |
if retcode := self._popen(args, shell).wait(): |
| 138 |
self.log.warning("'%s' returned with non-zero exit status (%d)", |
| 139 |
args if self.verbose else trim(args), retcode) |
| 140 |
if self.action_error is not None: |
| 141 |
self.action_error(self.action_args) |
| 142 |
elif self.action_success is not None: |
| 143 |
self.action_success(self.action_args) |
| 144 |
return retcode |
| 145 |
|
| 146 |
def _popen(self, args, shell): |
| 147 |
self.log.debug("Running '%s'", args if self.verbose else trim(args)) |
| 148 |
out = None if self.output else subprocess.DEVNULL |
| 149 |
return util.Popen( |
| 150 |
args, |
| 151 |
shell=shell, |
| 152 |
stdout=out, stderr=out, |
| 153 |
creationflags=self.creationflags, |
| 154 |
start_new_session=self.session, |
| 155 |
) |
| 156 |
|
| 157 |
def _replace(self, match): |
| 158 |
attr = { |
| 159 |
"" : "path", |
| 160 |
"_path" : "path", |
| 161 |
"_path_unc" : "realpath", |
| 162 |
"_temppath" : "temppath", |
| 163 |
"_temppath_unc" : "temppath", |
| 164 |
"_directory" : "directory", |
| 165 |
"_directory_unc": "realdirectory", |
| 166 |
"_filename" : "filename", |
| 167 |
}[match[1].lower()] |
| 168 |
return quote(getattr(self.pathfmt, attr)) |
| 169 |
|
| 170 |
|
| 171 |
__postprocessor__ = ExecPP |