Merge lp:~chris.macnaughton/mojo/py3003 into lp:~ost-maintainers/mojo/py3

Proposed by Chris MacNaughton on 2018-02-05
Status: Needs review
Proposed branch: lp:~chris.macnaughton/mojo/py3003
Merge into: lp:~ost-maintainers/mojo/py3
Diff against target: 217 lines (+56/-13)
5 files modified
mojo/cli.py (+1/-1)
mojo/contain.py (+26/-6)
mojo/juju/debuglogs.py (+7/-1)
mojo/juju/status.py (+9/-2)
mojo/phase.py (+13/-3)
To merge this branch: bzr merge lp:~chris.macnaughton/mojo/py3003
Reviewer Review Type Date Requested Status
OpenStack Charm Testing Maintainers 2018-02-05 Pending
Review via email: mp+337159@code.launchpad.net
To post a comment you must log in.
lp:~chris.macnaughton/mojo/py3003 updated on 2018-02-05
470. By Chris MacNaughton on 2018-02-05

cleanup error output

Alexandre Gomes (alejdg) wrote :

+1

Colin Watson (cjwatson) wrote :

Any reason not to use universal_newlines=True on subprocess.check_output calls that are supposed to emit text instead? As long as unicode output can be tolerated in the Python 2 case, this is generally a simpler way to do this.

Unmerged revisions

470. By Chris MacNaughton on 2018-02-05

cleanup error output

469. By Chris MacNaughton on 2018-02-05

