summaryrefslogtreecommitdiff
path: root/dtools/bin/extract_install_filenames
blob: 70465b44d6de1ae1af2bf842a3e60750fba009e2 (plain)
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
#!/usr/bin/env python

"""
Example usage:

  $ extract_install_filenames gnuradio-core/src/lib/swig/Makefile grgrpython_PYTHON

Produces:

  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_python.py
  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_runtime.py
  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_general.py
  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_gengen.py
  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_filter.py
  usr/local/lib64/python2.4/site-packages/gnuradio/gr/gnuradio_swig_py_io.py

"""

from optparse import OptionParser
import re
import sys
import tempfile
import os

def make_makefile_tail(dirname, full_var_name):
    
    s = '''
extract_install_filenames:
	@echo $(%s)
	@echo $(%s)

''' % (dirname, full_var_name)
    return s


def main():
    parser = OptionParser(usage="usage: %prog [options] Makefile AM-variable-name")
    (options, args) = parser.parse_args()
    if len(args) != 2:
        parser.print_help()
        raise SystemExit

    makefile_name = args[0]
    makefile = open(makefile_name, 'r')
    full_var_name = args[1]

    L = re.split('_', full_var_name)
    prefix = '_'.join(L[:-1])
    suffix = L[-1]

    #print "prefix= ", prefix
    #print "suffix= ", suffix

    if suffix.upper() != suffix:
        raise SystemExit, "AM-variable-name is malformed.  Expected something like grgrpython_PYTHON"

    if prefix[:5] == "dist_":
        dirname = prefix[5:] + "dir"
    else:
        dirname = prefix + "dir"
    #print "dirname =", dirname

    tail = make_makefile_tail(dirname, full_var_name)

    tmp_makefile = tempfile.NamedTemporaryFile()
    #print "tmp_makefile =", tmp_makefile
    tmp_name = tmp_makefile.name
    #print "tmp_name =", tmp_name
    s = makefile.read()
    tmp_makefile.write(s)
    tmp_makefile.write(tail)
    tmp_makefile.flush()

    (head, tail) = os.path.split(makefile_name)
    if head:
        # cd to directory that contained the original Makefile
        cmd = 'cd %s; make -s -f %s extract_install_filenames' % (head, tmp_name)
    else:
        cmd = 'make -s -f %s extract_install_filenames' % (tmp_name,)
        
    #print "cmd =", cmd
    make = os.popen(cmd, 'r')
    target_dirname = make.readline().rstrip()
    target_files = make.readline().rstrip()
    if target_dirname.startswith('/'):
        target_dirname = target_dirname[1:]
    
    #print "target_dirname =", target_dirname
    #print "target_files =", target_files

    for f in target_files.split():
        sys.stdout.write(os.path.join(target_dirname, f) + '\n')

if __name__ == '__main__':
  main()