From fe8a281d2d48c2574ea97b6be65713fb2e5be4ef Mon Sep 17 00:00:00 2001 From: Elliot Nunn Date: Sat, 25 May 2019 09:04:16 +0800 Subject: [PATCH] Make patch algorithm way cooler --- tbxi/dispatcher.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tbxi/dispatcher.py b/tbxi/dispatcher.py index 400aea7..b6f6fce 100644 --- a/tbxi/dispatcher.py +++ b/tbxi/dispatcher.py @@ -2,6 +2,7 @@ import importlib from subprocess import run, PIPE +import fnmatch import os from os import path @@ -18,6 +19,17 @@ class WrongFormat(Exception): pass +def strip_patch_name_ext(name): + # We need to match patch filenames to other filenames using fnmatch, + # but first we need to remove '.patch.sh' or '.patch' from the title + + # If it isn't a patch, return None + + for i in range(2): + name, ext = path.splitext(name) + if ext.lower() == '.patch': return name + + def build_dir(p): for fmt in FORMATS: mod = importlib.import_module('..%s_build' % fmt, __name__) @@ -52,11 +64,23 @@ def build_path(p): # Search the directory of the file for executable patches for sib in sorted(os.scandir(parent), key=lambda ent: ent.name): # Does the filename match? - if sib.name.startswith(name) and 'patch' in sib.name[len(name):]: + pattern = strip_patch_name_ext(sib.name) + if pattern and fnmatch.fnmatch(name, pattern): # This is a bit unsafe, so prompt the user (to pipe in `yes`...) if input('Apply %s to %s? [y/N] ' % (sib.name, name)).lower().startswith('y'): - result = run([sib.path], cwd=parent, input=data, stdout=PIPE, check=True) - data = result.stdout + # The script is told the original filename, but it should READ FROM STDIN! + cmd = [sib.path, name] + + # Run the script as a unixy filter + result = run(cmd, cwd=parent, input=data, stdout=PIPE) + + # Return 0 to apply stdout, 1 to nop, anything else to fail + if result.returncode == 0: + data = result.stdout + elif result.returncode == 1: + pass + else: + result.check_returncode() return data