summaryrefslogtreecommitdiff
path: root/cloudinit/analyze
diff options
context:
space:
mode:
authorBrett Holman <brett.holman@canonical.com>2022-07-13 13:05:46 -0600
committerGitHub <noreply@github.com>2022-07-13 14:05:46 -0500
commit9cbd94dd57112083856ead0e0ff724e9d1c1f714 (patch)
tree765e2001636bc233b775fb949699121ffdbaec6e /cloudinit/analyze
parentb1a1a59a36a2580b2ebcb12e16c1411773548e3a (diff)
downloadcloud-init-git-9cbd94dd57112083856ead0e0ff724e9d1c1f714.tar.gz
Resource leak cleanup (#1556)
Add tox target for tracing for resource leaks, fix some leaks
Diffstat (limited to 'cloudinit/analyze')
-rw-r--r--cloudinit/analyze/__main__.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/cloudinit/analyze/__main__.py b/cloudinit/analyze/__main__.py
index 36a5be78..df08d46c 100644
--- a/cloudinit/analyze/__main__.py
+++ b/cloudinit/analyze/__main__.py
@@ -6,6 +6,7 @@ import argparse
import re
import sys
from datetime import datetime
+from typing import IO
from cloudinit.util import json_dumps
@@ -192,6 +193,7 @@ def analyze_boot(name, args):
}
outfh.write(status_map[status_code].format(**kwargs))
+ clean_io(infh, outfh)
return status_code
@@ -218,6 +220,7 @@ def analyze_blame(name, args):
outfh.write("\n".join(srecs) + "\n")
outfh.write("\n")
outfh.write("%d boot records analyzed\n" % (idx + 1))
+ clean_io(infh, outfh)
def analyze_show(name, args):
@@ -254,12 +257,14 @@ def analyze_show(name, args):
)
outfh.write("\n".join(record) + "\n")
outfh.write("%d boot records analyzed\n" % (idx + 1))
+ clean_io(infh, outfh)
def analyze_dump(name, args):
"""Dump cloud-init events in json format"""
(infh, outfh) = configure_io(args)
outfh.write(json_dumps(_get_events(infh)) + "\n")
+ clean_io(infh, outfh)
def _get_events(infile):
@@ -293,6 +298,14 @@ def configure_io(args):
return (infh, outfh)
+def clean_io(*file_handles: IO) -> None:
+ """close filehandles"""
+ for file_handle in file_handles:
+ if file_handle in (sys.stdin, sys.stdout):
+ continue
+ file_handle.close()
+
+
if __name__ == "__main__":
parser = get_parser()
args = parser.parse_args()