grim/pyparser

Parents a0850d855c0d
Children 3718eba526ce
Implemented a parser for /proc/cpuinfo and wrote a unittest around it
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/data/cpuinfo.txt Sun Jun 23 23:57:51 2013 -0500
@@ -0,0 +1,162 @@
+processor : 0
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 800.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 0
+cpu cores : 6
+apicid : 0
+initial apicid : 0
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6645.07
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
+processor : 1
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 3300.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 1
+cpu cores : 6
+apicid : 1
+initial apicid : 1
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6644.79
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
+processor : 2
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 800.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 5
+cpu cores : 6
+apicid : 2
+initial apicid : 5
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6644.76
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
+processor : 3
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 800.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 2
+cpu cores : 6
+apicid : 3
+initial apicid : 2
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6644.74
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
+processor : 4
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 800.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 3
+cpu cores : 6
+apicid : 4
+initial apicid : 3
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6644.82
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
+processor : 5
+vendor_id : AuthenticAMD
+cpu family : 16
+model : 10
+model name : AMD Phenom(tm) II X6 1100T Processor
+stepping : 0
+microcode : 0x10000bf
+cpu MHz : 800.000
+cache size : 512 KB
+physical id : 0
+siblings : 6
+core id : 4
+cpu cores : 6
+apicid : 5
+initial apicid : 4
+fpu : yes
+fpu_exception : yes
+cpuid level : 6
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid aperfmperf pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt cpb hw_pstate npt lbrv svm_lock nrip_save pausefilter
+bogomips : 6644.79
+TLB size : 1024 4K pages
+clflush size : 64
+cache_alignment : 64
+address sizes : 48 bits physical, 48 bits virtual
+power management: ts ttp tm stc 100mhzsteps hwpstate [9]
+
--- a/tests/testregex.py Sun Jun 23 16:43:27 2013 -0500
+++ b/tests/testregex.py Sun Jun 23 23:57:51 2013 -0500
@@ -4,7 +4,7 @@
from pyparser.regex import RegexParser, Pattern
-
+import utils
class InvalidRegexPatternTest(unittest.TestCase): # pylint:disable-msg=R0904
@@ -41,3 +41,50 @@
InvalidRegexPatternTest.InvalidRegexParser(True)
+
+class CPUInfoParser(RegexParser):
+ """ Parsers /proc/cpuinfo """
+
+ def __init__(self):
+ RegexParser.__init__(self, kwargs=True)
+
+ self.data = {}
+ self.processor = None
+
+ @Pattern('^(?P<attribute>.+[^\s])\s+:\s+(?P<value>.+)$')
+ def attribute(self, match, attribute, value): # pylint:disable-msg=W0613
+ """ Parsers an attribute """
+
+ if attribute == 'processor':
+ self.processor = {}
+ self.data[value] = self.processor
+ else:
+ if self.processor is not None:
+ self.processor[attribute] = value
+
+
+class TestRegexParser(unittest.TestCase): # pylint:disable-msg=R0904
+ """ Tests the regex parser """
+
+ def test_cpu_info_parser(self):
+ """ Test parsing /proc/cpuinfo """
+
+ data = utils.parse_file(CPUInfoParser, 'cpuinfo.txt')
+
+ self.assertEqual(len(data), 6)
+
+ compare = None
+ for processor in data.values():
+ # remove values that are volatile
+ del processor['initial apicid']
+ del processor['apicid']
+ del processor['cpu MHz']
+ del processor['bogomips']
+ del processor['core id']
+
+ if compare is None:
+ compare = processor
+ continue
+
+ self.assertDictEqual(processor, compare)
+