1import os, sys
2
3def detectCPUs():
4    """
5    Detects the number of CPUs on a system. Cribbed from pp.
6    """
7    # Linux, Unix and MacOS:
8    if hasattr(os, "sysconf"):
9        if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
10            # Linux & Unix:
11            ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
12            if isinstance(ncpus, int) and ncpus > 0:
13                return ncpus
14        else: # OSX:
15            return int(capture(['sysctl', '-n', 'hw.ncpu']))
16    # Windows:
17    if os.environ.has_key("NUMBER_OF_PROCESSORS"):
18        ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
19        if ncpus > 0:
20            return ncpus
21    return 1 # Default
22
23def mkdir_p(path):
24    """mkdir_p(path) - Make the "path" directory, if it does not exist; this
25    will also make directories for any missing parent directories."""
26    import errno
27
28    if not path or os.path.exists(path):
29        return
30
31    parent = os.path.dirname(path)
32    if parent != path:
33        mkdir_p(parent)
34
35    try:
36        os.mkdir(path)
37    except OSError,e:
38        # Ignore EEXIST, which may occur during a race condition.
39        if e.errno != errno.EEXIST:
40            raise
41
42def capture(args, env=None):
43    import subprocess
44    """capture(command) - Run the given command (or argv list) in a shell and
45    return the standard output."""
46    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
47                         env=env)
48    out,_ = p.communicate()
49    return out
50
51def which(command, paths = None):
52    """which(command, [paths]) - Look up the given command in the paths string
53    (or the PATH environment variable, if unspecified)."""
54
55    if paths is None:
56        paths = os.environ.get('PATH','')
57
58    # Check for absolute match first.
59    if os.path.isfile(command):
60        return command
61
62    # Would be nice if Python had a lib function for this.
63    if not paths:
64        paths = os.defpath
65
66    # Get suffixes to search.
67    # On Cygwin, 'PATHEXT' may exist but it should not be used.
68    if os.pathsep == ';':
69        pathext = os.environ.get('PATHEXT', '').split(';')
70    else:
71        pathext = ['']
72
73    # Search the paths...
74    for path in paths.split(os.pathsep):
75        for ext in pathext:
76            p = os.path.join(path, command + ext)
77            if os.path.exists(p):
78                return p
79
80    return None
81
82def checkToolsPath(dir, tools):
83    for tool in tools:
84        if not os.path.exists(os.path.join(dir, tool)):
85            return False;
86    return True;
87
88def whichTools(tools, paths):
89    for path in paths.split(os.pathsep):
90        if checkToolsPath(path, tools):
91            return path
92    return None
93
94def printHistogram(items, title = 'Items'):
95    import itertools, math
96
97    items.sort(key = lambda (_,v): v)
98
99    maxValue = max([v for _,v in items])
100
101    # Select first "nice" bar height that produces more than 10 bars.
102    power = int(math.ceil(math.log(maxValue, 10)))
103    for inc in itertools.cycle((5, 2, 2.5, 1)):
104        barH = inc * 10**power
105        N = int(math.ceil(maxValue / barH))
106        if N > 10:
107            break
108        elif inc == 1:
109            power -= 1
110
111    histo = [set() for i in range(N)]
112    for name,v in items:
113        bin = min(int(N * v/maxValue), N-1)
114        histo[bin].add(name)
115
116    barW = 40
117    hr = '-' * (barW + 34)
118    print '\nSlowest %s:' % title
119    print hr
120    for name,value in items[-20:]:
121        print '%.2fs: %s' % (value, name)
122    print '\n%s Times:' % title
123    print hr
124    pDigits = int(math.ceil(math.log(maxValue, 10)))
125    pfDigits = max(0, 3-pDigits)
126    if pfDigits:
127        pDigits += pfDigits + 1
128    cDigits = int(math.ceil(math.log(len(items), 10)))
129    print "[%s] :: [%s] :: [%s]" % ('Range'.center((pDigits+1)*2 + 3),
130                                    'Percentage'.center(barW),
131                                    'Count'.center(cDigits*2 + 1))
132    print hr
133    for i,row in enumerate(histo):
134        pct = float(len(row)) / len(items)
135        w = int(barW * pct)
136        print "[%*.*fs,%*.*fs)" % (pDigits, pfDigits, i*barH,
137                                   pDigits, pfDigits, (i+1)*barH),
138        print ":: [%s%s] :: [%*d/%*d]" % ('*'*w, ' '*(barW-w),
139                                          cDigits, len(row),
140                                          cDigits, len(items))
141
142