add more decode calls

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'mojo/cli.py'
2--- mojo/cli.py 2018-01-22 06:58:28 +0000
3+++ mojo/cli.py 2018-02-05 16:24:42 +0000
4@@ -645,7 +645,7 @@
5 logging.error(e)
6 sys.exit(1)
7 except mojo.phase.ScriptPhaseException as e:
8- logging.error(e.output)
9+ logging.error(e)
10 sys.exit(1)
11 except mojo.exceptions.ConfigNotFoundException as e:
12 logging.error(e.message)
13
14=== modified file 'mojo/contain.py'
15--- mojo/contain.py 2018-01-18 10:23:15 +0000
16+++ mojo/contain.py 2018-02-05 16:24:42 +0000
17@@ -11,6 +11,7 @@
18 import os
19 import platform
20 import pwd
21+import six
22 import shlex
23 import shutil
24 import subprocess
25@@ -568,11 +569,17 @@
26 if state:
27 cmd = ('sudo', 'lxc-info', '-s', '-n', self.container_name)
28 logging.debug("Running command: {}".format(" ".join(cmd)))
29- containers = yaml.safe_load(subprocess.check_output(cmd))
30+ output = subprocess.check_output(cmd)
31+ if six.PY3:
32+ output = output.decode('utf-8')
33+ containers = yaml.safe_load(output)
34 # lxc-info shows 'state' (precise) or 'State' (newer)
35 return state in containers.get('state', containers.get('State', {}))
36 cmd = ('sudo', 'lxc-ls', '-1')
37- containers = subprocess.check_output(cmd).split()
38+ output = subprocess.check_output(cmd)
39+ if six.PY3:
40+ output = output.decode('utf-8')
41+ containers = output.split()
42 return self.container_name in containers
43
44 @ensure_defined
45@@ -633,7 +640,10 @@
46 logging.debug("Running command: {}".format(" ".join(cmd)))
47
48 # lxc-info -i will return a row for each IP, plus a newline
49- found_ips = subprocess.check_output(cmd).split('\n')
50+ output = subprocess.check_output(cmd)
51+ if six.PY3:
52+ output = output.decode('utf-8')
53+ found_ips = output.split('\n')
54 if len(found_ips) > 1:
55 networking = True
56 else:
57@@ -645,7 +655,10 @@
58 if attempts >= max_attempts:
59 logging.error("No networking after {} attempts, giving up".format(attempts))
60 cmd = ('sudo', 'lxc-info', '-n', self.container_name)
61- lxc_info_output = subprocess.check_output(cmd).split('\n')
62+ output = subprocess.check_output(cmd)
63+ if six.PY3:
64+ output = output.decode('utf-8')
65+ lxc_info_output = output.split('\n')
66 logging.error("lxc-info output: {}".format(lxc_info_output))
67 return False
68 return True
69@@ -694,6 +707,8 @@
70 "".format(self.container_name, ' '.join(command)))
71 try:
72 output = subprocess.check_output(cmd, stderr=stderr)
73+ if six.PY3:
74+ output = output.decode('utf-8')
75 if output.strip():
76 logging.info(output)
77 finally:
78@@ -791,9 +806,12 @@
79 return False
80
81 def create_user(self, user):
82+ output = subprocess.check_output(
83+ ['getent', 'passwd', user])
84+ if six.PY3:
85+ output = output.decode('utf-8')
86 (username, shadow, uid,
87- gid, name, home, shell) = subprocess.check_output(
88- ['getent', 'passwd', user]).split(':')
89+ gid, name, home, shell) = output.split(':')
90 # if the uid exists update the name to match the caller
91 try:
92 command = "useradd --home {} --create-home --uid {} {}" \
93@@ -843,6 +861,8 @@
94 "project. Running command without a container: {}"
95 "".format(command))
96 output = subprocess.check_output(command, stderr=subprocess.STDOUT)
97+ if six.PY3:
98+ output = output.decode('utf-8')
99 if output.strip():
100 logging.info(output)
101
102
103=== modified file 'mojo/juju/debuglogs.py'
104--- mojo/juju/debuglogs.py 2017-01-11 02:34:42 +0000
105+++ mojo/juju/debuglogs.py 2018-02-05 16:24:42 +0000
106@@ -1,6 +1,7 @@
107 import ast
108 import logging
109 import subprocess
110+import six
111 import yaml
112
113 import mojo.juju
114@@ -97,11 +98,16 @@
115 '[ -z "$PYTHON" ] && {{ echo ERROR: No python3 or python found. ; exit 1 ; }} ; '
116 '$PYTHON -c "import glob; print(glob.glob(\'{}\'))"'.format(path)]
117 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
118+ if six.PY3:
119+ output = output.decode('utf-8')
120 return ast.literal_eval(output)
121
122 def _run_remote_command(self, unit, command, sanitized_path_list):
123 cmd = ['juju', 'run', '--unit', unit, "{} {}".format(command, " ".join(sanitized_path_list))]
124- return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
125+ output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
126+ if six.PY3:
127+ output = output.decode('utf-8')
128+ return output
129
130 def _gather_logs(self):
131 output = []
132
133=== modified file 'mojo/juju/status.py'
134--- mojo/juju/status.py 2018-01-25 09:10:29 +0000
135+++ mojo/juju/status.py 2018-02-05 16:24:42 +0000
136@@ -42,7 +42,10 @@
137
138 cmd.extend(command)
139 try:
140- return subprocess.check_output(cmd)
141+ output = subprocess.check_output(cmd)
142+ if six.PY3:
143+ output = output.decode('utf-8')
144+ return output
145 except subprocess.CalledProcessError as cpe:
146 if command_timeout is not None:
147 # magic numbers here as per timeout(1)
148@@ -474,7 +477,11 @@
149 """ Parses the output of 'juju controllers' returning the controller version for the current controller.
150 Returns: The controller version reported by Juju
151 """
152- controllers = yaml.safe_load(subprocess.check_output(['juju', 'controllers', '--format', 'yaml']))
153+ output = subprocess.check_output(
154+ ['juju', 'controllers', '--format', 'yaml'])
155+ if six.PY3:
156+ output = output.decode('utf-8')
157+ controllers = yaml.safe_load(output)
158 current_controller = controllers['current-controller']
159 return controllers['controllers'][current_controller]['agent-version']
160
161
162=== modified file 'mojo/phase.py'
163--- mojo/phase.py 2018-01-29 11:51:05 +0000
164+++ mojo/phase.py 2018-02-05 16:24:42 +0000
165@@ -302,6 +302,8 @@
166 config = workspace.spec.get_config(self.config_base, stage)
167 cmd = [mojovol, '--config', config]
168 output = subprocess.check_output(cmd)
169+ if six.PY3:
170+ output = output.decode('utf-8')
171 logging.info(output)
172
173
174@@ -396,10 +398,14 @@
175 logging.error("Running exec-on-failure {}".format(exec_on_failure_script))
176 command = env_vars + [exec_on_failure_script]
177 try:
178- logging.error(subprocess.check_output(command, stderr=subprocess.STDOUT))
179+ output = subprocess.check_output(
180+ command, stderr=subprocess.STDOUT)
181+ if six.PY3:
182+ output = output.decode('utf-8')
183+ logging.error(output)
184 except subprocess.CalledProcessError as e:
185 logging.error("There was an error running exec-on-failure")
186- logging.error(e.output)
187+ logging.error(e)
188 except ConfigNotFoundException:
189 logging.debug("No exec-on-failure config found, see https://mojo.canonical.com/readme.html#exec-on-failure "
190 "for more details")
191@@ -718,6 +724,8 @@
192 subprocess.call(cmd)
193 else:
194 diff = subprocess.check_output(cmd)
195+ if six.PY3:
196+ diff = diff.decode('utf-8')
197 diff_processor(diff)
198
199 def run(self, project, workspace, stage, auto_secrets=True):
200@@ -821,6 +829,8 @@
201 failed_unit.replace("/", "-"),)]
202 with chdir(workspace.repo_dir):
203 failure = subprocess.check_output(cmd)
204+ if six.PY3:
205+ failure = failure.decode('utf-8')
206 logging.error(failure)
207 # Show juju status
208 juju_status = mojo.juju.Status(environment=juju_env)
209@@ -965,7 +975,7 @@
210 auto_secrets=auto_secrets, gather_debug_logs=False)
211 except ScriptPhaseException as e:
212 if i < retry:
213- logging.warning(e.output)
214+ logging.warning(e)
215 logging.warning(
216 "Error: verify script exited with status {} "
217 "(attempt {} of {}); sleeping for {}s".format(

Subscribers

People subscribed via source and target branches