47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
"""Hide VideoHelperSuite's background command windows without changing its media commands."""
|
|
import ast
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
|
|
def patch(source):
|
|
lines = source.splitlines(keepends=True)
|
|
offsets = [0]
|
|
for line in lines:
|
|
offsets.append(offsets[-1] + len(line.encode('utf-8')))
|
|
raw = source.encode('utf-8')
|
|
calls = []
|
|
for node in ast.walk(ast.parse(source)):
|
|
if not isinstance(node, ast.Call) or not isinstance(node.func, ast.Attribute):
|
|
continue
|
|
if not isinstance(node.func.value, ast.Name) or node.func.value.id != 'subprocess' or node.func.attr not in ('Popen', 'run', 'check_output', 'check_call', 'call'):
|
|
continue
|
|
if any(k.arg == 'creationflags' for k in node.keywords):
|
|
continue
|
|
calls.append(node)
|
|
for node in sorted(calls, key=lambda n: (n.end_lineno, n.end_col_offset), reverse=True):
|
|
end = offsets[node.end_lineno - 1] + node.end_col_offset - 1
|
|
prefix = b'' if raw[:end].rstrip().endswith(b',') else b','
|
|
raw = raw[:end] + prefix + b" creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)" + raw[end:]
|
|
result = raw.decode('utf-8')
|
|
ast.parse(result)
|
|
return result
|
|
|
|
if __name__ == '__main__':
|
|
root = pathlib.Path(sys.argv[1]).resolve(strict=True)
|
|
plans = []
|
|
for name in ('nodes.py', 'utils.py', 'load_video_nodes.py'):
|
|
path = root / 'videohelpersuite' / name
|
|
source = path.read_text(encoding='utf-8')
|
|
plans.append((path, source, patch(source)))
|
|
for path, source, result in plans:
|
|
if source == result:
|
|
print(f'Already patched: {path.name}')
|
|
continue
|
|
backup = path.with_suffix('.py.before-aigen-hidden')
|
|
if backup.exists():
|
|
raise RuntimeError(f'Backup already exists: {backup}')
|
|
shutil.copy2(path, backup)
|
|
path.write_text(result, encoding='utf-8')
|
|
print(f'Patched {path.name}; original backed up.')
|