]> git.lyx.org Git - lyx.git/blob - src/support/os.cpp
Remove updateInfo() calls in favor of doing the relevant work
[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 #include "debug.h"
15 #include "filetools.h"
16 #include "qstring_helpers.h"
17
18 #include <QDir>
19
20 #if defined(__CYGWIN__)
21 #include "support/os_cygwin.cpp"
22 #elif defined(_WIN32)
23 #include "support/os_win32.cpp"
24 #else
25 #include "support/os_unix.cpp"
26 #endif
27
28 // Static assert to break compilation on platforms where
29 // int/unsigned int is not 4 bytes. Added to make sure that
30 // e.g., the author hash is always 32-bit.
31 template<bool Condition> struct static_assert_helper;
32 template <> struct static_assert_helper<true> {};
33 enum {
34         dummy = sizeof(static_assert_helper<sizeof(int) == 4>)
35 };
36
37 namespace lyx {
38 namespace support {
39 namespace os {
40
41 static string const python23(string const & binary, bool verbose = false)
42 {
43         if (verbose)
44                 lyxerr << "Examining " << binary << "\n";
45
46         // Check whether this is a python 2 or 3 binary.
47         cmd_ret const out = runCommand(binary + " -V 2>&1");
48         if (out.first < 0 ||
49             (!prefixIs(out.second, "Python 2") &&
50              !prefixIs(out.second, "Python 3")))
51                 return string();
52
53         if (verbose)
54                 lyxerr << "Found " << out.second << "\n";
55         return binary;
56 }
57
58
59 int timeout_min()
60 {
61         return 3;
62 }
63
64
65 string const python(bool reset)
66 {
67         // Check whether the first python in PATH is the right one.
68         static string command = python23("python -tt");
69         // FIXME THREAD
70         if (reset) {
71                 command = python23("python -tt");
72         }
73
74         if (command.empty()) {
75                 // It was not, so check whether we can find it elsewhere in
76                 // PATH, maybe with some suffix appended.
77                 vector<string> const path = getEnvPath("PATH");
78                 vector<string>::const_iterator it = path.begin();
79                 vector<string>::const_iterator const end = path.end();
80                 lyxerr << "Looking for python v2.x or 3.x ...\n";
81                 for (; it != end; ++it) {
82                         QString const dir = toqstr(*it);
83                         string const localdir = dir.toLocal8Bit().constData();
84                         QDir qdir(dir);
85                         qdir.setFilter(QDir::Files | QDir::Executable);
86                         QStringList list = qdir.entryList(QStringList("python*"));
87                         for (int i = 0; i < list.size() && command.empty(); ++i) {
88                                 string const binary = addName(localdir,
89                                         list.at(i).toLocal8Bit().constData());
90                                 command = python23(binary, true);
91                         }
92                 }
93
94                 // Default to "python" if no usable binary was found.
95                 if (command.empty()) {
96                         lyxerr << "Warning: No python v2.x or 3.x binary found.\n";
97                         command = "python";
98                 }
99
100                 // Add the -tt switch so that mixed tab/whitespace
101                 // indentation is an error
102                 command += " -tt";
103         }
104         return command;
105 }
106
107 } // namespace os
108 } // namespace support
109 } // namespace lyx