diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-05-07 13:55:58 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-05-09 14:06:50 +0100 |
commit | e0bd393be8e6419cd19dd860c75de75ea73c7509 (patch) | |
tree | 9dea09d3e232038ecc46aa78d71e6b35691776aa | |
parent | 2ac4f8b39711209ba9323fc221c1dcd185da57ea (diff) | |
download | poky-e0bd393be8e6419cd19dd860c75de75ea73c7509.tar.gz |
buildstats: Update to ensure files are closed
python3 doesn't like files being left open. This updates the code style
to ensure file are closed.
(From OE-Core rev: ec74285bd2108f12f33fc8ac0dc1d124ab48be21)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | meta/classes/buildstats.bbclass | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/meta/classes/buildstats.bbclass b/meta/classes/buildstats.bbclass index 6fd13a8602..453c50b968 100644 --- a/meta/classes/buildstats.bbclass +++ b/meta/classes/buildstats.bbclass | |||
@@ -12,12 +12,14 @@ DEVFILE = "${BUILDSTATS_BASE}/.device" | |||
12 | ################################################################################ | 12 | ################################################################################ |
13 | 13 | ||
14 | def get_process_cputime(pid): | 14 | def get_process_cputime(pid): |
15 | fields = open("/proc/%d/stat" % pid, "r").readline().rstrip().split() | 15 | with open("/proc/%d/stat" % pid, "r") as f: |
16 | fields = f.readline().rstrip().split() | ||
16 | # 13: utime, 14: stime, 15: cutime, 16: cstime | 17 | # 13: utime, 14: stime, 15: cutime, 16: cstime |
17 | return sum(int(field) for field in fields[13:16]) | 18 | return sum(int(field) for field in fields[13:16]) |
18 | 19 | ||
19 | def get_cputime(): | 20 | def get_cputime(): |
20 | fields = open("/proc/stat", "r").readline().rstrip().split()[1:] | 21 | with open("/proc/stat", "r") as f: |
22 | fields = f.readline().rstrip().split()[1:] | ||
21 | return sum(int(field) for field in fields) | 23 | return sum(int(field) for field in fields) |
22 | 24 | ||
23 | def set_bn(e): | 25 | def set_bn(e): |
@@ -26,14 +28,12 @@ def set_bn(e): | |||
26 | os.remove(e.data.getVar('BNFILE', True)) | 28 | os.remove(e.data.getVar('BNFILE', True)) |
27 | except: | 29 | except: |
28 | pass | 30 | pass |
29 | file = open(e.data.getVar('BNFILE', True), "w") | 31 | with open(e.data.getVar('BNFILE', True), "w") as f: |
30 | file.write(os.path.join(bn, e.data.getVar('BUILDNAME', True))) | 32 | f.write(os.path.join(bn, e.data.getVar('BUILDNAME', True))) |
31 | file.close() | ||
32 | 33 | ||
33 | def get_bn(e): | 34 | def get_bn(e): |
34 | file = open(e.data.getVar('BNFILE', True)) | 35 | with open(e.data.getVar('BNFILE', True)) as f: |
35 | bn = file.readline() | 36 | bn = f.readline() |
36 | file.close() | ||
37 | return bn | 37 | return bn |
38 | 38 | ||
39 | def set_device(e): | 39 | def set_device(e): |
@@ -63,9 +63,10 @@ def set_device(e): | |||
63 | ############################################################################ | 63 | ############################################################################ |
64 | rdev="NoLogicalDevice" | 64 | rdev="NoLogicalDevice" |
65 | try: | 65 | try: |
66 | for line in open("/proc/diskstats", "r"): | 66 | with open("/proc/diskstats", "r") as f: |
67 | if majordev == int(line.split()[0]) and minordev == int(line.split()[1]): | 67 | for line in f: |
68 | rdev=line.split()[2] | 68 | if majordev == int(line.split()[0]) and minordev == int(line.split()[1]): |
69 | rdev=line.split()[2] | ||
69 | except: | 70 | except: |
70 | pass | 71 | pass |
71 | file = open(e.data.getVar('DEVFILE', True), "w") | 72 | file = open(e.data.getVar('DEVFILE', True), "w") |
@@ -85,9 +86,10 @@ def get_diskstats(dev): | |||
85 | ############################################################################ | 86 | ############################################################################ |
86 | DSTAT_KEYS = ['ReadsComp', 'ReadsMerged', 'SectRead', 'TimeReads', 'WritesComp', 'SectWrite', 'TimeWrite', 'IOinProgress', 'TimeIO', 'WTimeIO'] | 87 | DSTAT_KEYS = ['ReadsComp', 'ReadsMerged', 'SectRead', 'TimeReads', 'WritesComp', 'SectWrite', 'TimeWrite', 'IOinProgress', 'TimeIO', 'WTimeIO'] |
87 | try: | 88 | try: |
88 | for x in open("/proc/diskstats", "r"): | 89 | with open("/proc/diskstats", "r") as f: |
89 | if dev in x: | 90 | for x in f: |
90 | diskstats_val = x.rstrip().split()[4:] | 91 | if dev in x: |
92 | diskstats_val = x.rstrip().split()[4:] | ||
91 | except IOError as e: | 93 | except IOError as e: |
92 | return | 94 | return |
93 | diskstats = dict(itertools.izip(DSTAT_KEYS, diskstats_val)) | 95 | diskstats = dict(itertools.izip(DSTAT_KEYS, diskstats_val)) |
@@ -274,7 +276,6 @@ python run_buildstats () { | |||
274 | file = open(build_status,"a") | 276 | file = open(build_status,"a") |
275 | file.write(e.data.expand("Failed at: ${PF} at task: %s \n" % e.task)) | 277 | file.write(e.data.expand("Failed at: ${PF} at task: %s \n" % e.task)) |
276 | file.close() | 278 | file.close() |
277 | |||
278 | } | 279 | } |
279 | 280 | ||
280 | addhandler run_buildstats | 281 | addhandler run_buildstats |