File: config_unix.py

package info (click to toggle)
pyvorbis 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 304 kB
  • ctags: 268
  • sloc: ansic: 2,424; python: 530; sh: 335; makefile: 63
file content (199 lines) | stat: -rwxr-xr-x 5,089 bytes parent folder | download | duplicates (8)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python

import string
import os
import sys
import exceptions

log_name = 'config.log'
if os.path.isfile(log_name):
    os.unlink(log_name)

def write_log(msg):
    log_file = open(log_name, 'a')
    log_file.write(msg)
    log_file.write('\n')
    log_file.close()

def exit(code=0):
    sys.exit(code)

def msg_checking(msg):
    print "Checking", msg, "...",

def execute(cmd):
    write_log("Execute: %s" % cmd)
    full_cmd = '%s 1>>%s 2>&1' % (cmd, log_name)
    return os.system(full_cmd)

def run_test(input, flags = ''):
    try:
        f = open('_temp.c', 'w')
        f.write(input)
        f.close()
        compile_cmd = '%s -o _temp _temp.c %s' % (os.environ.get('CC', 'cc'),
                                                  flags)
        write_log("executing test: %s" % compile_cmd)
        if not execute(compile_cmd):
            execute('./_temp')
                
    finally:
        execute('rm -f _temp.c _temp')
    
ogg_test_program = '''
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>

int main ()
{
  system("touch conf.oggtest");
  return 0;
}
'''

def find_ogg(ogg_prefix = '/usr/local', enable_oggtest = 1):
    """A rough translation of ogg.m4"""

    ogg_include_dir = ogg_prefix + '/include'
    ogg_lib_dir = ogg_prefix + '/lib'
    ogg_libs = 'ogg'

    msg_checking('for Ogg')

    if enable_oggtest:
        execute('rm -f conf.oggtest')

        try:
            run_test(ogg_test_program, flags="-I" + ogg_include_dir)
            if not os.path.isfile('conf.oggtest'):
                raise RuntimeError, "Did not produce output"
            execute('rm conf.oggtest')
            
        except:
            print "test program failed"
            return None

    print "success"

    return {'ogg_libs' : ogg_libs,
            'ogg_lib_dir' : ogg_lib_dir,
            'ogg_include_dir' : ogg_include_dir}


vorbis_test_program = '''
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vorbis/codec.h>

int main ()
{
  system("touch conf.vorbistest");
  return 0;
}
'''

def find_vorbis(ogg_data,
                vorbis_prefix = '/usr/local',
                enable_vorbistest = 1):
    """A rough translation of vorbis.m4"""

    ogg_libs = ogg_data['ogg_libs']
    ogg_lib_dir = ogg_data['ogg_lib_dir']
    ogg_include_dir = ogg_data['ogg_include_dir']
    
    vorbis_include_dir = vorbis_prefix + '/include'
    vorbis_lib_dir = vorbis_prefix + '/lib'
    vorbis_libs = 'vorbis vorbisfile vorbisenc'

    msg_checking('for Vorbis')

    if enable_vorbistest:
        execute('rm -f conf.vorbistest')

        try:
            run_test(vorbis_test_program,
                     flags = "-I%s -I%s" % (vorbis_include_dir,
                                            ogg_include_dir))
            if not os.path.isfile('conf.vorbistest'):
                raise RuntimeError, "Did not produce output"
            execute('rm conf.vorbistest')
            
        except:
            print "test program failed"
            return None

    print "success"

    return {'vorbis_libs' : vorbis_libs,
            'vorbis_lib_dir' : vorbis_lib_dir,
            'vorbis_include_dir' : vorbis_include_dir}

def write_data(data):
    f = open('Setup', 'w')
    for item in data.items():
        f.write('%s = %s\n' % item)
    f.close()
    print "Wrote Setup file"
            
def print_help():
    print '''%s
    --prefix                  Give the prefix in which vorbis was installed.
    --with-ogg-dir [dir]      Give the directory for ogg files
                                (separated by a space)
    --with-vorbis-dir [dir]   Give the directory for vorbis files''' % sys.argv[0]
    exit()

def parse_args():
    def arg_check(data, argv, pos, arg_type, key):
        "Register an command line arg which takes an argument"
        if len(argv) == pos:
            print arg_type, "needs an argument"
            exit(1)
        data[key] = argv[pos]
        
    data = {}
    argv = sys.argv
    for pos in range(len(argv)):
        if argv[pos] == '--help':
            print_help()
        if argv[pos] == '--with-ogg-dir':
            pos = pos + 1
            arg_check(data, argv, pos, "Ogg dir", 'ogg_prefix')
        if argv[pos] == '--with-vorbis-dir':
            pos = pos + 1
            arg_check(data, argv, pos, "Vorbis dir", 'vorbis_prefix')
        if argv[pos] == '--prefix':
            pos = pos + 1
            arg_check(data, argv, pos, "Prefix", 'prefix')

    return data
    
def main():
    args = parse_args()
    prefix = args.get('prefix', '/usr/local')
    vorbis_prefix = args.get('vorbis_prefix', prefix)
    ogg_prefix = args.get('ogg_prefix', prefix)

    data = find_ogg(ogg_prefix = ogg_prefix)
    if not data:
        print "Config failure"
        exit(1)

    vorbis_data = find_vorbis(ogg_data = data,
                              vorbis_prefix = vorbis_prefix)
    if not vorbis_data:
        print "Config failure"
        exit(1)
    data.update(vorbis_data)
    
    write_data(data)

if __name__ == '__main__':
    main()