Merged r6224:6268 from features/deb into trunk. Implements most of a Debian package...
[debian/gnuradio] / dtools / bin / extract_install_filenames
1 #!/usr/bin/env python
2
3 """
4 Example usage:
5
6   $ extract_install_filenames gnuradio-core/src/lib/swig/Makefile grgrpython_PYTHON
7
8 Produces:
9
10   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_python.py
11   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_runtime.py
12   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_general.py
13   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_gengen.py
14   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_filter.py
15   usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_io.py
16
17 """
18
19 from optparse import OptionParser
20 import re
21 import sys
22 import tempfile
23 import os
24
25 def make_makefile_tail(dirname, full_var_name):
26     
27     s = '''
28 extract_install_filenames:
29         @echo $(%s)
30         @echo $(%s)
31
32 ''' % (dirname, full_var_name)
33     return s
34
35
36 def main():
37     parser = OptionParser(usage="usage: %prog [options] Makefile AM-variable-name")
38     (options, args) = parser.parse_args()
39     if len(args) != 2:
40         parser.print_help()
41         raise SystemExit
42
43     makefile_name = args[0]
44     makefile = open(makefile_name, 'r')
45     full_var_name = args[1]
46
47     L = re.split('_', full_var_name)
48     prefix = '_'.join(L[:-1])
49     suffix = L[-1]
50
51     #print "prefix= ", prefix
52     #print "suffix= ", suffix
53
54     if suffix.upper() != suffix:
55         raise SystemExit, "AM-variable-name is malformed.  Expected something like grgrpython_PYTHON"
56
57     dirname = prefix + "dir"
58
59     tail = make_makefile_tail(dirname, full_var_name)
60
61     tmp_makefile = tempfile.NamedTemporaryFile()
62     #print "tmp_makefile =", tmp_makefile
63     tmp_name = tmp_makefile.name
64     #print "tmp_name =", tmp_name
65     s = makefile.read()
66     tmp_makefile.write(s)
67     tmp_makefile.write(tail)
68     tmp_makefile.flush()
69
70     (head, tail) = os.path.split(makefile_name)
71     if head:
72         # cd to directory that contained the original Makefile
73         cmd = 'cd %s; make -s -f %s extract_install_filenames' % (head, tmp_name)
74     else:
75         cmd = 'make -s -f %s extract_install_filenames' % (tmp_name,)
76         
77     #print "cmd =", cmd
78     make = os.popen(cmd, 'r')
79     target_dirname = make.readline().rstrip()
80     target_files = make.readline().rstrip()
81     if target_dirname.startswith('/'):
82         target_dirname = target_dirname[1:]
83     
84     #print "target_dirname =", target_dirname
85     #print "target_files =", target_files
86
87     for f in target_files.split():
88         sys.stdout.write(os.path.join(target_dirname, f) + '\n')
89
90 if __name__ == '__main__':
91   main()
92
93
94