1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Copyright (C) 2014 Canonical Ltd.
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU Lesser General Public License as published by
7
# the Free Software Foundation; version 3.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU Lesser General Public License for more details.
14
# You should have received a copy of the GNU Lesser General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
# David Planella <david.planella@ubuntu.com>
21
This module parses a configuration file from the Qt Creator's CMake plugin and
22
enables programmatical read-only access to several of its configuration options
26
from lxml import etree
29
class CMakePluginParseError(Exception):
31
Custom exception for errors during the parsing of a
32
CMakeLists.txt.user file
34
def __init__(self, message):
35
Exception.__init__(self, message)
38
class CMakePluginParser(object):
40
Parses a CMake plugin's config file and provides R/O access to its
41
configuration options """
43
def __init__(self, cmakelists_usr_file='CMakeLists.txt.user'):
44
self.usr_file = cmakelists_usr_file
47
self.info = etree.parse(self.usr_file)
49
sys.stderr.write("Could not open the given " +
50
"CMakeLists.txt.user file: " + self.info)
53
def _get_active_build_target(self):
55
Return the active build target from the current project in Qt Creator
59
active_build_target_nr = self.info.xpath(
61
"[text()='ProjectExplorer.Project.ActiveTarget']" +
64
raise CMakePluginParseError("Could not find the active build " +
65
"target in the CMake plugin's config")
67
active_build_target = "ProjectExplorer.Project.Target." + \
68
active_build_target_nr
70
return active_build_target
72
def _get_active_build_config(self, active_build_target):
73
"""Return the active build config from the active build targed"""
76
active_build_config_nr = self.info.xpath(
77
"./data/variable[text()='{0}']".format(active_build_target) +
79
"'ProjectExplorer.Target.ActiveBuildConfiguration']")[0].text
81
raise CMakePluginParseError("Could not find the active build " +
82
"target's active build config " +
83
"in the CMake plugin's config")
85
active_build_config = "ProjectExplorer.Target.BuildConfiguration." + \
86
active_build_config_nr
88
return active_build_config
90
def _get_active_build_config_path(self):
91
"""Return the active build config's absolute path"""
93
active_build_target = self._get_active_build_target()
94
active_build_config = \
95
self._get_active_build_config(active_build_target)
98
active_build_config_node = self.info.xpath(
99
"./data/variable[text()='{0}']".format(active_build_target) +
100
"/..//valuemap[@key='{0}']".format(active_build_config))[0]
102
raise CMakePluginParseError("Could not find the active " +
103
"build config's node " +
104
"in the CMake plugin's config")
107
active_build_config_path = active_build_config_node.xpath(
109
"'ProjectExplorer.BuildConfiguration.BuildDirectory']")[0].text
111
raise CMakePluginParseError("Could not find the active build " +
112
"directory in the CMake plugin's " +
115
return active_build_config_path
118
def active_build_dir(self):
119
"""Return the active build config's directory as an absolute path"""
120
return self._get_active_build_config_path()