on 669c2af help sign in
unity8/tools/registry-tracker.cpp
146 lines 4.9 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Michal Hruby <michal.hruby@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// Qt
21#include <QtGlobal>
22#include <QDir>
24// local
25#include "registry-tracker.h"
28RegistryTracker::RegistryTracker(QStringList const& scopes, bool systemScopes, bool serverScopes):
29 m_scopes(scopes),
30 m_systemScopes(systemScopes),
31 m_serverScopes(serverScopes),
32 m_registry(nullptr),
33 m_endpoints_dir(QDir::temp().filePath("scope-dev-endpoints.XXXXXX"))
35 runRegistry();
38RegistryTracker::~RegistryTracker()
40 if (m_registry.state() != QProcess::NotRunning) {
41 m_registry.terminate();
42 m_registry.waitForFinished(5000);
43 m_registry.kill();
44 }
47#define RUNTIME_CONFIG \
48"[Runtime]\n" \
49"Registry.Identity = Registry\n" \
50"Registry.ConfigFile = %1\n" \
51"Default.Middleware = Zmq\n" \
52"Zmq.ConfigFile = %2\n" \
53"Smartscopes.Registry.Identity = %3\n"
55#define REGISTRY_CONFIG \
56"[Registry]\n" \
57"Middleware = Zmq\n" \
58"Zmq.ConfigFile = %1\n" \
59"Scope.InstallDir = %2\n" \
60"Scoperunner.Path = %3\n"
62#define MW_CONFIG \
63"[Zmq]\n" \
64"EndpointDir = %1\n"
66void RegistryTracker::runRegistry()
68 QDir tmp(QDir::temp());
69 m_runtime_config.setFileTemplate(tmp.filePath("Runtime.XXXXXX.ini"));
70 m_registry_config.setFileTemplate(tmp.filePath("Registry.XXXXXX.ini"));
71 m_mw_config.setFileTemplate(tmp.filePath("Zmq.XXXXXX.ini"));
73 if (!m_runtime_config.open() || !m_registry_config.open() || !m_mw_config.open() || !m_endpoints_dir.isValid()) {
74 qWarning("Unable to open temporary files!");
75 return;
76 }
78 // FIXME At the moment we use pkg-config but ideally the library
79 // would just have a function that returns these values
80 QString scopeInstallDir;
81 QString scopeRegistryBin;
82 QString scopeRunnerBin;
83 {
84 QProcess pkg_config;
85 QByteArray output;
86 QStringList arguments;
87 arguments << "--variable=scopesdir";
88 arguments << "libunity-scopes";
89 pkg_config.start("pkg-config", arguments);
90 pkg_config.waitForFinished();
91 output = pkg_config.readAllStandardOutput();
92 scopeInstallDir = QDir(QString::fromLocal8Bit(output)).path().trimmed();
94 arguments[0] = "--variable=scoperegistry_bin";
95 pkg_config.start("pkg-config", arguments);
96 pkg_config.waitForFinished();
97 output = pkg_config.readAllStandardOutput();
98 scopeRegistryBin = QString::fromLocal8Bit(output).trimmed();
100 arguments[0] = "--variable=scoperunner_bin";
101 pkg_config.start("pkg-config", arguments);
102 pkg_config.waitForFinished();
103 output = pkg_config.readAllStandardOutput();
104 scopeRunnerBin = QString::fromLocal8Bit(output).trimmed();
105 }
107 if (scopeInstallDir.isEmpty() || scopeRegistryBin.isEmpty() || scopeRunnerBin.isEmpty()) {
108 qWarning("Unable to find libunity-scopes package config file");
109 return;
110 }
112 // FIXME: keep in sync with the SSRegistry config
113 QString runtime_ini = QString(RUNTIME_CONFIG).arg(m_registry_config.fileName(), m_mw_config.fileName(), m_serverScopes ? "SSRegistry" : "");
114 if (!m_systemScopes) {
115 m_scopeInstallDir.reset(new QTemporaryDir(tmp.filePath("scopes.XXXXXX")));
116 if (!m_scopeInstallDir->isValid()) {
117 qWarning("Unable to create temporary scopes directory!");
118 }
119 scopeInstallDir = m_scopeInstallDir->path();
120 }
121 QString registry_ini = QString(REGISTRY_CONFIG).arg(m_mw_config.fileName(), scopeInstallDir, scopeRunnerBin);
122 QString mw_ini = QString(MW_CONFIG).arg(m_endpoints_dir.path());
124 if (!m_systemScopes) {
125 // Disable OEM and Click scopes when system scopes are disabled
126 registry_ini += "OEM.InstallDir = /unused\n";
127 registry_ini += "Click.InstallDir = /unused\n";
128 }
130 m_runtime_config.write(runtime_ini.toUtf8());
131 m_registry_config.write(registry_ini.toUtf8());
132 m_mw_config.write(mw_ini.toUtf8());
134 m_runtime_config.flush();
135 m_registry_config.flush();
136 m_mw_config.flush();
138 qputenv("UNITY_SCOPES_RUNTIME_PATH", m_runtime_config.fileName().toLocal8Bit());
140 QStringList arguments;
141 arguments << m_runtime_config.fileName();
142 arguments << m_scopes;
144 m_registry.setProcessChannelMode(QProcess::ForwardedChannels);
145 m_registry.start(scopeRegistryBin, arguments);