diff options
author | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-08-03 14:13:02 +0100 |
---|---|---|
committer | Radostin Stoyanov <rstoyanov1@gmail.com> | 2017-08-03 14:27:40 +0100 |
commit | 3ec0454995dec97fb5fa1eceda93c59d33df3d0e (patch) | |
tree | ad8630d27ed09bd6e987df2418baccf404169dc3 /src | |
parent | 6f5d1932c610f1bde6514ce2227dad5c1014ff42 (diff) | |
download | virt-bootstrap.git-3ec0454995dec97fb5fa1eceda93c59d33df3d0e.tar.gz virt-bootstrap.git-3ec0454995dec97fb5fa1eceda93c59d33df3d0e.tar.xz virt-bootstrap.git-3ec0454995dec97fb5fa1eceda93c59d33df3d0e.zip |
Use explicit import
Reduce the number of import statements and improve readability.
Update the unit tests to match these changes.
Diffstat (limited to 'src')
-rw-r--r-- | src/virtBootstrap/sources.py | 11 | ||||
-rw-r--r-- | src/virtBootstrap/utils.py | 32 |
2 files changed, 32 insertions, 11 deletions
diff --git a/src/virtBootstrap/sources.py b/src/virtBootstrap/sources.py index f4bae72..40b66f9 100644 --- a/src/virtBootstrap/sources.py +++ b/src/virtBootstrap/sources.py @@ -25,7 +25,7 @@ import shutil import getpass import os import logging -from subprocess import CalledProcessError, PIPE, Popen +import subprocess from virtBootstrap import utils @@ -259,12 +259,17 @@ class DockerSource(object): """ Parse the output from skopeo copy to track download progress. """ - proc = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True) + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True + ) # Without `make_async`, `fd.read` in `read_async` blocks. utils.make_async(proc.stdout) if not self.parse_output(proc): - raise CalledProcessError(proc.returncode, ' '.join(cmd)) + raise subprocess.CalledProcessError(proc.returncode, ' '.join(cmd)) def validate_image_layers(self): """ diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py index dbe4677..63ef57a 100644 --- a/src/virtBootstrap/utils.py +++ b/src/virtBootstrap/utils.py @@ -27,12 +27,12 @@ import fcntl import hashlib import json import os +import subprocess import sys import tempfile import logging import re -from subprocess import CalledProcessError, PIPE, Popen import passlib.hosts # pylint: disable=invalid-name @@ -80,7 +80,11 @@ def execute(cmd): cmd_str = ' '.join(cmd) logger.debug("Call command:\n%s", cmd_str) - proc = Popen(cmd, stdout=PIPE, stderr=PIPE) + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) output, err = proc.communicate() if output: @@ -89,7 +93,7 @@ def execute(cmd): logger.debug("Stderr:\n%s", err.decode('utf-8')) if proc.returncode != 0: - raise CalledProcessError(proc.returncode, cmd_str) + raise subprocess.CalledProcessError(proc.returncode, cmd_str) def safe_untar(src, dest): @@ -170,8 +174,12 @@ def get_mime_type(path): """ Get the mime type of a file. """ - return (Popen(["/usr/bin/file", "--mime-type", path], stdout=PIPE) - .stdout.read().decode('utf-8').split()[1]) + return ( + subprocess.Popen( + ["/usr/bin/file", "--mime-type", path], + stdout=subprocess.PIPE + ).stdout.read().decode('utf-8').split()[1] + ) def create_qcow2(tar_file, layer_file, backing_file=None, size=DEF_QCOW2_SIZE): @@ -273,7 +281,11 @@ def get_image_details(src, raw=False, cmd.append('--tls-verify=false') if username and password: cmd.append("--creds=%s:%s" % (username, password)) - proc = Popen(cmd, stdout=PIPE, stderr=PIPE) + proc = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) output, error = proc.communicate() if error: raise ValueError("Image could not be retrieved:", @@ -396,8 +408,12 @@ def write_progress(prog): """ # Get terminal width try: - terminal_width = int(Popen(["stty", "size"], stdout=PIPE).stdout - .read().decode('utf-8').split()[1]) + terminal_width = int( + subprocess.Popen( + ["stty", "size"], + stdout=subprocess.PIPE + ).stdout.read().decode('utf-8').split()[1] + ) except Exception: terminal_width = 80 # Prepare message |