pypy / pypy / config / support.py

""" 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 darwin_get_cpu_count()
    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+)', 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 darwin_get_cpu_count(cmd = "/usr/sbin/sysctl hw.ncpu"):
    try:
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
        # 'hw.ncpu: 20'
        count = proc.communicate()[0].rstrip()[8:]
        return int(count)
    except (OSError, ValueError):
        return 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.