[PATCH 1/1] readpipe: replace with subprocess.check_output

0 views
Skip to first unread message

Rob Browning

unread,
Mar 8, 2026, 3:26:22 PM (6 days ago) Mar 8
to bup-...@googlegroups.com
Signed-off-by: Rob Browning <r...@defaultvalue.org>
Tested-by: Rob Browning <r...@defaultvalue.org>
---

Pushed to main.

dev/subtree-hash | 5 +++--
lib/bup/helpers.py | 4 ----
test/ext/test_prune_older.py | 2 +-
test/int/test_commit.py | 32 ++++++++++++++++++--------------
test/int/test_git.py | 12 ++++++------
test/int/test_helpers.py | 9 +--------
6 files changed, 29 insertions(+), 35 deletions(-)

diff --git a/dev/subtree-hash b/dev/subtree-hash
index 24390bad..af4c61e2 100755
--- a/dev/subtree-hash
+++ b/dev/subtree-hash
@@ -4,10 +4,11 @@ bup_exec="$(dirname "$0")/bup-exec" || exit $?
exec "$bup_exec" "$0" ${1+"$@"}
"""

+from subprocess import check_output
import os.path, sys

from bup.compat import argv_bytes, get_argvb
-from bup.helpers import handle_ctrl_c, readpipe
+from bup.helpers import handle_ctrl_c
from bup.io import byte_stream
from bup import options

@@ -30,7 +31,7 @@ path = [argv_bytes(x) for x in extra[1:]]

while path:
target_name = path[0]
- subtree_items = readpipe([b'git', b'ls-tree', b'-z', tree_hash])
+ subtree_items = check_output([b'git', b'ls-tree', b'-z', tree_hash])
target_hash = None
for entry in subtree_items.split(b'\0'):
if not entry:
diff --git a/lib/bup/helpers.py b/lib/bup/helpers.py
index 5b487d90..e1e76eb3 100644
--- a/lib/bup/helpers.py
+++ b/lib/bup/helpers.py
@@ -329,10 +329,6 @@ def exo(cmd,
', stderr: %r' % err if err else ''))
return out, err, p

-def readpipe(argv, shell=False):
- """Run a subprocess and return its output."""
- return exo(argv, shell=shell)[0]
-

def resolve_parent(p):
"""Return the absolute path of a file without following any final symlink.
diff --git a/test/ext/test_prune_older.py b/test/ext/test_prune_older.py
index 06a46b32..54c0d585 100644
--- a/test/ext/test_prune_older.py
+++ b/test/ext/test_prune_older.py
@@ -12,7 +12,7 @@ import random, sys

from bup import compat
from bup.compat import environ
-from bup.helpers import partition, period_as_secs, readpipe
+from bup.helpers import partition, period_as_secs
from bup.io import byte_stream
from buptest import ex, exo
from wvpytest import wvfail, wvpass, wvpasseq, wvpassne, wvstart
diff --git a/test/int/test_commit.py b/test/int/test_commit.py
index bcc34883..c9396567 100644
--- a/test/int/test_commit.py
+++ b/test/int/test_commit.py
@@ -1,19 +1,23 @@

from os import environb as environ
-from subprocess import check_call
+from subprocess import check_call, check_output
import os, sys

from wvpytest import *

from bup import git
from bup.commit import _git_date_str, has_trailers, parse_commit
-from bup.helpers import readpipe
+from bup.io import path_msg


def exc(*cmd):
- print(repr(cmd), file=sys.stderr)
+ print(' '.join(path_msg(x) for x in cmd), file=sys.stderr)
check_call(cmd)

+def exo(*cmd):
+ print(' '.join(path_msg(x) for x in cmd), file=sys.stderr)
+ return check_output(cmd)
+

def test_commit_parsing(tmpdir):
def restore_env_var(name, val):
@@ -23,8 +27,8 @@ def test_commit_parsing(tmpdir):
environ[name] = val

def showval(commit, val):
- return readpipe([b'git', b'show', b'-s',
- b'--pretty=format:%s' % val, commit]).strip()
+ return exo(b'git', b'show', b'-s',
+ b'--pretty=format:%s' % val, commit).strip()

