~ubuntu-docviewer-dev/ubuntu-docviewer-app/lo-viewer

« back to all changes in this revision

Viewing changes to tests/autopilot/ubuntu_docviewer_app/CMakePluginParser.py

  • Committer: Tarmac
  • Author(s): David Planella, Daniel Holbach, Fabio Colella, Stefano Verzegnassi, Arto Jalkanen
  • Date: 2014-10-29 07:39:08 UTC
  • mfrom: (31.1.22 add-plugin)
  • Revision ID: tarmac-20141029073908-8s6r18syexjo37ga
Adds the file plugin to the source tree.

Approved by Stefano Verzegnassi, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
#
 
3
# Copyright (C) 2014 Canonical Ltd.
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
#
 
17
# Author:
 
18
#   David Planella <david.planella@ubuntu.com>
 
19
 
 
20
"""
 
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
 
23
"""
 
24
 
 
25
import sys
 
26
from lxml import etree
 
27
 
 
28
 
 
29
class CMakePluginParseError(Exception):
 
30
    """
 
31
    Custom exception for errors during the parsing of a
 
32
    CMakeLists.txt.user file
 
33
    """
 
34
    def __init__(self, message):
 
35
        Exception.__init__(self, message)
 
36
 
 
37
 
 
38
class CMakePluginParser(object):
 
39
    """
 
40
    Parses a CMake plugin's config file and provides R/O access to its
 
41
    configuration options """
 
42
 
 
43
    def __init__(self, cmakelists_usr_file='CMakeLists.txt.user'):
 
44
        self.usr_file = cmakelists_usr_file
 
45
 
 
46
        try:
 
47
            self.info = etree.parse(self.usr_file)
 
48
        except:
 
49
            sys.stderr.write("Could not open the given " +
 
50
                             "CMakeLists.txt.user file: " + self.info)
 
51
            raise
 
52
 
 
53
    def _get_active_build_target(self):
 
54
        """
 
55
        Return the active build target from the current project in Qt Creator
 
56
        """
 
57
 
 
58
        try:
 
59
            active_build_target_nr = self.info.xpath(
 
60
                "./data/variable" +
 
61
                "[text()='ProjectExplorer.Project.ActiveTarget']" +
 
62
                "/../value")[0].text
 
63
        except:
 
64
            raise CMakePluginParseError("Could not find the active build " +
 
65
                                        "target in the CMake plugin's config")
 
66
 
 
67
        active_build_target = "ProjectExplorer.Project.Target." + \
 
68
            active_build_target_nr
 
69
 
 
70
        return active_build_target
 
71
 
 
72
    def _get_active_build_config(self, active_build_target):
 
73
        """Return the active build config from the active build targed"""
 
74
 
 
75
        try:
 
76
            active_build_config_nr = self.info.xpath(
 
77
                "./data/variable[text()='{0}']".format(active_build_target) +
 
78
                "/..//value[@key="
 
79
                "'ProjectExplorer.Target.ActiveBuildConfiguration']")[0].text
 
80
        except:
 
81
            raise CMakePluginParseError("Could not find the active build " +
 
82
                                        "target's active build config " +
 
83
                                        "in the CMake plugin's config")
 
84
 
 
85
        active_build_config = "ProjectExplorer.Target.BuildConfiguration." + \
 
86
            active_build_config_nr
 
87
 
 
88
        return active_build_config
 
89
 
 
90
    def _get_active_build_config_path(self):
 
91
        """Return the active build config's absolute path"""
 
92
 
 
93
        active_build_target = self._get_active_build_target()
 
94
        active_build_config = \
 
95
            self._get_active_build_config(active_build_target)
 
96
 
 
97
        try:
 
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]
 
101
        except:
 
102
            raise CMakePluginParseError("Could not find the active " +
 
103
                                        "build config's node " +
 
104
                                        "in the CMake plugin's config")
 
105
 
 
106
        try:
 
107
            active_build_config_path = active_build_config_node.xpath(
 
108
                "./value[@key=" +
 
109
                "'ProjectExplorer.BuildConfiguration.BuildDirectory']")[0].text
 
110
        except:
 
111
            raise CMakePluginParseError("Could not find the active build " +
 
112
                                        "directory in the CMake plugin's " +
 
113
                                        "config")
 
114
 
 
115
        return active_build_config_path
 
116
 
 
117
    @property
 
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()