2 import os
, shutil
, re
, sys
3 from xml
.dom
import minidom
, XMLNS_NAMESPACE
4 from optparse
import OptionParser
6 XMLNS_IFACE
= "http://zero-install.sourceforge.net/2004/injector/interface"
7 XMLNS_0COMPILE
= 'http://zero-install.sourceforge.net/2006/namespaces/0compile'
10 parser
= OptionParser(usage
="usage: %prog [options]")
11 parser
.add_option("-k", "--keep", help="keep a file we would normally delete", action
='append')
12 parser
.add_option("-V", "--version", help="display version information", action
='store_true')
13 (options
, args
) = parser
.parse_args()
16 print "Make-headers (zero-install) " + version
17 print "Copyright (C) 2007 Thomas Leonard"
18 print "This program comes with ABSOLUTELY NO WARRANTY,"
19 print "to the extent permitted by law."
20 print "You may redistribute copies of this program"
21 print "under the terms of the GNU General Public License."
22 print "For more information about these matters, see the file named COPYING."
25 configure
= os
.path
.join(os
.environ
['SRCDIR'], 'configure')
26 prefix
= os
.environ
['DISTDIR']
29 if os
.spawnvp(os
.P_WAIT
, argv
[0], argv
):
30 raise Exception("Command '%s' failed" % repr(argv
))
32 run(configure
, '--prefix', prefix
, *args
)
33 run('make', 'install')
35 keep
= options
.keep
or []
38 for path
in ['bin', 'man', 'share']:
39 if path
not in keep
and os
.path
.isdir(path
):
42 # TODO: get 0compile to tell us this instead of guessing
43 xml_files
= [x
for x
in os
.listdir('0install') if x
.endswith('.xml')]
44 xml_files
.remove('build-environment.xml')
45 assert len(xml_files
) == 1, xml_files
46 feed_file
= os
.path
.abspath(os
.path
.join('0install', xml_files
[0]))
51 for x
in os
.listdir('.'):
52 if os
.path
.isfile(x
) or os
.path
.islink(x
):
53 if re
.match('^lib.*\.so[.0-9]*$', x
) or \
54 re
.match('^lib.*[.0-9]*\.dylib$', x
) or \
55 re
.match('^lib.*\.l?a$', x
):
56 if x
.endswith('.so') and os
.path
.islink(x
):
57 target
= os
.readlink(x
)
58 mappings
[x
[3:-3]] = target
.split('.so.', 1)[1].split('.', 1)[0]
59 if x
.endswith('.dylib') and os
.path
.islink(x
):
60 target
= os
.readlink(x
)
61 mappings
[x
[3:-6]] = target
.split('.dylib.', 1)[0].split('.', 1)[0]
63 elif os
.path
.isdir(x
):
64 if re
.match('^python.*$', x
):
68 print "Detected library major versions:", mappings
69 doc
= minidom
.parse(feed_file
)
70 impls
= doc
.getElementsByTagNameNS(XMLNS_IFACE
, "implementation")
71 assert len(impls
) == 1, impls
73 # Find the innermost element with some existing mappings, if any
74 while not node
.hasAttributeNS(XMLNS_0COMPILE
, 'lib-mappings'):
75 if node
.parentNode
.nodeName
!= 'group':
77 node
= node
.parentNode
78 existing_mappings
= node
.getAttributeNS(XMLNS_0COMPILE
, 'lib-mappings')
80 print "Merging existing mappings", existing_mappings
81 node
.removeAttributeNS(XMLNS_0COMPILE
, 'lib-mappings')
82 for existing_mapping
in existing_mappings
.split(' '):
83 key
, value
= existing_mapping
.split(':')
85 if value
!= mappings
[key
]:
86 print "WARNING: overwriting detected mapping %s -> %s with %s!" % (key
, mappings
[key
], value
)
88 print "NOTE: unnecessary mapping given in feed (%s -> %s). I can work it out for myself!" % (key
, value
)
92 lib_mappings
.append("%s:%s" % (key
, mappings
[key
]))
93 node
.setAttributeNS(XMLNS_0COMPILE
, 'compile:lib-mappings', ' '.join(lib_mappings
))
94 doc
.documentElement
.setAttributeNS(XMLNS_NAMESPACE
, 'xmlns:compile', XMLNS_0COMPILE
)
95 stream
= file(feed_file
, 'w')
100 for pc
in os
.listdir('.'):
101 if pc
.endswith('.pc'):
102 data
= file(pc
).read().split('\n')
103 for i
in range(len(data
)):
104 if data
[i
].startswith('prefix='):
105 data
[i
] = 'prefix=${pcfiledir}/../..'
107 out
.write('\n'.join(data
))