Include waf as an extracted source directory, rather than as a one-in-a-file script.
[samba.git] / buildtools / wafadmin / Tools / gnome.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2006-2008 (ita)
4
5 "Gnome support"
6
7 import os, re
8 import TaskGen, Utils, Runner, Task, Build, Options, Logs
9 import cc
10 from Logs import error
11 from TaskGen import taskgen, before, after, feature
12
13 n1_regexp = re.compile('<refentrytitle>(.*)</refentrytitle>', re.M)
14 n2_regexp = re.compile('<manvolnum>(.*)</manvolnum>', re.M)
15
16 def postinstall_schemas(prog_name):
17         if Build.bld.is_install:
18                 dir = Build.bld.get_install_path('${PREFIX}/etc/gconf/schemas/%s.schemas' % prog_name)
19                 if not Options.options.destdir:
20                         # add the gconf schema
21                         Utils.pprint('YELLOW', 'Installing GConf schema')
22                         command = 'gconftool-2 --install-schema-file=%s 1> /dev/null' % dir
23                         ret = Utils.exec_command(command)
24                 else:
25                         Utils.pprint('YELLOW', 'GConf schema not installed. After install, run this:')
26                         Utils.pprint('YELLOW', 'gconftool-2 --install-schema-file=%s' % dir)
27
28 def postinstall_icons():
29         dir = Build.bld.get_install_path('${DATADIR}/icons/hicolor')
30         if Build.bld.is_install:
31                 if not Options.options.destdir:
32                         # update the pixmap cache directory
33                         Utils.pprint('YELLOW', "Updating Gtk icon cache.")
34                         command = 'gtk-update-icon-cache -q -f -t %s' % dir
35                         ret = Utils.exec_command(command)
36                 else:
37                         Utils.pprint('YELLOW', 'Icon cache not updated. After install, run this:')
38                         Utils.pprint('YELLOW', 'gtk-update-icon-cache -q -f -t %s' % dir)
39
40 def postinstall_scrollkeeper(prog_name):
41         if Build.bld.is_install:
42                 # now the scrollkeeper update if we can write to the log file
43                 if os.access('/var/log/scrollkeeper.log', os.W_OK):
44                         dir1 = Build.bld.get_install_path('${PREFIX}/var/scrollkeeper')
45                         dir2 = Build.bld.get_install_path('${DATADIR}/omf/%s' % prog_name)
46                         command = 'scrollkeeper-update -q -p %s -o %s' % (dir1, dir2)
47                         ret = Utils.exec_command(command)
48
49 def postinstall(prog_name='myapp', schemas=1, icons=1, scrollkeeper=1):
50         if schemas: postinstall_schemas(prog_name)
51         if icons: postinstall_icons()
52         if scrollkeeper: postinstall_scrollkeeper(prog_name)
53
54 # OBSOLETE
55 class gnome_doc_taskgen(TaskGen.task_gen):
56         def __init__(self, *k, **kw):
57                 TaskGen.task_gen.__init__(self, *k, **kw)
58
59 @feature('gnome_doc')
60 def init_gnome_doc(self):
61         self.default_install_path = '${PREFIX}/share'
62
63 @feature('gnome_doc')
64 @after('init_gnome_doc')
65 def apply_gnome_doc(self):
66         self.env['APPNAME'] = self.doc_module
67         lst = self.to_list(self.doc_linguas)
68         bld = self.bld
69         lst.append('C')
70
71         for x in lst:
72                 if not x == 'C':
73                         tsk = self.create_task('xml2po')
74                         node = self.path.find_resource(x+'/'+x+'.po')
75                         src = self.path.find_resource('C/%s.xml' % self.doc_module)
76                         out = self.path.find_or_declare('%s/%s.xml' % (x, self.doc_module))
77                         tsk.set_inputs([node, src])
78                         tsk.set_outputs(out)
79                 else:
80                         out = self.path.find_resource('%s/%s.xml' % (x, self.doc_module))
81
82                 tsk2 = self.create_task('xsltproc2po')
83                 out2 = self.path.find_or_declare('%s/%s-%s.omf' % (x, self.doc_module, x))
84                 tsk2.set_outputs(out2)
85                 node = self.path.find_resource(self.doc_module+".omf.in")
86                 tsk2.inputs = [node, out]
87
88                 tsk2.run_after.append(tsk)
89
90                 if bld.is_install:
91                         path = self.install_path + '/gnome/help/%s/%s' % (self.doc_module, x)
92                         bld.install_files(self.install_path + '/omf', out2, env=self.env)
93                         for y in self.to_list(self.doc_figures):
94                                 try:
95                                         os.stat(self.path.abspath() + '/' + x + '/' + y)
96                                         bld.install_as(path + '/' + y, self.path.abspath() + '/' + x + '/' + y)
97                                 except:
98                                         bld.install_as(path + '/' + y, self.path.abspath() + '/C/' + y)
99                         bld.install_as(path + '/%s.xml' % self.doc_module, out.abspath(self.env))
100                         if x == 'C':
101                                 xmls = self.to_list(self.doc_includes)
102                                 xmls.append(self.doc_entities)
103                                 for z in xmls:
104                                         out = self.path.find_resource('%s/%s' % (x, z))
105                                         bld.install_as(path + '/%s' % z, out.abspath(self.env))
106
107 # OBSOLETE
108 class xml_to_taskgen(TaskGen.task_gen):
109         def __init__(self, *k, **kw):
110                 TaskGen.task_gen.__init__(self, *k, **kw)
111
112 @feature('xml_to')
113 def init_xml_to(self):
114         Utils.def_attrs(self,
115                 source = 'xmlfile',
116                 xslt = 'xlsltfile',
117                 target = 'hey',
118                 default_install_path = '${PREFIX}',
119                 task_created = None)
120
121 @feature('xml_to')
122 @after('init_xml_to')
123 def apply_xml_to(self):
124         xmlfile = self.path.find_resource(self.source)
125         xsltfile = self.path.find_resource(self.xslt)
126         tsk = self.create_task('xmlto', [xmlfile, xsltfile], xmlfile.change_ext('html'))
127         tsk.install_path = self.install_path
128
129 def sgml_scan(self):
130         node = self.inputs[0]
131
132         env = self.env
133         variant = node.variant(env)
134
135         fi = open(node.abspath(env), 'r')
136         content = fi.read()
137         fi.close()
138
139         # we should use a sgml parser :-/
140         name = n1_regexp.findall(content)[0]
141         num = n2_regexp.findall(content)[0]
142
143         doc_name = name+'.'+num
144
145         if not self.outputs:
146                 self.outputs = [self.generator.path.find_or_declare(doc_name)]
147
148         return ([], [doc_name])
149
150 class gnome_sgml2man_taskgen(TaskGen.task_gen):
151         def __init__(self, *k, **kw):
152                 TaskGen.task_gen.__init__(self, *k, **kw)
153
154 @feature('gnome_sgml2man')
155 def apply_gnome_sgml2man(self):
156         """
157         we could make it more complicated, but for now we just scan the document each time
158         """
159         assert(getattr(self, 'appname', None))
160
161         def install_result(task):
162                 out = task.outputs[0]
163                 name = out.name
164                 ext = name[-1]
165                 env = task.env
166                 self.bld.install_files('${DATADIR}/man/man%s/' % ext, out, env)
167
168         self.bld.rescan(self.path)
169         for name in self.bld.cache_dir_contents[self.path.id]:
170                 base, ext = os.path.splitext(name)
171                 if ext != '.sgml': continue
172
173                 task = self.create_task('sgml2man')
174                 task.set_inputs(self.path.find_resource(name))
175                 task.task_generator = self
176                 if self.bld.is_install: task.install = install_result
177                 # no outputs, the scanner does it
178                 # no caching for now, this is not a time-critical feature
179                 # in the future the scanner can be used to do more things (find dependencies, etc)
180                 task.scan()
181
182 cls = Task.simple_task_type('sgml2man', '${SGML2MAN} -o ${TGT[0].bld_dir(env)} ${SRC}  > /dev/null', color='BLUE')
183 cls.scan = sgml_scan
184 cls.quiet = 1
185
186 Task.simple_task_type('xmlto', '${XMLTO} html -m ${SRC[1].abspath(env)} ${SRC[0].abspath(env)}')
187
188 Task.simple_task_type('xml2po', '${XML2PO} ${XML2POFLAGS} ${SRC} > ${TGT}', color='BLUE')
189
190 # how do you expect someone to understand this?!
191 xslt_magic = """${XSLTPROC2PO} -o ${TGT[0].abspath(env)} \
192 --stringparam db2omf.basename ${APPNAME} \
193 --stringparam db2omf.format docbook \
194 --stringparam db2omf.lang ${TGT[0].abspath(env)[:-4].split('-')[-1]} \
195 --stringparam db2omf.dtd '-//OASIS//DTD DocBook XML V4.3//EN' \
196 --stringparam db2omf.omf_dir ${PREFIX}/share/omf \
197 --stringparam db2omf.help_dir ${PREFIX}/share/gnome/help \
198 --stringparam db2omf.omf_in ${SRC[0].abspath(env)} \
199 --stringparam db2omf.scrollkeeper_cl ${SCROLLKEEPER_DATADIR}/Templates/C/scrollkeeper_cl.xml \
200 ${DB2OMF} ${SRC[1].abspath(env)}"""
201
202 #--stringparam db2omf.dtd '-//OASIS//DTD DocBook XML V4.3//EN' \
203 Task.simple_task_type('xsltproc2po', xslt_magic, color='BLUE')
204
205 def detect(conf):
206         conf.check_tool('gnu_dirs glib2 dbus')
207         sgml2man = conf.find_program('docbook2man', var='SGML2MAN')
208
209         def getstr(varname):
210                 return getattr(Options.options, varname, '')
211
212         # addefine also sets the variable to the env
213         conf.define('GNOMELOCALEDIR', os.path.join(conf.env['DATADIR'], 'locale'))
214
215         xml2po = conf.find_program('xml2po', var='XML2PO')
216         xsltproc2po = conf.find_program('xsltproc', var='XSLTPROC2PO')
217         conf.env['XML2POFLAGS'] = '-e -p'
218         conf.env['SCROLLKEEPER_DATADIR'] = Utils.cmd_output("scrollkeeper-config --pkgdatadir", silent=1).strip()
219         conf.env['DB2OMF'] = Utils.cmd_output("/usr/bin/pkg-config --variable db2omf gnome-doc-utils", silent=1).strip()
220
221 def set_options(opt):
222         opt.add_option('--want-rpath', type='int', default=1, dest='want_rpath', help='set rpath to 1 or 0 [Default 1]')
223