qBittorrent
foreignapps.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2018 Mike Tzou
4  * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
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, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * In addition, as a special exception, the copyright holders give permission to
21  * link this program with the OpenSSL project's "OpenSSL" library (or with
22  * modified versions of it that use the same license as the "OpenSSL" library),
23  * and distribute the linked executables. You must obey the GNU General Public
24  * License in all respects for all of the code used other than "OpenSSL". If you
25  * modify file(s), you may extend this exception to your version of the file(s),
26  * but you are not obligated to do so. If you do not wish to do so, delete this
27  * exception statement from your version.
28  */
29 
30 #include "foreignapps.h"
31 
32 #if defined(Q_OS_WIN)
33 #include <windows.h>
34 #endif
35 
36 #include <QCoreApplication>
37 #include <QProcess>
38 #include <QRegularExpression>
39 #include <QStringList>
40 
41 #if defined(Q_OS_WIN)
42 #include <QDir>
43 #endif
44 
45 #include "base/logger.h"
46 #include "base/utils/bytearray.h"
47 
48 using namespace Utils::ForeignApps;
49 
50 namespace
51 {
52  bool testPythonInstallation(const QString &exeName, PythonInfo &info)
53  {
54  QProcess proc;
55  proc.start(exeName, {"--version"}, QIODevice::ReadOnly);
56  if (proc.waitForFinished() && (proc.exitCode() == QProcess::NormalExit))
57  {
58  QByteArray procOutput = proc.readAllStandardOutput();
59  if (procOutput.isEmpty())
60  procOutput = proc.readAllStandardError();
61  procOutput = procOutput.simplified();
62 
63  // Software 'Anaconda' installs its own python interpreter
64  // and `python --version` returns a string like this:
65  // "Python 3.4.3 :: Anaconda 2.3.0 (64-bit)"
66  const QVector<QByteArray> outputSplit = Utils::ByteArray::splitToViews(procOutput, " ", Qt::SkipEmptyParts);
67  if (outputSplit.size() <= 1)
68  return false;
69 
70  // User reports: `python --version` -> "Python 3.6.6+"
71  // So trim off unrelated characters
72  const QString versionStr = outputSplit[1];
73  const int idx = versionStr.indexOf(QRegularExpression("[^\\.\\d]"));
74 
75  try
76  {
77  info = {exeName, versionStr.left(idx)};
78  }
79  catch (const RuntimeError &)
80  {
81  return false;
82  }
83 
84  LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, executable name: '%1', version: %2")
85  .arg(info.executableName, info.version), Log::INFO);
86  return true;
87  }
88 
89  return false;
90  }
91 
92 #if defined(Q_OS_WIN)
93  enum REG_SEARCH_TYPE
94  {
95  USER,
96  SYSTEM_32BIT,
97  SYSTEM_64BIT
98  };
99 
100  QStringList getRegSubkeys(const HKEY handle)
101  {
102  QStringList keys;
103 
104  DWORD cSubKeys = 0;
105  DWORD cMaxSubKeyLen = 0;
106  LONG res = ::RegQueryInfoKeyW(handle, NULL, NULL, NULL, &cSubKeys, &cMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, NULL);
107 
108  if (res == ERROR_SUCCESS)
109  {
110  ++cMaxSubKeyLen; // For null character
111  LPWSTR lpName = new WCHAR[cMaxSubKeyLen];
112  DWORD cName;
113 
114  for (DWORD i = 0; i < cSubKeys; ++i)
115  {
116  cName = cMaxSubKeyLen;
117  res = ::RegEnumKeyExW(handle, i, lpName, &cName, NULL, NULL, NULL, NULL);
118  if (res == ERROR_SUCCESS)
119  keys.push_back(QString::fromWCharArray(lpName));
120  }
121 
122  delete[] lpName;
123  }
124 
125  return keys;
126  }
127 
128  QString getRegValue(const HKEY handle, const QString &name = {})
129  {
130  QString result;
131 
132  DWORD type = 0;
133  DWORD cbData = 0;
134  LPWSTR lpValueName = NULL;
135  if (!name.isEmpty())
136  {
137  lpValueName = new WCHAR[name.size() + 1];
138  name.toWCharArray(lpValueName);
139  lpValueName[name.size()] = 0;
140  }
141 
142  // Discover the size of the value
143  ::RegQueryValueExW(handle, lpValueName, NULL, &type, NULL, &cbData);
144  DWORD cBuffer = (cbData / sizeof(WCHAR)) + 1;
145  LPWSTR lpData = new WCHAR[cBuffer];
146  LONG res = ::RegQueryValueExW(handle, lpValueName, NULL, &type, (LPBYTE)lpData, &cbData);
147  if (lpValueName)
148  delete[] lpValueName;
149 
150  if (res == ERROR_SUCCESS)
151  {
152  lpData[cBuffer - 1] = 0;
153  result = QString::fromWCharArray(lpData);
154  }
155  delete[] lpData;
156 
157  return result;
158  }
159 
160  QString pythonSearchReg(const REG_SEARCH_TYPE type)
161  {
162  HKEY hkRoot;
163  if (type == USER)
164  hkRoot = HKEY_CURRENT_USER;
165  else
166  hkRoot = HKEY_LOCAL_MACHINE;
167 
168  REGSAM samDesired = KEY_READ;
169  if (type == SYSTEM_32BIT)
170  samDesired |= KEY_WOW64_32KEY;
171  else if (type == SYSTEM_64BIT)
172  samDesired |= KEY_WOW64_64KEY;
173 
174  QString path;
175  LONG res = 0;
176  HKEY hkPythonCore;
177  res = ::RegOpenKeyExW(hkRoot, L"SOFTWARE\\Python\\PythonCore", 0, samDesired, &hkPythonCore);
178 
179  if (res == ERROR_SUCCESS)
180  {
181  QStringList versions = getRegSubkeys(hkPythonCore);
182  qDebug("Python versions nb: %d", versions.size());
183  versions.sort();
184 
185  bool found = false;
186  while (!found && !versions.empty())
187  {
188  const QString version = versions.takeLast() + "\\InstallPath";
189  LPWSTR lpSubkey = new WCHAR[version.size() + 1];
190  version.toWCharArray(lpSubkey);
191  lpSubkey[version.size()] = 0;
192 
193  HKEY hkInstallPath;
194  res = ::RegOpenKeyExW(hkPythonCore, lpSubkey, 0, samDesired, &hkInstallPath);
195  delete[] lpSubkey;
196 
197  if (res == ERROR_SUCCESS)
198  {
199  qDebug("Detected possible Python v%s location", qUtf8Printable(version));
200  path = getRegValue(hkInstallPath);
201  ::RegCloseKey(hkInstallPath);
202 
203  if (!path.isEmpty())
204  {
205  const QDir baseDir {path};
206 
207  if (baseDir.exists("python3.exe"))
208  {
209  found = true;
210  path = baseDir.filePath("python3.exe");
211  }
212  else if (baseDir.exists("python.exe"))
213  {
214  found = true;
215  path = baseDir.filePath("python.exe");
216  }
217  }
218  }
219  }
220 
221  if (!found)
222  path = QString();
223 
224  ::RegCloseKey(hkPythonCore);
225  }
226 
227  return path;
228  }
229 
230  QString findPythonPath()
231  {
232  QString path = pythonSearchReg(USER);
233  if (!path.isEmpty())
234  return path;
235 
236  path = pythonSearchReg(SYSTEM_32BIT);
237  if (!path.isEmpty())
238  return path;
239 
240  path = pythonSearchReg(SYSTEM_64BIT);
241  if (!path.isEmpty())
242  return path;
243 
244  // Fallback: Detect python from default locations
245  const QFileInfoList dirs = QDir("C:/").entryInfoList({"Python*"}, QDir::Dirs, (QDir::Name | QDir::Reversed));
246  for (const QFileInfo &info : dirs)
247  {
248  const QString py3Path {info.absolutePath() + "/python3.exe"};
249  if (QFile::exists(py3Path))
250  return py3Path;
251 
252  const QString pyPath {info.absolutePath() + "/python.exe"};
253  if (QFile::exists(pyPath))
254  return pyPath;
255  }
256 
257  return {};
258  }
259 #endif // Q_OS_WIN
260 }
261 
263 {
264  return (!executableName.isEmpty() && version.isValid());
265 }
266 
268 {
269  return (version >= Version {3, 5, 0});
270 }
271 
273 {
274  static PythonInfo pyInfo;
275  if (!pyInfo.isValid())
276  {
277  if (testPythonInstallation("python3", pyInfo))
278  return pyInfo;
279 
280  if (testPythonInstallation("python", pyInfo))
281  return pyInfo;
282 
283 #if defined(Q_OS_WIN)
284  if (testPythonInstallation(findPythonPath(), pyInfo))
285  return pyInfo;
286 #endif
287 
288  LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python not detected"), Log::INFO);
289  }
290 
291  return pyInfo;
292 }
void LogMsg(const QString &message, const Log::MsgType &type)
Definition: logger.cpp:125
@ INFO
Definition: logger.h:46
QVector< QByteArray > splitToViews(const QByteArray &in, const QByteArray &sep, const Qt::SplitBehavior behavior=Qt::KeepEmptyParts)
Definition: bytearray.cpp:34
PythonInfo pythonInfo()
bool testPythonInstallation(const QString &exeName, PythonInfo &info)
Definition: foreignapps.cpp:52