| 1 |
|
| 2 |
""" Some support code |
| 3 |
""" |
| 4 |
|
| 5 |
import re, sys, os, subprocess |
| 6 |
|
| 7 |
def detect_number_of_processors(filename_or_file='/proc/cpuinfo'): |
| 8 |
if os.environ.get('MAKEFLAGS'): |
| 9 |
return 1 |
| 10 |
if sys.platform == 'darwin': |
| 11 |
return sysctl_get_cpu_count('/usr/sbin/sysctl') |
| 12 |
elif sys.platform.startswith('freebsd'): |
| 13 |
return sysctl_get_cpu_count('/sbin/sysctl') |
| 14 |
elif not sys.platform.startswith('linux'): |
| 15 |
return 1 |
| 16 |
try: |
| 17 |
if isinstance(filename_or_file, str): |
| 18 |
f = open(filename_or_file, "r") |
| 19 |
else: |
| 20 |
f = filename_or_file |
| 21 |
count = max([int(re.split('processor[^\d]*(\d+)', line)[1]) |
| 22 |
for line in f.readlines() |
| 23 |
if line.startswith('processor')]) + 1 |
| 24 |
if count >= 4: |
| 25 |
return max(count // 2, 3) |
| 26 |
else: |
| 27 |
return count |
| 28 |
except: |
| 29 |
return 1 |
| 30 |
|
| 31 |
def sysctl_get_cpu_count(cmd, name='hw.ncpu'): |
| 32 |
try: |
| 33 |
proc = subprocess.Popen([cmd, '-n', name], stdout=subprocess.PIPE) |
| 34 |
count = proc.communicate()[0] |
| 35 |
return int(count) |
| 36 |
except (OSError, ValueError): |
| 37 |
return 1 |
| 38 |
|
| 39 |
def detect_pax(): |
| 40 |
""" |
| 41 |
Function to determine if your system comes with PAX protection. |
| 42 |
""" |
| 43 |
if sys.platform.startswith('linux'): |
| 44 |
|
| 45 |
with open("/proc/self/status") as fd: |
| 46 |
data = fd.read() |
| 47 |
if 'PaX' in data: |
| 48 |
return True |
| 49 |
return False |