File: support.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (49 lines) | stat: -rw-r--r-- 1,616 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

""" Some support code
"""

import re, sys, os, subprocess

def detect_number_of_processors(filename_or_file='/proc/cpuinfo'):
    if os.environ.get('MAKEFLAGS'):
        return 1    # don't override MAKEFLAGS.  This will call 'make' without any '-j' option
    if sys.platform == 'darwin':
        return sysctl_get_cpu_count('/usr/sbin/sysctl')
    elif sys.platform.startswith('freebsd'):
        return sysctl_get_cpu_count('/sbin/sysctl')
    elif not sys.platform.startswith('linux'):
        return 1    # implement me
    try:
        if isinstance(filename_or_file, str):
            f = open(filename_or_file, "r")
        else:
            f = filename_or_file
        count = max([int(re.split('processor[^\d]*(\d+)', line)[1])
                    for line in f.readlines()
                    if line.startswith('processor')]) + 1
        if count >= 4:
            return max(count // 2, 3)
        else:
            return count
    except:
        return 1 # we really don't want to explode here, at worst we have 1

def sysctl_get_cpu_count(cmd, name='hw.ncpu'):
    try:
        proc = subprocess.Popen([cmd, '-n', name], stdout=subprocess.PIPE)
        count = proc.communicate()[0]
        return int(count)
    except (OSError, ValueError):
        return 1

def detect_pax():
    """
    Function to determine if your system comes with PAX protection.
    """
    if sys.platform.startswith('linux'):
        # use PID of current process for the check
        with open("/proc/self/status") as fd:
            data = fd.read()
        if 'PaX' in data:
            return True
    return False