orig_cwd = os.getcwd()
workdir = tmpdir + b'/work'
@@ -39,17 +43,17 @@ def test_commit_parsing(tmpdir):
environ[b'GIT_COMMITTER_EMAIL'] = environ[b'GIT_AUTHOR_EMAIL']
try:
environ[b'GIT_DIR'] = environ[b'BUP_DIR'] = repodir
- readpipe([b'git', b'init', workdir])
+ exc(b'git', b'init', workdir)
exc(b'git', b'symbolic-ref', b'HEAD', b'refs/heads/main')
git.check_repo_or_die(repodir)
os.chdir(workdir)
with open('foo', 'wb') as f:
f.write(b'bar\n')
- readpipe([b'git', b'add', b'.'])
- readpipe([b'git', b'commit', b'-am', b'Do something',
- b'--author', b'Someone <someone@somewhere>',
- b'--date', b'Sat Oct 3 19:48:49 2009 -0400'])
- commit = readpipe([b'git', b'show-ref', b'-s', b'main']).strip()
+ exc(b'git', b'add', b'.')
+ exc(b'git', b'commit', b'-am', b'Do something',
+ b'--author', b'Someone <someone@somewhere>',
+ b'--date', b'Sat Oct 3 19:48:49 2009 -0400')
+ commit = exo(b'git', b'show-ref', b'-s', b'main').strip()
parents = showval(commit, b'%P')
tree = showval(commit, b'%T')
cname = showval(commit, b'%cn')
@@ -75,9 +79,9 @@ def test_commit_parsing(tmpdir):
WVPASSEQ(commit_items.message, b'Do something\n')
with open(b'bar', 'wb') as f:
f.write(b'baz\n')
- readpipe([b'git', b'add', '.'])
- readpipe([b'git', b'commit', b'-am', b'Do something else'])
- child = readpipe([b'git', b'show-ref', b'-s', b'main']).strip()
+ exc(b'git', b'add', b'.')
+ exc(b'git', b'commit', b'-am', b'Do something else')
+ child = exo(b'git', b'show-ref', b'-s', b'main').strip()
parents = showval(child, b'%P')
commit_items = git.get_commit_items(child, git.cp())
WVPASSEQ(commit_items.parents, [commit])
diff --git a/test/int/test_git.py b/test/int/test_git.py
index 596eceb0..f89ec874 100644
--- a/test/int/test_git.py
+++ b/test/int/test_git.py
@@ -2,8 +2,8 @@
import sys
from binascii import hexlify, unhexlify
from contextlib import ExitStack
-from subprocess import check_call
from functools import partial
+from subprocess import check_call, check_output
from time import localtime
import struct, os
import pytest
@@ -12,20 +12,20 @@ from wvpytest import *

from bup import git, path
from bup.compat import environ
-from bup.helpers import OBJECT_EXISTS, finalized, log, mkdirp, readpipe
+from bup.helpers import OBJECT_EXISTS, finalized, log, mkdirp
+from bup.io import path_msg


bup_exe = path.exe()


def exc(*cmd):
- print(repr(cmd), file=sys.stderr)
+ print(' '.join(path_msg(x) for x in cmd), file=sys.stderr)
check_call(cmd)

-
def exo(*cmd):
- print(repr(cmd), file=sys.stderr)
- return readpipe(cmd)
+ print(' '.join(path_msg(x) for x in cmd), file=sys.stderr)
+ return check_output(cmd)


def local_writer():
diff --git a/test/int/test_helpers.py b/test/int/test_helpers.py
index e26c70a8..2086be49 100644
--- a/test/int/test_helpers.py
+++ b/test/int/test_helpers.py
@@ -8,7 +8,7 @@ from bup import helpers
from bup.compat import environ
from bup.helpers import (atomically_replaced_file, detect_fakeroot,
grafted_path_components, parse_num,
- path_components, readpipe, stripped_path_components,
+ path_components, stripped_path_components,
shstr,
utc_offset_str)

@@ -107,13 +107,6 @@ def test_shstr():
WVPASSEQ(shstr(("'1", '3')), "''\"'\"'1' 3")


-def test_readpipe():
- x = readpipe([b'echo', b'42'])
- WVPASSEQ(x, b'42\n')
- with pytest.raises(Exception, match='^subprocess b?"bash -c \'exit 42\'" failed with status 42$'):
- readpipe([b'bash', b'-c', b'exit 42'])
-
-
@pytest.mark.parametrize('sync_atomic_replace', (True, False))
def test_atomically_replaced_file(sync_atomic_replace, tmpdir):
target_file = os.path.join(tmpdir, b'test-atomic-write')
--
2.47.3

Reply all
Reply to author
Forward
0 new messages