Skip to content

Commit

Permalink
elbepack: migrate system_out to subprocess package
Browse files Browse the repository at this point in the history
The subprocess APIs are more powerful, better documented and
standardized.

Signed-off-by: Thomas Weißschuh <[email protected]>
Reviewed-by: Benedikt Spranger <[email protected]>
  • Loading branch information
t-8ch committed Mar 18, 2024
1 parent e04dc70 commit e1b0661
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
7 changes: 5 additions & 2 deletions elbepack/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
import shutil
import subprocess
import sys
from optparse import OptionParser

Expand All @@ -13,7 +14,7 @@
from elbepack.directories import elbe_dir, init_template_dir
from elbepack.filesystem import Filesystem
from elbepack.log import elbe_logging
from elbepack.shellhelper import do, system, system_out
from elbepack.shellhelper import do, system
from elbepack.templates import get_initvm_preseed, write_template
from elbepack.treeutils import etree
from elbepack.validate import validate_xml
Expand Down Expand Up @@ -214,7 +215,9 @@ def run_command(argv):
keys.append(key.et.text)

if opt.cdrom:
keys.append(system_out(f'7z x -so "{opt.cdrom}" repo.pub'))
keys.append(subprocess.run([
'7z', 'x', '-so', opt.cdrom, 'repo.pub',
], check=True, capture_output=True, encoding='utf-8').stdout)

import_keyring = os.path.join(out_path, 'elbe-keyring')

Expand Down
7 changes: 4 additions & 3 deletions elbepack/commands/parselicence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import io
import os
import subprocess
import sys
from datetime import datetime
from optparse import OptionParser
from tempfile import NamedTemporaryFile

from elbepack.shellhelper import system_out
from elbepack.treeutils import etree
from elbepack.version import elbe_version

Expand Down Expand Up @@ -93,8 +93,9 @@ def map_lic(self, pkgname, licenses, errors):
def scan_nomos(license_text):
with NamedTemporaryFile() as f:
f.write(license_text.encode('utf-8'))
nomos_out = system_out(
f'/usr/share/fossology/nomos/agent/nomos "{f.name}"')
nomos_out = subprocess.run([
'/usr/share/fossology/nomos/agent/nomos', f.name,
], check=True, capture_output=True, encoding='utf-8').stdout

expected_start = f'File {os.path.basename(f.name)} contains license(s) '
if not nomos_out.startswith(expected_start):
Expand Down
26 changes: 0 additions & 26 deletions elbepack/shellhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,6 @@ def command_out(cmd, stdin=None, output=PIPE, env_add=None):
return p.returncode, out


def system_out(cmd, stdin=None, allow_fail=False, env_add=None):
"""system_out() - Wrapper around command_out().
On failure, raises an exception if allow_fail=False, on success,
returns the output of cmd.
--
>>> system_out("false") # doctest: +ELLIPSIS
Traceback (most recent call last):
...
subprocess.CalledProcessError: ...
>>> system_out("false", allow_fail=True)
''
"""
code, out = command_out(cmd, stdin=stdin, env_add=env_add)

if code != 0:
if not allow_fail:
raise subprocess.CalledProcessError(code, cmd)

return out


def command_out_stderr(cmd, stdin=None, env_add=None):
"""command_out_stderr() - Execute cmd in a shell.
Expand Down

0 comments on commit e1b0661

Please sign in to comment.