diff --git a/pyaptly/main.py b/pyaptly/main.py index 1e39b80..16dae12 100755 --- a/pyaptly/main.py +++ b/pyaptly/main.py @@ -2,7 +2,6 @@ import argparse import codecs import logging -import subprocess import sys import yaml @@ -15,29 +14,6 @@ lg = logging.getLogger(__name__) -# TODO remove -def call_output(args, input_=None): - """Call command and return output. - - :param args: Command to execute - :type args: list - :param input_: Input to command - :type input_: bytes - """ - p = subprocess.Popen( - args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - output, err = p.communicate(input_) - if p.returncode != 0: - raise subprocess.CalledProcessError( - p.returncode, - args, - output, - err, - ) - return (output.decode("UTF-8"), err.decode("UTF-8")) - - def main(argv=None): """Define parsers and executes commands. diff --git a/pyaptly/state_reader.py b/pyaptly/state_reader.py index 0a6273f..078f475 100644 --- a/pyaptly/state_reader.py +++ b/pyaptly/state_reader.py @@ -2,7 +2,7 @@ import logging import re -from . import main +from . import util lg = logging.getLogger(__name__) @@ -70,9 +70,8 @@ def read_gpg(self): "--list-keys", "--with-colons", ] - data, _ = main.call_output(cmd) - lg.debug("GPG returned: %s", data) - for line in data.split("\n"): + result = util.run_command(cmd, stdout=util.PIPE, check=True) + for line in result.stdout.split("\n"): field = line.split(":") if field[0] in ("pub", "sub"): key = field[4] @@ -87,9 +86,9 @@ def read_publish_map(self): re_snap = re.compile(r"\s+[\w\d-]+\:\s([\w\d-]+)\s\[snapshot\]") for publish in self.publishes: prefix, dist = publish.split(" ") - data, _ = main.call_output(["aptly", "publish", "show", dist, prefix]) - - sources = self._extract_sources(data) + cmd = ["aptly", "publish", "show", dist, prefix] + result = util.run_command(cmd, stdout=util.PIPE, check=True) + sources = self._extract_sources(result.stdout) matches = [re_snap.match(source) for source in sources] snapshots = [match.group(1) for match in matches if match] self.publish_map[publish] = set(snapshots) @@ -105,8 +104,10 @@ def read_snapshot_map(self): # match example: test-snapshot [snapshot] re_snap = re.compile(r"\s+([\w\d-]+)\s\[snapshot\]") for snapshot_outer in self.snapshots: - data, _ = main.call_output(["aptly", "snapshot", "show", snapshot_outer]) - sources = self._extract_sources(data) + cmd = ["aptly", "snapshot", "show", snapshot_outer] + + result = util.run_command(cmd, stdout=util.PIPE, check=True) + sources = self._extract_sources(result.stdout) matches = [re_snap.match(source) for source in sources] snapshots = [match.group(1) for match in matches if match] self.snapshot_map[snapshot_outer] = set(snapshots) @@ -141,9 +142,9 @@ def read_aptly_list(self, type_, list_): :param list_: Read into this list :param list_: list """ - data, _ = main.call_output(["aptly", type_, "list", "-raw"]) - lg.debug("Aptly returned %s: %s", type_, data) - for line in data.split("\n"): + cmd = ["aptly", type_, "list", "-raw"] + result = util.run_command(cmd, stdout=util.PIPE, check=True) + for line in result.stdout.split("\n"): clean_line = line.strip() if clean_line: list_.add(clean_line) diff --git a/pyaptly/tests/test_helpers.py b/pyaptly/tests/test_helpers.py index 81eb9b3..4ae0e0f 100644 --- a/pyaptly/tests/test_helpers.py +++ b/pyaptly/tests/test_helpers.py @@ -1,24 +1,5 @@ """Testing testing helper functions.""" -import subprocess - -from .. import command, main, state_reader - - -def test_call_output_error(): - """Test if call_output raises errors correctly.""" - # TDOD remove - args = [ - "bash", - "-c", - "exit 42", - ] - error = False - try: - main.call_output(args) - except subprocess.CalledProcessError as e: - assert e.returncode == 42 - error = True - assert error +from .. import command, state_reader def test_command_dependency_fail():