aboutsummaryrefslogtreecommitdiffstats
path: root/brutha/util.py
blob: 723a374f40541867bd1f01fbf57e40713b6dbee4 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-

import os
import re


def escape(x):
    if '\'' not in x:
        return '\'' + x + '\''
    s = '"'
    for c in x:
        if c in '\\$"`':
            s = s + '\\'
        s = s + c
    s = s + '"'
    return s


def uprint(stream):
    def p(text='', end='\n'):
        stream.write(text + end)
    return p


def find_executable(name, names=None):
    envname = '%s_EXECUTABLE' % name.upper()
    if names is None:
        names = [name]
    if os.getenv(envname):
        return os.getenv(envname)
    paths = os.getenv('PATH', os.defpath).split(os.pathsep)
    exts = os.getenv('PATHEXT', os.pathsep).split(os.pathsep)
    for name in names:
        for path in paths:
            for ext in exts:
                fpath = os.path.join(path, name) + ext
                if os.path.exists(fpath) and os.access(fpath, os.X_OK):
                    return fpath


def require_executable(name, names=None):
    e = find_executable(name, names)
    if e:
        return e
    raise Exception('Could not find executable: %s' % name)


def detect_cores():
    try:
        import numpy.distutils.cpuinfo
        return numpy.distutils.cpuinfo.cpuinfo()._getNCPUs()
    except ImportError:
        try:
            with open('/proc/cpuinfo') as f:
                return len(re.findall('^processor\s+:\s+(\d+)$', f.read(), re.MULTILINE))
        except IOError:
            pass


def default_output(cores=None):
    if cores and cores > 1:
        if find_executable('make', ['gmake', 'make']):
            return 'make'
        if find_executable('parallel'):
            return 'parallel'
    return 'sh'