Skip to content

Commit

Permalink
Do not use decode on subprocess.check_output
Browse files Browse the repository at this point in the history
The argument universal_newlines is passed in which means it returns
strings rather than bytes. That means there is no decode().

Output with Python 3.7.5 as /usr/bin/python before this patch:

    [WARNING]:  * Failed to parse /home/ekohl/dev/forklift/inventories/vagrant.py with script plugin: Inventory script (/home/ekohl/dev/forklift/inventories/vagrant.py) had an execution
    error: Traceback (most recent call last):   File "/home/ekohl/dev/forklift/inventories/vagrant.py", line 119, in <module>     main()   File
    "/home/ekohl/dev/forklift/inventories/vagrant.py", line 111, in main     hosts = list_running_hosts()   File "/home/ekohl/dev/forklift/inventories/vagrant.py", line 52, in
    list_running_hosts     hosts = list(get_running_hosts())   File "/home/ekohl/dev/forklift/inventories/vagrant.py", line 39, in get_running_hosts     status =
subprocess.check_output(cmd.split(), universal_newlines=True).decode().rstrip() AttributeError: 'str' object has no attribute 'decode'
  • Loading branch information
ekohl authored and ehelms committed Jan 13, 2020
1 parent 72d963a commit ad21b06
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions inventories/vagrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_running_hosts():
return

cmd = "vagrant status --machine-readable"
status = subprocess.check_output(cmd.split(), universal_newlines=True).decode().rstrip()
status = subprocess.check_output(cmd.split(), universal_newlines=True).rstrip()

for line in status.split('\n'):
if len(line.split(',')) == 4:
Expand Down Expand Up @@ -65,7 +65,7 @@ def list_running_hosts():
def get_ssh_configs(hosts):
cmd = ['vagrant', 'ssh-config'] + hosts
try:
output = subprocess.check_output(cmd, universal_newlines=True, stderr=DEVNULL).decode()
output = subprocess.check_output(cmd, universal_newlines=True, stderr=DEVNULL)
except subprocess.CalledProcessError:
return None

Expand All @@ -85,7 +85,7 @@ def get_host_ssh_config(config, host):
def get_variables(hosts):
cmd = [os.path.join(os.path.dirname(os.path.dirname(__file__)), 'bin', 'ansible-vars')] + hosts
try:
output = subprocess.check_output(cmd, universal_newlines=True, stderr=DEVNULL).decode()
output = subprocess.check_output(cmd, universal_newlines=True, stderr=DEVNULL)
except subprocess.CalledProcessError:
return {}

Expand Down

0 comments on commit ad21b06

Please sign in to comment.