]> git.lyx.org Git - lyx.git/blob - src/support/os.cpp
abb9ec6d672d32eae81f477914f6104723021743
[lyx.git] / src / support / os.cpp
1 /**
2  * \file os.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #ifdef _WIN32
15 # define _WIN32_WINNT 0x0600
16 #endif
17
18 #include "support/convert.h"
19 #include "support/debug.h"
20 #include "support/filetools.h"
21 #include "support/qstring_helpers.h"
22
23 #include <QDir>
24
25 #include <regex>
26
27 #if defined(__CYGWIN__)
28 #include "support/os_cygwin.cpp"
29 #elif defined(_WIN32)
30 #include "support/os_win32.cpp"
31 #else
32 #include "support/os_unix.cpp"
33 #endif
34
35 // Static assert to break compilation on platforms where
36 // int/unsigned int is not 4 bytes. Added to make sure that
37 // e.g., the author hash is always 32-bit.
38 template<bool Condition> struct static_assert_helper;
39 template <> struct static_assert_helper<true> {};
40 enum {
41         dummy = sizeof(static_assert_helper<sizeof(int) == 4>)
42 };
43
44 namespace lyx {
45 namespace support {
46 namespace os {
47
48 int timeout_ms()
49 {
50         // Starting in 2.4.0, we allow the user to cancel the background
51         // process at any time with LFUN_EXPORT_CANCEL, so the timeout dialog
52         // is no longer useful.
53         // "-1" effectively disables the timeout (it is a special case in
54         // SystemcallPrivate::waitWhile()).
55         return -1;
56 }
57
58
59 static string const python23_call(string const & binary, bool verbose = false)
60 {
61         const string version_info = " -c \"from __future__ import print_function;import sys; print(sys.version_info[:2], end=\\\"\\\")\"";
62         // Default to "python" if no binary is given.
63         if (binary.empty())
64                 return "python -tt";
65
66         if (verbose)
67                 lyxerr << "Examining " << binary << "\n";
68         // Check whether this is a python 2 or 3 binary.
69         cmd_ret const out = runCommand(binary + version_info);
70
71         smatch sm;
72         try {
73                 static regex const python_reg("\\((\\d*), (\\d*)\\)");
74                 if (!out.valid || !regex_match(out.result, sm, python_reg))
75                         return string();
76         } catch(regex_error const & /*e*/) {
77                 LYXERR0("Regex error! This should not happen.");
78                 return string();
79         }
80
81         int major = convert<int>(sm.str(1));
82         int minor = convert<int>(sm.str(2));
83         if((major == 2 && minor < 7) || (major == 3 && minor < 5))
84                 return string();
85
86         if (verbose)
87                 lyxerr << "Found Python " << out.result << "\n";
88         // Add the -tt switch so that mixed tab/whitespace
89         // indentation is an error
90         return binary + " -tt";
91 }
92
93
94 static string const find_python_binary()
95 {
96         // This function takes inspiration from PEP 394 and PEP 397
97         // PEP 394 -- The "python" Command on Unix-Like Systems
98         // https://www.python.org/dev/peps/pep-0394/
99         // PEP 397 -- Python launcher for Windows
100         // https://www.python.org/dev/peps/pep-0397/
101
102 #ifdef _WIN32
103         // Check through python launcher whether python 3 is
104         // installed on computer.
105         string command = python23_call("py -3");
106 #else
107         // Check whether python3 in PATH is the right one.
108         string command = python23_call("python3");
109 #endif // _WIN32
110         if (!command.empty())
111                 return command;
112
113 #ifndef _WIN32
114         // python3 does not exists, let us try to find python3.x in PATH
115         // the search is probably broader than required
116         // but we are trying hard to find a valid python binary
117         vector<string> const path = getEnvPath("PATH");
118         lyxerr << "Looking for python 3.x ...\n";
119         for (auto const & bin : path) {
120                 QString const dir = toqstr(bin);
121                 string const localdir = dir.toLocal8Bit().constData();
122                 QDir qdir(dir);
123                 qdir.setFilter(QDir::Files | QDir::Executable);
124                 QStringList list = qdir.entryList(QStringList("python3*"));
125                 for (auto const & bin2 : list) {
126                         string const binary = "\"" + addName(localdir,
127                                 bin2.toLocal8Bit().constData()) + "\"";
128                         command = python23_call(binary, true);
129                         if (!command.empty())
130                                 return command;
131                 }
132         }
133 #endif // !_WIN32
134
135         // python 3 was not found let us look for python 2
136 #ifdef _WIN32
137         command = python23_call("py -2");
138 #else
139         command = python23_call("python2");
140 #endif // _WIN32
141         if (!command.empty())
142                 return command;
143
144 #ifdef _WIN32
145         // python launcher is not installed, let cmd auto check 
146         // PATH for a python.exe
147         command = python23_call("python");
148         if (!command.empty())
149                 return command;
150
151         //failed, prepare to search PATH manually
152         vector<string> const path = getEnvPath("PATH");
153         lyxerr << "Manually looking for python in PATH ...\n";
154         QString const exeName = "python*";
155 #else
156         // python2 does not exists, let us try to find python2.x in PATH
157         // the search is probably broader than required
158         // but we are trying hard to find a valid python binary
159         lyxerr << "Looking for python 2.x ...\n";
160         QString const exeName = "python2*";
161 #endif // _WIN32
162
163         for (auto const & bin : path) {
164                 QString const dir = toqstr(bin);
165                 string const localdir = dir.toLocal8Bit().constData();
166                 QDir qdir(dir);
167                 qdir.setFilter(QDir::Files | QDir::Executable);
168                 QStringList list = qdir.entryList(QStringList(exeName));
169                 for (auto const & bin2 : list) {
170                         string const binary = "\"" + addName(localdir,
171                                 bin2.toLocal8Bit().constData()) + "\"";
172                         command = python23_call(binary, true);
173                         if (!command.empty())
174                                 return command;
175                 }
176         }
177
178         // If this happens all hope is lost that this is a sane system
179         lyxerr << "Warning: No python v2.x or 3.x binary found.\n";
180         return python23_call("");
181 }
182
183
184 string const python(bool reset)
185 {
186         static string command = find_python_binary();
187         // FIXME THREAD
188         if (reset) {
189                 command = find_python_binary();
190         }
191         return command;
192 }
193
194
195 bool hasPython()
196 {
197         return !(python23_call(python()).empty());
198 }
199
200 } // namespace os
201 } // namespace support
202 } // namespace lyx