diff options
Diffstat (limited to 'common.py')
-rw-r--r-- | common.py | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -1,5 +1,6 @@ | |||
1 | import os | 1 | import os |
2 | import re | 2 | import re |
3 | import stat | ||
3 | import subprocess | 4 | import subprocess |
4 | import sys | 5 | import sys |
5 | 6 | ||
@@ -134,3 +135,53 @@ def load_params(fname): | |||
134 | raise IOError("Invalid param file: %s\n%s" % (fname, e)) | 135 | raise IOError("Invalid param file: %s\n%s" % (fname, e)) |
135 | 136 | ||
136 | return params | 137 | return params |
138 | |||
139 | |||
140 | def num_cpus(): | ||
141 | '''Return the number of CPUs in the system.''' | ||
142 | |||
143 | lnx_re = re.compile(r'^(processor|online)') | ||
144 | cpus = 0 | ||
145 | |||
146 | with open('/proc/cpuinfo', 'r') as f: | ||
147 | for line in f: | ||
148 | if lnx_re.match(line): | ||
149 | cpus += 1 | ||
150 | return cpus | ||
151 | |||
152 | def ft_freq(): | ||
153 | umachine = subprocess.check_output(["uname", "-m"]) | ||
154 | |||
155 | if re.match("armv7", umachine): | ||
156 | # Arm V7s use a millisecond timer | ||
157 | freq = 1000.0 | ||
158 | elif re.match("x86", umachine): | ||
159 | # X86 timer is equal to processor clock | ||
160 | reg = re.compile(r'^cpu MHz\s*:\s*(?P<FREQ>\d+)', re.M) | ||
161 | with open('/proc/cpuinfo', 'r') as f: | ||
162 | data = f.read() | ||
163 | |||
164 | match = re.search(reg, data) | ||
165 | if not match: | ||
166 | raise Exception("Cannot parse CPU frequency from x86 CPU!") | ||
167 | freq = int(match.group('FREQ')) | ||
168 | else: | ||
169 | # You're on your own | ||
170 | freq = 0 | ||
171 | return freq | ||
172 | |||
173 | |||
174 | def uname_matches(reg): | ||
175 | data = subprocess.check_output(["uname", "-r"]) | ||
176 | return bool( re.match(reg, data) ) | ||
177 | |||
178 | def is_executable(fname): | ||
179 | '''Return whether the file passed in is executable''' | ||
180 | mode = os.stat(fname)[stat.ST_MODE] | ||
181 | return mode & stat.S_IXUSR and mode & stat.S_IRUSR | ||
182 | |||
183 | def is_device(dev): | ||
184 | if not os.path.exists(dev): | ||
185 | return False | ||
186 | mode = os.stat(dev)[stat.ST_MODE] | ||
187 | return not (not mode & stat.S_IFCHR) | ||