huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
67
68
69
70
import os
import glob
 
"""
One day, when this module grows up, it might actually try to fix things.
'apt-cache search | apt-get install' ... or a less terrifying version of
the same. With added distro-independant pixie dust.
"""
 
def command(cmd):
    # this could use '/usr/bin/which', I suppose. But this seems simpler
    for dir in os.environ['PATH'].split(':'):
        file = os.path.join(os.path.expandvars(os.path.expanduser(dir)), cmd)
        if os.path.exists(file):
            return file
    raise ValueError('Missing command: %s' % cmd)
 
 
def commands(*cmds):
    results = []
    for cmd in cmds:
        results.append(command(cmd))
 
 
def library(lib):
    lddirs = []
    # read lddirs from  main ld.so.conf file
    for line in open('/etc/ld.so.conf', 'r').readlines():
        if line.startswith('include '):
            glob_pattern = line.split('include ')[1]
            if not os.path.isabs(glob_pattern):
                # prepend with a base path of '/etc'
                glob_pattern = os.path.join('/etc', glob_pattern)
            glob_result = glob.glob(glob_pattern)
            for conf_file in glob_result:
                for conf_file_line in open(conf_file, 'r').readlines():
                    if os.path.isdir(conf_file_line):
                        lddirs.append(conf_file_line)
        else:
            if os.path.isdir(line):
                lddirs.append(line.strip())
 
    lddirs = set(lddirs)
    lddirs = list(lddirs)
 
    for dir in ['/lib', '/usr/lib', '/lib64', '/usr/lib64'] + lddirs:
        file = os.path.join(dir, lib)
        if os.path.exists(file):
            return file
    raise ValueError('Missing library: %s' % lib)
 
 
def libraries(*libs):
    results = []
    for lib in libs:
        results.append(library(lib))
 
 
def header(hdr):
    for dir in ['/usr/include', '/usr/local/include']:
        file = os.path.join(dir, hdr)
        if os.path.exists(file):
            return file
    raise ValueError('Missing header: %s' % hdr)
 
 
def headers(*hdrs):
    results = []
    for hdr in hdrs:
        results.append(header(hdr))