Skip to content

Commit

Permalink
refactor: replace call_output with run_command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Louis Fuchs committed Mar 4, 2024
1 parent aa82c4d commit 620ba84
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 56 deletions.
24 changes: 0 additions & 24 deletions pyaptly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import argparse
import codecs
import logging
import subprocess
import sys

import yaml
Expand All @@ -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.
Expand Down
25 changes: 13 additions & 12 deletions pyaptly/state_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import re

from . import main
from . import util

lg = logging.getLogger(__name__)

Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 1 addition & 20 deletions pyaptly/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down

0 comments on commit 620ba84

Please sign in to comment.