add Vcs entries to control file
[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     if prefix[:5] == "dist_":
58         dirname = prefix[5:] + "dir"
59     else:
60         dirname = prefix + "dir"
61     #print "dirname =", dirname
62
63     tail = make_makefile_tail(dirname, full_var_name)
64
65     tmp_makefile = tempfile.NamedTemporaryFile()
66     #print "tmp_makefile =", tmp_makefile
67     tmp_name = tmp_makefile.name
68     #print "tmp_name =", tmp_name
69     s = makefile.read()
70     tmp_makefile.write(s)
71     tmp_makefile.write(tail)
72     tmp_makefile.flush()
73
74     (head, tail) = os.path.split(makefile_name)
75     if head:
76         # cd to directory that contained the original Makefile
77         cmd = 'cd %s; make -s -f %s extract_install_filenames' % (head, tmp_name)
78     else:
79         cmd = 'make -s -f %s extract_install_filenames' % (tmp_name,)
80         
81     #print "cmd =", cmd
82     make = os.popen(cmd, 'r')
83     target_dirname = make.readline().rstrip()
84     target_files = make.readline().rstrip()
85     if target_dirname.startswith('/'):
86         target_dirname = target_dirname[1:]
87    
88     #print "target_dirname =", target_dirname
89     #print "target_files =", target_files
90
91     for f in target_files.split():
92         sys.stdout.write(os.path.join(target_dirname, f.split('/')[-1]) + '\n')
93
94 if __name__ == '__main__':
95   main()
96
97
98