Merge lp:~fo0bar/turku/turku-storage-log-output into lp:turku

Proposed by Ryan Finnie
Status: Merged
Approved by: Barry Price
Approved revision: 40
Merged at revision: 39
Proposed branch: lp:~fo0bar/turku/turku-storage-log-output
Merge into: lp:turku
Diff against target: 46 lines (+14/-21)
1 file modified
turku_storage/ping.py (+14/-21)
To merge this branch: bzr merge lp:~fo0bar/turku/turku-storage-log-output
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+386152@code.launchpad.net

This proposal supersedes a proposal from 2020-06-20.

Commit message

Log run output directly to logger

Description of the change

Python 2.7 made real-time logging of subprocess piped output difficult
(and actually a bit dangerous). But now that Turku is Python 3, Popen's
context manager will DTRT and clean up after itself, so we don't need an
intermediary.

Note that this removes return_output from run_logging(), but
return_output was not actually used.

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : Posted in a previous version of this proposal

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Stuart Bishop (stub) wrote :

Looks good. I hadn't noticed that Popen objects could be used as context managers, but see it documented since 3.2 and implementing behavior we need.

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 39

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'turku_storage/ping.py'
2--- turku_storage/ping.py 2020-06-21 22:41:21 +0000
3+++ turku_storage/ping.py 2020-06-21 22:41:21 +0000
4@@ -60,28 +60,21 @@
5 self.lh_master.setLevel(logging.DEBUG)
6 self.logger.addHandler(self.lh_master)
7
8- def run_logging(
9- self, args, loglevel=logging.DEBUG, cwd=None, env=None, return_output=False
10- ):
11+ def run_logging(self, args, loglevel=logging.DEBUG, cwd=None, env=None):
12 self.logger.log(loglevel, "Running: %s" % repr(args))
13- t = tempfile.NamedTemporaryFile(mode="w+", encoding="UTF-8")
14- self.logger.log(
15- loglevel, "(Command output is in %s until written here at the end)" % t.name
16- )
17- returncode = subprocess.call(args, cwd=cwd, env=env, stdout=t, stderr=t)
18- t.flush()
19- t.seek(0)
20- out = ""
21- for line in t.readlines():
22- if return_output:
23- out = out + line
24- self.logger.log(loglevel, line.rstrip("\n"))
25- t.close()
26- self.logger.log(loglevel, "Return code: %d" % returncode)
27- if return_output:
28- return (returncode, out)
29- else:
30- return returncode
31+ with subprocess.Popen(
32+ args,
33+ cwd=cwd,
34+ env=env,
35+ encoding="UTF-8",
36+ stdout=subprocess.PIPE,
37+ stderr=subprocess.STDOUT,
38+ ) as proc:
39+ with proc.stdout as stdout:
40+ for line in iter(stdout.readline, ""):
41+ self.logger.log(loglevel, line.rstrip("\n"))
42+ self.logger.log(loglevel, "Return code: %d" % proc.returncode)
43+ return proc.returncode
44
45 def process_ping(self):
46 jsonin = ""

Subscribers

People subscribed via source and target branches

to all changes: