diff options
| author | Daniel Manrique <roadmr@ubuntu.com> | 2014-08-11 21:57:51 +0000 |
|---|---|---|
| committer | Daniel Manrique <> | 2014-08-11 21:57:51 +0000 |
| commit | 22b6ce9a102e94a32f5d5d341b1fd6d34c7efbe0 (patch) | |
| tree | ebb2d342f6126905a7d6b4383453695061934c1c /bin | |
| parent | efedf7f324c7e1161cf6101f8b3b6f62b612b68c (diff) | |
| parent | 6a2eb6d9eefafe55dbe45c46cb7354c967362d36 (diff) | |
"providers:checkbox: Remove unused THRESHOLD constant from memory_compare, PEP8 fixes in memory_compare, Added proper copyright header comment
[r=bladernr][bug=1355408][author=roadmr]"
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/memory_compare | 60 |
1 files changed, 48 insertions, 12 deletions
diff --git a/bin/memory_compare b/bin/memory_compare index 24d6c84..4143673 100755 --- a/bin/memory_compare +++ b/bin/memory_compare @@ -1,13 +1,34 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# This file is part of Checkbox. +# +# Copyright 2014 Canonical Ltd. +# +# Authors: +# Brendan Donegan <brendan.donegan@canonical.com> +# Jeff Lane <jeff.lane@canonical.com> +# Sylvain Pineau <sylvain.pineau@canonical.com> +# +# Checkbox is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# Checkbox is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. import os import sys +from subprocess import check_output, PIPE from checkbox_support.parsers.lshwjson import LshwJsonParser from checkbox_support.parsers.meminfo import MeminfoParser -from subprocess import check_output, PIPE -THRESHOLD = 25 class LshwJsonResult: @@ -20,8 +41,9 @@ class LshwJsonResult: elif 'bank' in hardware['id']: self.banks_reported += int(hardware.get('size', 0)) + def get_installed_memory_size(): - lshw = LshwJsonParser(check_output(['lshw','-json'], + lshw = LshwJsonParser(check_output(['lshw', '-json'], universal_newlines=True, stderr=PIPE)) result = LshwJsonResult() @@ -32,6 +54,7 @@ def get_installed_memory_size(): else: return result.banks_reported + class MeminfoResult: memtotal = 0 @@ -39,6 +62,7 @@ class MeminfoResult: def setMemory(self, memory): self.memtotal = memory['total'] + def get_visible_memory_size(): parser = MeminfoParser(open('/proc/meminfo')) result = MeminfoResult() @@ -46,6 +70,7 @@ def get_visible_memory_size(): return result.memtotal + def get_threshold(installed_memory): GB = 1024**3 if installed_memory <= 2 * GB: @@ -55,6 +80,7 @@ def get_threshold(installed_memory): else: return 10 + def main(): if os.geteuid() != 0: print("This script must be run as root.", file=sys.stderr) @@ -69,22 +95,32 @@ def main(): percentage = difference / installed_memory * 100 except ZeroDivisionError: print("Results:") - print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), file=sys.stderr) - print("\tlshw reports:\t%s kB" % (installed_memory / 1024), file=sys.stderr) - print("\nFAIL: Either lshw or /proc/meminfo returned a memory size of 0 kB", file=sys.stderr) + print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), + file=sys.stderr) + print("\tlshw reports:\t%s kB" % (installed_memory / 1024), + file=sys.stderr) + print("\nFAIL: Either lshw or /proc/meminfo returned a memory size " + "of 0 kB", file=sys.stderr) return 1 - + if percentage <= threshold: print("Results:") print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024)) print("\tlshw reports:\t%s kB" % (installed_memory / 1024)) - print("\nPASS: Meminfo reports %d bytes less than lshw, a difference of %.2f%%. This is less than the %d%% variance allowed." % (difference, percentage, threshold)) + print("\nPASS: Meminfo reports %d bytes less than lshw, a " + "difference of %.2f%%. This is less than the " + "%d%% variance allowed." % (difference, percentage, threshold)) return 0 else: - print("Results") - print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), file=sys.stderr) - print("\tlshw reports:\t%s kB" % (installed_memory / 1024), file=sys.stderr) - print("\nFAIL: Meminfo reports %d bytes less than lshw, a difference of %.2f%%. Only a variance of %d%% in reported memory is allowed." % (difference, percentage, threshold), file=sys.stderr) + print("Results:", file=sys.stderr) + print("\t/proc/meminfo reports:\t%s kB" % (visible_memory / 1024), + file=sys.stderr) + print("\tlshw reports:\t%s kB" % (installed_memory / 1024), + file=sys.stderr) + print("\nFAIL: Meminfo reports %d bytes less than lshw, " + "a difference of %.2f%%. Only a variance of %d%% in " + "reported memory is allowed." % + (difference, percentage, threshold), file=sys.stderr) return 1 if __name__ == "__main__": |
