Prepared ugit for i18n support
[git-cola.git] / buildutils / pyuic4.py
blob9c3dd34de6dca8b702aee0a9214b2e21a73b4c8b
1 #!/usr/bin/env python
2 # Author: David Aguilar
4 '''
5 pyuic4: support for generating .py Python scripts from Qt Designer4 .ui files.
7 NOTES:
9 - If PYQT4_ROOT is given(absolute path), the configuration will look
10 in PYQT4_ROOT/bin first.
12 - This module hooks adds a python hook that runs
13 a pyuic4 action when '.ui' files are encoutnered.
16 '''
17 import os
19 import Action
20 import Params
22 import python
23 python.pyobj.s_default_ext.append('.ui')
25 def set_options(opt):
26 '''Adds the --pyuic4 build option.'''
27 opt.add_option( '--pyuic4', type='string', dest='pyuic4',
28 default='', help='path to pyuic4')
30 def create_pyuic4_tasks(self, node):
31 '''Creates the tasks to generate python files.
32 The 'pyuic4' action is called for this.'''
34 # Create a pyuic4 task to generate the python .py file
35 pyuic4task = self.create_task('pyuic4')
36 pyuic4task.set_inputs(node)
37 pyuic4task.set_outputs(node.change_ext('.py'))
39 # Add the python compilation tasks
40 if self.pyc:
41 task = self.create_task('pyc', self.env, 50)
42 task.set_inputs(node.change_ext('.py'))
43 task.set_outputs(node.change_ext('.pyc'))
45 if self.pyo:
46 task = self.create_task('pyo', self.env, 50)
47 task.set_inputs(node.change_ext('.py'))
48 task.set_outputs(node.change_ext('.pyo'))
50 def setup(env):
51 '''Creates a python hook and registers it with the environment.'''
52 # create the hook action
53 cmd_template = '${PYUIC4} ${PYUIC4_FLAGS} ${SRC} -o ${TGT}'
54 Action.simple_action('pyuic4', cmd_template, 'BLUE')
56 # register .ui for use with python
57 env.hook('py', 'PYUIC4_EXT', create_pyuic4_tasks)
59 def detect(conf):
60 env = conf.env
61 opt = Params.g_options
63 pyuic4 = None
64 try:
65 pyuic4 = opt.pyuic4
66 except:
67 pass
69 if not pyuic4:
70 qtdir = os.environ.get('PYQT4_ROOT', '')
71 if qtdir:
72 binpath = [qtdir] + os.environ['PATH'].split(':')
73 else:
74 binpath = os.environ['PATH'].split(':')
76 for f in ['pyuic4', 'pyuic-qt4', 'pyuic']:
77 pyuic4 = conf.find_program(f, path_list=binpath)
78 if pyuic4:
79 break
81 if not pyuic4:
82 conf.check_message('pyuic4 binary', '(not found)', 0)
83 return False
85 # Set the path to pyuic4
86 env['PYUIC4'] = pyuic4
87 env['PYUIC4_EXT'] = ['.ui']
88 env['PYUIC4_FLAGS'] = '-x'
90 vercmd = env['PYUIC4'] + ' --version 2>&1'
91 version = os.popen(vercmd).read().strip().split(' ')[-1]
92 version = version.split('.')[0]
93 if not version.isdigit() or int(version) < 4:
94 conf.check_message('pyuic4 version', '(not found or too old)', 0,
95 option= '(%s)' % version)
96 return False
98 conf.check_message('pyuic4 version', '', 1, option='(%s)' % version)
100 # all tests passed
101 return True