Mercurial > hg > easyhg
comparison hgrunner.cpp @ 188:07b908b4fa5f
* Set python library path when unbundling extension, generally prefer unbundled copy
author | Chris Cannam |
---|---|
date | Mon, 20 Dec 2010 16:15:43 +0000 |
parents | bf366e0b9050 |
children | 6bb510abe4f1 |
comparison
equal
deleted
inserted
replaced
187:869825bc8bc4 | 188:07b908b4fa5f |
---|---|
24 #include <QDialog> | 24 #include <QDialog> |
25 #include <QLabel> | 25 #include <QLabel> |
26 #include <QVBoxLayout> | 26 #include <QVBoxLayout> |
27 #include <QSettings> | 27 #include <QSettings> |
28 #include <QInputDialog> | 28 #include <QInputDialog> |
29 #include <QTemporaryFile> | |
29 #include <QDir> | 30 #include <QDir> |
30 | 31 |
31 #include <iostream> | 32 #include <iostream> |
32 #include <errno.h> | 33 #include <errno.h> |
33 #include <stdio.h> | 34 #include <stdio.h> |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 QString HgRunner::findExtension() | 66 QString HgRunner::findExtension() |
66 { | 67 { |
68 // If we haven't unbundled an extension, always do that (so that | |
69 // it's available in case someone wants to use it later, e.g. to | |
70 // fix a malfunctioning setup). But the path we actually prefer | |
71 // is the one in the settings first; then the unbundled one; then | |
72 // anything in the path if for some reason unbundling failed | |
73 | |
67 QSettings settings; | 74 QSettings settings; |
68 settings.beginGroup("Locations"); | 75 settings.beginGroup("Locations"); |
76 | |
77 QString unbundled = getUnbundledFileName(); | |
78 if (!QFile(unbundled).exists()) { | |
79 unbundled = unbundleExtension(); | |
80 } | |
81 | |
69 QString extpath = settings.value("extensionpath", "").toString(); | 82 QString extpath = settings.value("extensionpath", "").toString(); |
70 if (extpath != "") return extpath; | 83 if (extpath != "") return extpath; |
71 extpath = findInPath("easyhg.py", m_myDirPath, false); | 84 |
72 if (extpath == "easyhg.py") { | 85 extpath = unbundled; |
73 extpath = unbundleExtension(); | 86 if (extpath == "") { |
74 } | 87 extpath = findInPath("easyhg.py", m_myDirPath, false); |
88 if (extpath == "easyhg.py") { | |
89 extpath = ""; | |
90 } | |
91 } | |
92 | |
75 settings.setValue("extensionpath", extpath); | 93 settings.setValue("extensionpath", extpath); |
76 return extpath; | 94 return extpath; |
77 } | 95 } |
78 | 96 |
79 QString HgRunner::unbundleExtension() | 97 QString HgRunner::getUnbundledFileName() |
80 { | 98 { |
81 QString bundled = ":easyhg.py"; | |
82 QString home = QDir::homePath(); | 99 QString home = QDir::homePath(); |
83 QString target = QString("%1/.easyhg").arg(home); | 100 QString target = QString("%1/.easyhg").arg(home); |
101 QString extpath = QString("%1/easyhg.py").arg(target); | |
102 return extpath; | |
103 } | |
104 | |
105 QString HgRunner::unbundleExtension() | |
106 { | |
107 // Pull out the bundled Python file into a temporary file, and | |
108 // copy it to our known extension location, replacing the magic | |
109 // text NO_EASYHG_IMPORT_PATH with our installation location | |
110 | |
111 QString bundled = ":easyhg.py"; | |
112 QString unbundled = getUnbundledFileName(); | |
113 | |
114 QString target = QFileInfo(unbundled).path(); | |
84 if (!QDir().mkpath(target)) { | 115 if (!QDir().mkpath(target)) { |
85 DEBUG << "Failed to make unbundle path " << target << endl; | 116 DEBUG << "Failed to make unbundle path " << target << endl; |
86 std::cerr << "Failed to make unbundle path " << target.toStdString() << std::endl; | 117 std::cerr << "Failed to make unbundle path " << target << std::endl; |
87 return ""; | 118 return ""; |
88 } | 119 } |
120 | |
89 QFile bf(bundled); | 121 QFile bf(bundled); |
90 if (!bf.exists()) { | 122 DEBUG << "unbundle: bundled file will be " << bundled << endl; |
123 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) { | |
91 DEBUG << "Bundled extension is missing!" << endl; | 124 DEBUG << "Bundled extension is missing!" << endl; |
92 return ""; | 125 return ""; |
93 } | 126 } |
94 QString extpath = QString("%1/easyhg.py").arg(target); | 127 |
95 if (QFile(extpath).exists()) { | 128 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target)); |
96 QFile(extpath).remove(); | 129 tmpfile.setAutoRemove(false); |
97 } | 130 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl; |
98 if (!bf.copy(extpath)) { | 131 if (!tmpfile.open()) { |
99 DEBUG << "Failed to unbundle extension to " << target << endl; | 132 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl; |
100 std::cerr << "Failed to unbundle extension to " << extpath.toStdString() << std::endl; | 133 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl; |
101 return ""; | 134 return ""; |
102 } | 135 } |
103 DEBUG << "Unbundled extension to " << extpath << endl; | 136 |
104 return extpath; | 137 QString all = QString::fromUtf8(bf.readAll()); |
138 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath); | |
139 tmpfile.write(all.toUtf8()); | |
140 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl; | |
141 | |
142 tmpfile.close(); | |
143 | |
144 QFile ef(unbundled); | |
145 if (ef.exists()) { | |
146 DEBUG << "unbundle: removing old file " << unbundled << endl; | |
147 ef.remove(); | |
148 } | |
149 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl; | |
150 if (!tmpfile.rename(unbundled)) { | |
151 DEBUG << "Failed to move temporary file to target file " << unbundled << endl; | |
152 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl; | |
153 return ""; | |
154 } | |
155 | |
156 DEBUG << "Unbundled extension to " << unbundled << endl; | |
157 return unbundled; | |
105 } | 158 } |
106 | 159 |
107 void HgRunner::requestAction(HgAction action) | 160 void HgRunner::requestAction(HgAction action) |
108 { | 161 { |
109 DEBUG << "requestAction " << action.action << endl; | 162 DEBUG << "requestAction " << action.action << endl; |