Improve the config option handling
[git-cola.git] / build / pyuic4.py
blobb285011423a9ce72baa91ae0e3abf2412d4f155f
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 Object
21 import Params
23 import python
24 python.pyobj.s_default_ext.append('.ui')
26 def set_options(opt):
27 '''Adds the --pyuic4 build option.'''
28 opt.add_option( '--pyuic4', type='string', dest='pyuic4',
29 default='', help='path to pyuic4')
31 def create_pyuic4_tasks(self, node):
32 '''Creates the tasks to generate python files.
33 The 'pyuic4' action is called for this.'''
35 # Create a pyuic4 task to generate the python .py file
36 pyuic4task = self.create_task('pyuic4')
37 pyuic4task.set_inputs(node)
38 pyuic4task.set_outputs(node.change_ext('.py'))
40 # Add the python compilation tasks
41 if self.pyc:
42 task = self.create_task('pyc', self.env, 50)
43 task.set_inputs(node.change_ext('.py'))
44 task.set_outputs(node.change_ext('.pyc'))
46 if self.pyo:
47 task = self.create_task('pyo', self.env, 50)
48 task.set_inputs(node.change_ext('.py'))
49 task.set_outputs(node.change_ext('.pyo'))
51 def setup(env):
52 '''Creates a python hook and registers it with the environment.'''
53 # create the hook action
54 cmd_template = '${PYUIC4} ${PYUIC4_FLAGS} ${SRC} -o ${TGT}'
55 Action.simple_action('pyuic4', cmd_template, 'GREEN')
57 # register .ui for use with python
58 Object.hook('py', 'PYUIC4_EXT', create_pyuic4_tasks)
60 def detect(conf):
61 env = conf.env
62 opt = Params.g_options
64 pyuic4 = None
65 try:
66 pyuic4 = opt.pyuic4
67 except:
68 pass
70 if not pyuic4:
71 qtdir = os.environ.get('PYQT4_ROOT', '')
72 if qtdir:
73 binpath = [qtdir] + os.environ['PATH'].split(':')
74 else:
75 binpath = os.environ['PATH'].split(':')
77 for f in ['pyuic4', 'pyuic-qt4', 'pyuic']:
78 pyuic4 = conf.find_program(f, path_list=binpath)
79 if pyuic4:
80 break
82 if not pyuic4:
83 conf.check_message('pyuic4 binary', '(not found)', 0)
84 Params.fatal('Error: missing PyQt4 development tools.')
85 return False
87 # Set the path to pyuic4
88 env['PYUIC4'] = pyuic4
89 env['PYUIC4_EXT'] = ['.ui']
90 env['PYUIC4_FLAGS'] = '-x'
92 vercmd = env['PYUIC4'] + ' --version 2>&1'
93 version = os.popen(vercmd).read().strip().split(' ')[-1]
94 version = version.split('.')[0]
95 if not version.isdigit() or int(version) < 4:
96 conf.check_message('pyuic4 version', '(not found or too old)', 0,
97 option= '(%s)' % version)
98 return False
100 conf.check_message('pyuic4 version', '', 1, option='(%s)' % version)
102 # all tests passed
103 return True