File: pdblauncher.cpp

package info (click to toggle)
kdevelop-python 5.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,480 kB
  • sloc: python: 183,325; cpp: 17,155; xml: 1,137; sh: 14; makefile: 13
file content (144 lines) | stat: -rw-r--r-- 4,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
    This file is part of kdev-python, the python language plugin for KDevelop
    Copyright (C) 2012  Sven Brauch <svenbrauch@googlemail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "pdblauncher.h"
#include <interfaces/idocumentcontroller.h>
#include "debugjob.h"

#include <util/executecompositejob.h>
#include <executescript/iexecutescriptplugin.h>
#include <interfaces/launchconfigurationpage.h>
#include <interfaces/ilaunchconfiguration.h>
#include <interfaces/iplugincontroller.h>
#include <interfaces/iruncontroller.h>
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>

#include <KLocalizedString>
#include <KMessageBox>
#include <KParts/MainWindow>
#include <KConfigGroup>
#include <QFileInfo>

#include <QDebug>
#include "debuggerdebug.h"
#include <util/environmentprofilelist.h>


namespace Python {
    
PdbLauncher::PdbLauncher()
{

}

QList< KDevelop::LaunchConfigurationPageFactory* > PdbLauncher::configPages() const
{
    return QList<KDevelop::LaunchConfigurationPageFactory*>();
}

QString PdbLauncher::description() const
{
    return i18n("A plugin to debug Python applications with pdb.");
}

QString PdbLauncher::id()
{
    return "pdbdebugger";
}

QString PdbLauncher::name() const
{
    return "pdbdebugger";
}

KJob* PdbLauncher::start(const QString& launchMode, KDevelop::ILaunchConfiguration* cfg)
{
    qCDebug(KDEV_PYTHON_DEBUGGER) << "start of debugger process requested";
    if ( launchMode == "debug" ) {
        IExecuteScriptPlugin* iface = KDevelop::ICore::self()->pluginController()
                                      ->pluginForExtension("org.kdevelop.IExecuteScriptPlugin")->extension<IExecuteScriptPlugin>();
        Q_ASSERT(iface);
        QString err;
        QString interpreter = iface->interpreter(cfg, err);
        
        // check the interpreter
        QProcess p;
        p.setReadChannelMode(QProcess::MergedChannels);
        p.start(interpreter, QStringList() << "--version");
        p.waitForFinished(500);
        QByteArray version = p.readAll();
        qCDebug(KDEV_PYTHON_DEBUGGER) << "interpreter version:" << version;
        if ( ! version.startsWith("Python 3.") ) {
            KMessageBox::error(ICore::self()->uiController()->activeMainWindow(),
                            i18n("Sorry, debugging is only supported for Python 3.x applications."),
                            i18n("Unsupported interpreter"));
            return nullptr;
        }

        QUrl scriptUrl;
        if ( iface->runCurrentFile(cfg) ) {
            auto document = KDevelop::ICore::self()->documentController()->activeDocument();
            if ( ! document ) {
                qCDebug(KDEV_PYTHON_DEBUGGER) << "no current document";
                return nullptr;
            }
            scriptUrl = document->url();
        }
        else {
            scriptUrl = iface->script(cfg, err);
        }

        auto wd = iface->workingDirectory(cfg);
        if( !wd.isValid() || wd.isEmpty() )
        {
            wd = QUrl::fromLocalFile( QFileInfo( scriptUrl.toLocalFile() ).absolutePath() );
        }

        DebugJob* job = new DebugJob();
        job->m_scriptUrl = scriptUrl;
        job->m_interpreter = interpreter;
        job->m_args = iface->arguments(cfg, err);
        job->m_workingDirectory = wd;

        const KDevelop::EnvironmentProfileList environmentProfiles(KSharedConfig::openConfig());
        QString envProfileName = iface->environmentProfileName(cfg);

        if (envProfileName.isEmpty()) {
            qCWarning(KDEV_PYTHON_DEBUGGER) << "No environment profile specified, looks like a broken "
                                       "configuration, please check run configuration " << cfg->name() <<
                                       ". Using default environment profile.";
            envProfileName = environmentProfiles.defaultProfileName();
        }
        job->m_envProfileName = envProfileName;

        QList<KJob*> l;
        l << job;
        return new KDevelop::ExecuteCompositeJob( KDevelop::ICore::self()->runController(), l );
    }
    qCDebug(KDEV_PYTHON_DEBUGGER) << "unknown launch mode";
    return nullptr;
}

QStringList PdbLauncher::supportedModes() const
{
    return QStringList() << "debug";
}

}